#include #include #include #include // Use Device NodeMCU 1.0 #define WIFI_SSID "myssid" #define WIFI_PASS "mypassword" #define SERIAL_BAUDRATE 115200 // Adafruit IO #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "myusername" #define AIO_KEY "mykey" #define RELAY_ON_TIME 500 // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'outside' for subscribing to changes. // Value for Outside Feed are: // 1,ON // 1,OFF // 2,ON // 2,OFF // 3,ON // 3,OFF Adafruit_MQTT_Subscribe feed = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/outside"); // ----------------------------------------------------------------------------- // wifiSetup() // ----------------------------------------------------------------------------- void wifiSetup() { // Set WIFI module to STA mode WiFi.mode(WIFI_STA); // Connect Serial.printf("[WIFI] Connecting to SSID[%s] ", WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASS); // Wait while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println(); // Connected! Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); } // ----------------------------------------------------------------------------- // af_connect() -- connect to adafruit io via MQTT // ----------------------------------------------------------------------------- void af_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); } // ----------------------------------------------------------------------------- // process_request() // ----------------------------------------------------------------------------- void process_request(uint8_t device_id, const char * device_name, bool state) { Serial.print(" Set "); Serial.print(device_name); Serial.print(" state to "); if (state) { Serial.println("ON"); } else { Serial.println("OFF"); } if (strcmp(device_name,"outside one") == 0){ Serial.println(" Action: outside one"); if (state) { digitalWrite(15, HIGH); //S1 D3 0 delay(RELAY_ON_TIME); digitalWrite(15, LOW); } else { digitalWrite(10, HIGH); //S2 D4 1 delay(RELAY_ON_TIME); digitalWrite(10, LOW); } } else if (strcmp(device_name, "outside two") == 0) { Serial.println(" Action: outside two"); if (state) { digitalWrite(5, HIGH); //S7 D12 2 delay(RELAY_ON_TIME); digitalWrite(5, LOW); } else { digitalWrite(4, HIGH); //S6 D11 3 delay(RELAY_ON_TIME); digitalWrite(4, LOW); } } else if (strcmp(device_name, "outside three") == 0){ Serial.println(" Action: outside three"); if (state) { digitalWrite(13, HIGH); //S9 D10 4 delay(RELAY_ON_TIME); digitalWrite(13, LOW); } else { digitalWrite(16, HIGH); //S8 D16 5 delay(RELAY_ON_TIME); digitalWrite(16, LOW); } } else { Serial.println(" Device Not Found!"); } } // ----------------------------------------------------------------------------- // process_afmessage() // Values for Outside Feed are: // 1,ON // 1,OFF // 2,ON // 2,OFF // 3,ON // 3,OFF // ----------------------------------------------------------------------------- void process_afmessage(String message) { if (message == "1,ON") { process_request(NULL, "outside one", 1); } else if (message == "1,OFF") { process_request(NULL, "outside one", 0); } else if (message == "2,ON") { process_request(NULL, "outside two", 1); } else if (message == "2,OFF") { process_request(NULL, "outside two", 0); } else if (message == "3,ON") { process_request(NULL, "outside three", 1); } else if (message == "3,OFF") { process_request(NULL, "outside three", 0); } } // ----------------------------------------------------------------------------- // setup() // ----------------------------------------------------------------------------- void setup() { // Init serial port and clean garbage pinMode(4, OUTPUT); digitalWrite(4, LOW); //S6 pinMode(5, OUTPUT); digitalWrite(5, LOW); //S7 pinMode(10, OUTPUT); digitalWrite(10, LOW); //S9 pinMode(12, OUTPUT); digitalWrite(12, LOW); //S3 pinMode(13, OUTPUT); digitalWrite(13, LOW); //S4 pinMode(14, OUTPUT); digitalWrite(14, LOW); //S2 pinMode(15, OUTPUT); digitalWrite(15, LOW); //S8 pinMode(16, OUTPUT); digitalWrite(16, LOW); //S1 Serial.begin(SERIAL_BAUDRATE); Serial.println("IFTTT to AF to RF"); // Wifi wifiSetup(); // listen for events on the lamp feed mqtt.subscribe(&feed); // connect to adafruit io af_connect(); } // ----------------------------------------------------------------------------- // loop() // ----------------------------------------------------------------------------- void loop() { int i; Adafruit_MQTT_Subscribe *subscription; // Adafruit feed monitoring // ping adafruit io a few times to make sure we remain connected if(! mqtt.ping(3)) { // reconnect to adafruit io if(! mqtt.connected()) af_connect(); } // Wait for incoming subscription packets // If we read one packet then stay in the while loop until no packets are read and we timeout while ((subscription = mqtt.readSubscription(5000))) { // we only care about the lamp events if (subscription == &feed) { // convert mqtt ascii payload to int char *value = (char *)feed.lastread; Serial.print(F("Received AF Msg: ")); Serial.println(value); // Froccess Feed Message String message = String(value); message.trim(); process_afmessage(message); } } }