ESP8266 ESP01 module sends temperature data to Adafruit MQTT

There are a couples of ways to use ESP8266 to send data to other devices. It can be used as a server which it can sends a response to a client's request. Or it can send data to other server such as MQTT server.
For this project, the ESP01 will send temperature data to Adafruit MQTT server.
Below is the wiring diagram :-
In order to use Adafruit MQTT,  you need to create an Adafruit account at https://io.adafruit.com. To know more about Adafruit MQTT; visit https://learn.adafruit.com/mqtt-adafruit-io-and-you/ .

While I was doing this project, I stumbled on a few problems :-
Problem and solution:-
1. DHT11 sensor - This sensor uses external 5v power supply.
2. Error occurred while trying to connect to MQTT.
- To solve this problem, I did not store MQTT server, username, password and path in flash memory.

Note :
- Previously, I used capacitors as part of components for ESP01 power supply. Later I found out that batteries and voltage regulator which are also part of components for ESP01 power supply became hot. Once I removed the capacitors, there was no heat problem anymore.
- I also found out that voltage regulator that has 500mA output current was not enough to power the ESP01. So I change to voltage regulator with 1A output current.

Here are the libraries that are used in this project :-
DHT11 - https://arduino-info.wikispaces.com/file/detail/DHT-lib.zip
Adafruit MQTT - https://github.com/adafruit/Adafruit_MQTT_Library

As for the code, initially I wanted to use the code from this link; https://www.hackster.io/mtashiro/temp-sensor-connected-to-esp8266-and-upload-data-using-mqtt-5e05c9 (Credit to Mark Tashiro), but it was using DHT11 library from Adafruit. I already have DHT11 library from https://arduino-info.wikispaces.com so I had to modified the code. Another modification that I made is the way it stores the MQTT server, username, password and path which I have pointed out the problem earlier.
Here is the code :-
 #include <ESP8266WiFi.h>  
 #include <Adafruit_MQTT.h>  
 #include <Adafruit_MQTT_Client.h>  
 #include <dht11.h>  
 /* WIFI SETUP */  
 #define WLAN_SSID    "...your SSID..." //Put your SSID here  
 #define WLAN_PASS    "...your password..." //Put you wifi password here  
 /* ADAFRUIT IO SETUP */  
 #define AIO_SERVER   "io.adafruit.com"  
 #define AIO_SERVERPORT 1883          // use 8883 for SSL  
 #define AIO_USERNAME  "...your username..."   //Put your Adafruit userid here  
 #define AIO_KEY     "...your key..."  //Put your Adafruit IO key here  
 dht11 DHT11;  
 #define DHT11PIN 2  
 /************ Global State (you don't need to change this!) ******************/  
 // Create an ESP8266 WiFiClient class to connect to the MQTT server.  
 WiFiClient client;  
 // or... use WiFiFlientSecure for SSL  
 //WiFiClientSecure client;  
 // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.  
 //Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); /* There a bug in Adafruit library when using PROGMEM */  
 Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);  
 /************************* Feeds ***************************************/  
 // Setup a feed called 'temp' for publishing.  
 // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>  
 Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");  
 /********************** Sketch Code ************************************/  
 // Bug workaround for Arduino 1.6.6, it seems to need a function declaration  
 // for some reason (only affects ESP8266, likely an arduino-builder bug).  
 //void MQTT_connect();  
 void setup(){  
  Serial.begin(115200);  
  delay(10);  
  Serial.println("DHT11 TEST PROGRAM ");  
  Serial.print("LIBRARY VERSION: ");  
  Serial.println(DHT11LIB_VERSION);  
  Serial.println();  
  Serial.println(F("MQTT Temp"));  
  // Connect to WiFi access point.  
  Serial.println(); Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(WLAN_SSID);  
  WiFi.begin(WLAN_SSID, WLAN_PASS);  
  while (WiFi.status() != WL_CONNECTED) {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println();  
  Serial.println("WiFi connected");  
  Serial.println("IP address: "); Serial.println(WiFi.localIP());  
 }  
 //int delayTime = 300000; //Wait 5 minutes before sending data to web  
 int delayTime = 60000;  
 int startDelay = 0;  
 float temp_f; // Values read from sensor  
 void loop(){  
  Serial.println("\n");  
  int chk = DHT11.read(DHT11PIN);  
  Serial.print("Read sensor: ");  
  switch (chk)  
  {  
   case DHTLIB_OK:   
   Serial.println("OK");   
   break;  
   case DHTLIB_ERROR_CHECKSUM:   
   Serial.println("Checksum error");   
   break;  
   case DHTLIB_ERROR_TIMEOUT:   
   Serial.println("Time out error");   
   break;  
   default:   
   Serial.println("Unknown error");   
   break;  
  }  
  Serial.print("Temperature (C): ");  
  Serial.println((float)DHT11.temperature, 2);  
  // ---- Start MQTT connection ---   
  // Ensure the connection to the MQTT server is alive (this will make the first  
  // connection and automatically reconnect when disconnected). See the MQTT_connect  
  // function definition further below.  
  MQTT_connect();  
  // Now we can publish stuff!  
  if (millis() - startDelay < delayTime) {  
   Serial.println("waiting delaytime");  
  } else {  
   temp_f = (float)DHT11.temperature;        //Get temp in Celsius  
   startDelay = millis();  
   //Serial.print(F("\nSending temp: "));  
   Serial.print("\nSending temp: ");  
   Serial.print(temp_f);  
   Serial.print("...");  
   if (! temp.publish(temp_f)) {           //Publish to Adafruit  
    //Serial.println(F("Failed"));  
    Serial.println("Failed");  
   } else {  
    //Serial.println(F("Sent!"));  
    Serial.println("Sent!");  
   }  
  }  
  // ping the server to keep the mqtt connection alive  
  // NOT required if you are publishing once every KEEPALIVE seconds  
  if(! mqtt.ping()) {  
   mqtt.disconnect();  
  }  
  delay(2000);  
 }// End of loop  
 // Function to connect and reconnect as necessary to the MQTT server.  
 // Should be called in the loop function and it will take care if connecting.  
 void MQTT_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!");  
 }  
 //Celsius to Fahrenheit conversion  
 double Fahrenheit(double celsius)  
 {  
  return 1.8 * celsius + 32;  
 }  
 // fast integer version with rounding  
 //int Celcius2Fahrenheit(int celcius)  
 //{  
 // return (celsius * 18 + 5)/10 + 32;  
 //}  
 //Celsius to Kelvin conversion  
 double Kelvin(double celsius)  
 {  
  return celsius + 273.15;  
 }  

Comments