ESP8266

ESP8266 with Adafruit IoT Platform, Adafruit IO, Adafruit MQTT

Adafruit IoT Platform:

 

ESP8266 with Adafruit IoT Platform, Adafruit IO, Adafruit MQTT– In this article, you will learn how to use Nodemcu ESP8266 WiFi module with Adafruit IoT Platform or Adafruit IO for sensor monitoring.

Since this is a getting started tutorial on the Adafruit IO and I have to explain the most basic things including, how to create a free account on Adafruit IO, how to create your first IoT dashboard or Adafruit IO Applet, how to install the Adafruit MQTT Library, and how to write a very simple program to send the sensor data to the Adafruit IoT platform; So, I decided to use DHT11 Temperature and Humidity sensor with the Nodemcu ESP8266 WiFi module. For this project, you will also need an Oled display module, the one you can see in the image above is the SSD1306 I2C-supported Oled display module and you will also need a 5v buzzer. If you don’t want to the use Oled display and buzzer, its ok, you can continue with ESP8266 and DHT11 sensor.


You might be thinking why Adafruit IO? Well, Adafruit IO is an IoT platform designed to display, respond, and interact with your project’s data. Adafruit IO keeps your data private and secure for you. It’s the internet of things for everyone, this means with Adafruit IO or Adafruit IoT platform you can monitor your sensors and control your electrical or electronic devices from any part of the world provided if the internet connection is available. Adafruit IO is absolutely free forever. But, if you need more dashboards and features then you can also try the paid version.

Adafruit IoT Platform

Here is my Nodemcu ESP8266 development board along which DHT11 Temperature and Humidity Sensor, SSD1306 Oled display module, and a 5V buzzer are connected. Let’s kick off our practical demonstration and afterward I will explain everything else.



Altium Designer + Altium 365 + Octopart:

Arduino LoRa Free SMS

Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.

Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.


Adafruit IoT Platform

I have powered up the Nodemcu ESP8266 WiFi module using a 5V regulated power supply. If you don’t have an external power supply then you can use your computer or laptop to power up the Nodemcu ESP8266 WiFi Module.

Anyway, right now my Nodemcu module and Laptop are connected to the Internet.  You can see the temperature and humidity values on the Oled display module and also on the Gauges. You might be thinking why am I displaying data on the Oled display and Gauges? Why not only Oled display or Adafruit IO? Well, my friends, if you are near to the circuit then there is no need to open the Adafruit IO, but suppose if you are away, out of a town, or even out of the country, then in a situation like this, you can use your Adafruit IoT Platform for monitoring the Temperature and Humidity values.


Besides this, I have also added code for controlling the buzzer. So, whenever the temperature increases above 40 degrees Celsius, the buzzer automatically turns ON. Let me practically demonstrate it for you.

Adafruit IoT Platform

I applied heat to the Dht11 sensor, I was able to monitor the temperature and humidity values on the Oled display module and also on the Adafruit IO dashboard. As you can see right now the temperature is above 40o Celsius, at this very moment the buzzer was also turned ON.

After a minute or so the temperature started to decrease. And when the temperature dropped below 40 degrees Celsius the Buzzer turned OFF.

Adafruit IoT Platform

I am sure by now, you might have got an idea of how does this system work. So, without any further delay let’s get started!!!


Amazon Links:

DHT11 Temperature and Humidity sensor

SSD1306 Oled display Module

5V buzzer

ESP8266 D1 Mini

ESP8266 Nodemcu

ESP32 WiFi + Bluetooth Module

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



DHT11 Sensor:

The DHT11 Temperature and Humidity Sensor is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. It is simple to use, but requires careful timing to grab data.

Adafruit IoT Platform

Description:

  • Low cost
  • 3 to 5V power and I/O
  • 5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm


ESP8266 (NodeMCU):

ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.
ESP8266 has powerful on-board processing and storage capabilities that allow it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, and the entire solution, including front-end module, is designed to occupy minimal PCB area.

Adafruit IoT Platform

Specifications:

  • Serial/UART baud rate: 115200 bps
  • Input power: 3.3V
  • I/O voltage tolerance: 3.6V Max
  • Flash Memory Size: 1MB (8Mbit)
  • WiFi security modes: WPA, WPA2

DHT11 sensor with ESP8266 NodeMCU:

Adafruit IoT Platform

The DHT11 sensor S pin or Data pin is connected with D4 pin of the Nodemcu ESP8266 WiFi module. While the VCC and GND pins are connected with 3.3V and GND pins of the ESP8266.

The 5V buzzer is controlled using D5 pin on the Nodemcu ESP8266 WiFi module. The Red wire of the buzzer is connected with the Vin pin which is connected with the regulated 5 volts. We use 2N2222 NPN transistor and a 10K ohm resistor to control the Buzzer.

The GND and VDD pins of the Oled display module are connected with the Nodemcu module GND and 3.3V pins respectively. While the SCK or SCL and SDA pins of the Oled display are connected with the D1 and D2 pins on the Nodemcu module. D1 is the SCL and D2 is the SDA.

On the Left side is the regulated 5V power supply based on the linear voltage regulator 7805. Don’t forget to add these 470uF decoupling capacitors at the input and output sides of the regulator. So, that’s all about the circuit diagram and now let’s start with the Adafruit IO.


Designing Adafruit IO applet:

Go to https://io.adafruit.com/

If you are a new user then first you will need to create the account in the Adafruit IO by clicking on the Get Started for Free.

Adafruit IoT Platform

To sign up for a free account, enter the required details “Enter first name, last name, email, username, and password” Finally, click on the CREATE ACCOUNT button

Adafruit IoT Platform

Once you click on the CREATE ACCOUNT button, your free Adafruit IO account will be created, which you can use for creating amazing dashboards. Anyway, just re-open your Adafruit IO account and enter your login credentials. When you have successfully logged into your account, click on the IO and then click on the New Dashboard. Now, enter your dashboard name, you can select any name, but it’s good to select a related name. In my case, I am going to select Temperature Monitoring. If you want you can also add a little bit of description, e.g you can explain the purpose of creating this dashboard etc. Anyway, when you are done, then finally, you can click on the Create button.

Adafruit IoT Platform

After creating a dashboard, click on the Feeds and then click on the New Feed for Temperature and Humidity. So, first I am going to do it for the Temperature. Simply enter the Feed name and click on the Create button.

Adafruit IoT Platform


In the same way create Feed for the Humidity.

Adafruit IoT Platform

Now, click on the Dashboards, and then click on the Temperature Monitoring.

Adafruit IoT Platform

Then in order to add the gadgets we will Click on the Dashboard Edit Controls and click on the Create New Block.

Adafruit IoT Platform

Different blocks or widgets etc will be displayed. I am interested in Gauge, so I will select two gauges, one I will use for the Humidity and the other one I will use for the temperature.

Adafruit IoT Platform



So, click on the Gauge and select the Feed name Humidity and click on the Next step.

Adafruit IoT Platform

After this we can select the Block Title, Gauge Min Value, and Gauge Max Values. You can also specify the Gauge Width. In the Gauge label, you can write the % sign. Once you are satisfied with all the values then you can click on the Create block button.

Adafruit IoT Platform

Similar steps will be performed for creating the Temperature gauge. Now the two gauges are created.

Adafruit IoT Platform

Again Click on the Dashboard Edit Controls and this time click on the Edit Layout to adjust the gauges. The Adafruit IO Dashboard is ready. Now, let’s go ahead and take a look at the programming.

One more thing, prior to the programming. Open a notepad file. Click on the Key icon.


Just write down the Username and IO Key. Simply copy and paste these values in the notepad, later we will need these in the programming.

Adafruit IoT Platform

If you want to change the key click on the regenerate key; the key will be changed.

Required Libraries:

Before, you start the programming, first of all, make sure you download all the necessary libraries.

I have also a very detailed getting started tutorial on the ESP8266 WiFi module, in which I have explained how to install the ESP8266 using the Boards manager URL link. So, once you install the ESP8266 board the ESP8266WiFi.h will be automatically installed on your system.

The Adafruit_GFX.h and Adafruit_SSD1306.h libraries I have been using for quite a long time and I have already explained how to download and use these libraries.

Download: Adafruit_GFX.h

Download: Adafruit_SSD1306.h

To install the DHT library, simply click on the Sketch menu, go to include library, and then click on the manage libraries. In the search box type DHT.

Adafruit IoT Platform

Select the desired DHT library and install it. As you can see I have already installed this library.

You will also need the Adafruit MQTT library, for this repeat the same steps but this time in the search box type Adafruit MQTT.

Adafruit IoT Platform

Select the desired library and install it.

If you have downloaded and installed all of these libraries then I must congratulate you as you have done all the hard work. The rest of the code is pretty straightforward.


Adafruit IO ESP8266 Programming:

#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"


#define DHTPIN D4 //connect DHT data pin to D4
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// WiFi parameters
#define WLAN_SSID       "AndroidAP3DEC"
#define WLAN_PASS       "electroniclinic"


// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
//Enter the username and key from the Adafruit IO
#define AIO_USERNAME    "electroniclinic"
#define AIO_KEY         "aio_KrWj56Oiufjv84CQca5HfWD8ewWT" 
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);
Adafruit_MQTT_Publish Temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");
Adafruit_MQTT_Publish Humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");

float temp; //to store the temperature value
float hum; // to store the humidity value
int buzzer=D5;
void setup() {
  Serial.begin(115200);
  pinMode(buzzer,OUTPUT);

   pinMode(DHTPIN, OUTPUT);
  dht.begin();          //Begins to receive Temperature and humidity values.  
  Serial.println(F("Adafruit IO Example"));
  // Connect to WiFi access point.
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
    
  }
  Serial.println();
  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // connect to adafruit io
  connect();

}

// connect to adafruit io via MQTT
void connect() {
  Serial.print(F("Connecting to Adafruit IO... "));
  int8_t ret;
  while ((ret = mqtt.connect()) != 0) {
    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(10000);
  }
  Serial.println(F("Adafruit IO Connected!"));
}

void loop() {
  // ping adafruit io a few times to make sure we remain connected
  
 
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }
 temp = dht.readTemperature();
 
 hum = dht.readHumidity();
Serial.print("temperature = ");
Serial.println(temp);
Serial.print("humidity = ");
Serial.println(hum);

  delay(5000);
   
   if (! Temperature.publish(temp)) {                     //Publish to Adafruit
      Serial.println(F("Failed"));
    } 
       if (! Humidity.publish(hum)) {                     //Publish to Adafruit
      Serial.println(F("Failed"));
    }
    else {
      Serial.println(F("Sent!"));
    }
    display.clearDisplay();
     display.setTextSize(2);
  display.setCursor(0, 10);
  display.print(temp);
   display.print((char)247);
  display.print("C");
    display.setTextSize(2);
  display.setCursor(0, 30);
 display.print("H:"+String(hum)+"%"); 

  
  display.display();
if(temp>40)
{
  digitalWrite(buzzer,HIGH);
  }
  else
  {
      digitalWrite(buzzer,LOW);

    }
}


Program Explanation:

Define the pin and type of the DHT sensor you are using. There are some other versions of the dht sensor. Right now I am using DHT11 sensor.

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

These lines of codes are for the Oled display module. The SSD1306 Oled display module I have used in hundreds of projects and its code is always the same. If in case you are not able to see any data on the Oled display module then it means you are using a different version of the Oled display module. You don’t have to be worried, watch my getting started tutorial on the Oled display module, in which I have explained how to solve the Oled display modules related issues.

// WiFi parameters
#define WLAN_SSID       "AndroidAP3DEC"
#define WLAN_PASS       "electroniclinic"

Enter your WiFi router name and password. You can also use a Hotspot.



// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
//Enter the username and key from the Adafruit IO
#define AIO_USERNAME    "electroniclinic"
#define AIO_KEY         "aio_KrWj56Oiufjv84CQca5HfWD8ewWT"

Now, in order to connect your Nodemcu ESP8266 WiFi module with the Adafruit IO, you will need to enter these details. The Adafruit IO Server and the Adafruit IO Server Port. These are same for all the users. But the Adafruit IO username and Adafruit IO key are different. You can copy the username and key from your registered Adafruit IO account.

float temp; //to store the temperature value
float hum; // to store the humidity value
int buzzer=D5;

Define variables for storing the temperature and humidity values. And also define a pin for the Buzzer.

Rest of the code is pretty simple. The ESP8266 WiFi module reads the DHT11 sensor and sends the Temperature and Humidity values to the Adafruit IO Dashboard. Make sure you enter the correct Feed Name, in my case I am using two gauges with Feed Names Temperature and Humidity.

temp = dht.readTemperature();
 
 hum = dht.readHumidity();
Serial.print("temperature = ");
Serial.println(temp);
Serial.print("humidity = ");
Serial.println(hum);

  delay(5000);
   
   if (! Temperature.publish(temp)) {                     //Publish to Adafruit
      Serial.println(F("Failed"));
    } 
       if (! Humidity.publish(hum)) {                     //Publish to Adafruit
      Serial.println(F("Failed"));
    }
    else {
      Serial.println(F("Sent!"));
    }
    display.clearDisplay();
     display.setTextSize(2);
  display.setCursor(0, 10);
  display.print(temp);
   display.print((char)247);
  display.print("C");
    display.setTextSize(2);
  display.setCursor(0, 30);
 display.print("H:"+String(hum)+"%"); 

  
  display.display();
if(temp>40)
{
  digitalWrite(buzzer,HIGH);
  }
  else
  {
      digitalWrite(buzzer,LOW);

    }

Finally, these other instructions are used to display the Temperature and humidity values on the Oled display module and also to check if the temperature is above 40 degrees Celsius or below. So, that’s all about the programming.


Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button