#include #include #include #include #include #include #include #include #include #ifndef WIFI_SSID #define WIFI_SSID "ReplaceThis" #endif #ifndef WIFI_PASS #define WIFI_PASS "ReplaceThis" #endif #ifndef MQTT_SERVER #define MQTT_SERVER "m21.cloudmqtt.com" #endif #ifndef MQTT_USER #define MQTT_USER "ReplaceThis" #endif #ifndef MQTT_PASS #define MQTT_PASS "ReplaceThis" #endif #ifndef MQTT_PORT #define MQTT_PORT 15739 // MQTT_PORT default for me #endif #ifndef MQTT_TOPIC #define MQTT_TOPIC "lightingLivingRoom" // MQTT_TOPIC default for me #endif WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); // Switch on the LED if an 1 was received as first character if ((char)payload[0] == '1') { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.println("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("alive", "hello world from ESP8266Client1"); // ... and resubscribe client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup_wifi() { WiFi.mode(WIFI_STA); WiFi.setPhyMode(WIFI_PHY_MODE_11N); // Low power WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } // Port defaults to 8266 // ArduinoOTA.setPort(8266); // Hostname defaults to esp8266-[ChipID] //ArduinoOTA.setHostname("ESP8266-WITTY-RGB"); // No authentication by default //ArduinoOTA.setPassword((const char *)"admin"); ArduinoOTA.onStart([]() { Serial.println("Start"); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("OTA Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void setup(){ Serial.begin(115200); Serial.println("Booting"); setup_wifi(); client.setServer(MQTT_SERVER, MQTT_PORT); client.setCallback(callback); } void loop(){ ArduinoOTA.handle(); if (!client.connected()){ reconnect(); } client.loop(); int value = LightSensor.getAveragedValueTimes(6); snprintf (msg, 75, "device: %ld lighting: %ld", "0", value); Serial.print("Publish message: "); Serial.println(msg); client.publish(MQTT_TOPIC, msg); Serial.println("Going to sleep for 8 sec"); ESP.deepSleep(1000000*8); // 8 seconds deepsleep Serial.println("Sleeping"); // Should not be reached and printed }