ESP8266 Deep sleep with MQTT
This is the continuation of the previous post regarding ESP8266 Deep sleep. In that post we discussed about how to utilise the deep sleep mode to save the power and wake up only to read temperature and post the result. In this post we will utilise the same mechanism, the ESP8266 will perform the deep sleep and only wake up every 2 minute to read the temperature and post the result to MQTT broker.
Defining the connection
The MQTT broker we use for this example is shiftr.io, you can get a free account with them if things are starting to get serious. In this example we are only using the default try account, so the definition is done as below:
// MQTT info const char* thehostname = "broker.shiftr.io"; int port = 10031; const char* user = "try"; const char* user_password = "try"; const char* id = "ESP8266";
Setting up
When the ESP8266 wake up, it will need to first initialising the WiFi, initialising the connection to the MQTT host, in this case the “broker.shiftr.io”. Then the connectToWifi() function will be called, this is the procedure that actually trying to connect to the wifi, and when success it will connect to the MQTT broker. Once this is all done, the program will then read the analog temperature sensor connected to A0,and post the data using data(), and goes back to sleep for another 2 minutes. The setup() function looks like the following:
void setup() { Serial.begin(115200); Serial.println("device is in Wake up mode"); while (!Serial) { } Serial.print("Connecting to: SSID NAME"); //uncomment next line to show SSID name //Serial.print(ssid); WiFi.begin(ssid, password); Serial.println(" ");// print an empty line // Connect to MQTT client.begin(thehostname, net); client.onMessage(messageReceived); connectToWifi(); int value = analogRead(A0); float volts=(value * 3.03) / 1024; //conversion to volts tempC = volts*100.0; //conversion to temp Celsius Serial.print("Temperature C: "); Serial.println(tempC); data(); Serial.println("deep sleep for 120 seconds"); ESP.deepSleep(120e6); }
Connecting to Wifi and MQTT
The connectToWifi() function is attempting to connect with a second delay until 10 tries, when it is successful, it will then attempt to connect to the MQTT broker with a second delay in between tries. Upon successful connection it will subscribe to “/homeautomation/temperature” node. The code is described as below:
void connectToWifi() { Serial.print("Attempting to connect: "); //try to connect for 10 seconds int i = 10; while(WiFi.status() != WL_CONNECTED && i >=0) { delay(1000); Serial.print(i); Serial.print(", "); i--; } Serial.println(" ");// print an empty line //print connection result if(WiFi.status() == WL_CONNECTED){ Serial.print("Connected."); Serial.println(" ");// print an empty line Serial.print("NodeMCU ip address: "); Serial.println(WiFi.localIP()); Serial.print("\nconnecting to MQTT…"); while (!client.connect(id, user, user_password)) { Serial.print("."); delay(1000); } client.subscribe("/homeautomation/temperature"); // client.unsubscribe("/hello"); } else { Serial.println("Connection failed - check your credentials or connection"); } }
Posting the temperature to MQTT broker
The function data() will post the temperature data to MQTT broker, this is shown below:
void data() { if (client.connected()) { Serial.println("MQTT client connected"); client.publish("/homeautomation/temperature", String(tempC)); delay(1000); } else { Serial.println("MQTT client not connected"); } }
If all is well you will see the temperature is posted in the shiftr.io broker as shown in the following picture
If you would like to get the full source code, it can be found in the following link. That would be all for today, I hope you like this post, and don’t forget to subscribe for more similar post related to Arduino and IoT.