Making Projects with ESP8266 is always fun. In this tutorial we will learn to interface ESP8266 with DHT11 or DHT22 sensor, Read Temperature(also capable of Humidity) information and send it to RETE IOT server using MQTT protocol.
Before You Jump Into Tutorial:
Read this tutorial >> Rete IOT introduction and ESP8266 Switch Control Tutorial
Other ESP Projects >> Facebook Chat Device Control Using ESP8266
Video Demo OF DHT11 ESP8266 Interfacing:
DHT11 Library Download and Installation:
- Click Here to download ESP8266 library for arduino
- Extract The zip file >> COPY DHT folder to >> Your C Drive
- Paste it in Document Folder of your C Drive
- Open Arduino IDE, You are all set
If you are New To ESP8266 Arduino:
Follow this tutorial , if you are new , and don’t have idea about ESP8266. In this tutorial , I have explained how to install esp8266 library in Arduino IDE.
DHT11 Datasheet:
If you want to learn more about DHT11, you can checkout this datasheet.
Circuit Diagram:
Download DHT11 ESP8266 code and Upload:
- Click Here to Download DHT11 ESP8266 source code
- Change Your home wifi SSID and Password Name
1 2 |
const char* ssid = "YourWIFIName"; // change to your home wifi name const char* password = "YourWifiPassword"; // change to your home wifi password |
- Login to Reteiot.com , If you dont have one account, Please sign up.
- In the esp8266 arduino source code, change your Rete IOT email ID
1 |
ESP8266 DHT11 Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <Adafruit_Sensor.h> #include <DHT.h> #include <DHT_U.h> #define DHTTYPE DHT11 // Update these with values suitable for your network. const char* ssid = "YourWifiName"; // change to your home wifi name const char* password = "YourWifiPassword"; // change to your home wifi password const char* mqtt_server = "io.reteiot.com"; // MQTT broker Name const int DHTPin = 5; // Initialize DHT sensor. DHT dht(DHTPin, DHTTYPE); static char fahrenheitTemp[7]; static char celsiusTemp[7]; static char humidityTemp[7]; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { digitalWrite(BUILTIN_LED, LOW); delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); digitalWrite(BUILTIN_LED, HIGH); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } 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] == 'o' && (char)payload[1]== 'n') { // Here we are comparing if ON command is coming from server. // digitalWrite(D7, HIGH); digitalWrite(D3, HIGH); } else { // digitalWrite(D7, LOW); // else turn off led digitalWrite(D3, LOW); } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str())) { Serial.println("connected"); digitalWrite(BUILTIN_LED, HIGH); } 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() { dht.begin(); pinMode(BUILTIN_LED, OUTPUT); // on board led for mqtt status indication. // pinMode(D7, OUTPUT); // our Buzzer is connected to D7 pinMode(D3, OUTPUT); // lED Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); digitalWrite(BUILTIN_LED, HIGH); digitalWrite(D3, LOW); } int readTemp(){ delay(5000); float f = dht.readTemperature(true); float t = dht.readTemperature(); float h = dht.readHumidity(); if (isnan(f)) { Serial.println("Failed to read from DHT sensor!"); strcpy(fahrenheitTemp, "Failed"); } else{ // // Serial.print("Humidity: "); // Serial.print(h); // Serial.print(" %\t Temperature: "); // Serial.print(t); // Serial.print(" *C "); // Serial.print(f); // Serial.print(" *F\n "); String jsonToSend = String(t) ; char toBeSend[100]; jsonToSend.toCharArray(toBeSend, sizeof(toBeSend)); Serial.print(jsonToSend); Serial.print("\n"); } } void loop() { if (!client.connected()) { digitalWrite(BUILTIN_LED, LOW); reconnect(); } readTemp(); client.loop(); } |
- Compile and Upload the code.
- Now You can see the Temperature sensor on your Rete IoT dashboard.
Hope You Find this tutorial useful.
If you Like, Please share with your friends, so others will get benefited.