NodeMCU Temperature & Humidity

Updated: Dec 3, 2021


In this example, we will publish the dht11 sensor and temperature and humidity information to our smartespiot software.


After reviewing our Nodemcu first message example, you can continue with this example.


DHT 11 sensor is connected to D5-GPIO14 pin of NODEMCU.


The library we will use and the necessary settings are below.


// settings dht
#include "DHT.h"
#include <DHT_U.h>
#define DHTPIN 14 // esp8266 d5
// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

float t = 0.0;
float h = 0.0;

//


We should publish the data in the same format again.

client.publish("devicename/temp", String(t).c_str());// send temp
  client.publish("devicename/hum", String(h).c_str());// send hum



#include <ESP8266WiFi.h> //The library we will use for the wifi connection
#include <PubSubClient.h> // The library we will use for the mqtt connection

//mqtt connection library settings
WiFiClient espClient;
PubSubClient client(espClient);

// settings dht
#include "DHT.h"
#include <DHT_U.h>
#define DHTPIN 14 // esp8266 d5
// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

float t = 0.0;
float h = 0.0;

//



//settings for internet and mqtt connection
const char* ssid = ""; // wifi SSID
const char* password =  ""; // wifi password 
const char* mqttServer = ""; // mqtt server ip  to be sent by smartespiot by mail 
const int mqttPort = ; // mqtt port   to be sent by smartespiot by mail 
const char* mqttUser = ""; // mqtt username  to be sent by smartespiot by mail 
const char* mqttPassword = ""; // mqtt pass to be sent by smartespiot by mail 



//
void setup() {
  Serial.begin(115200); // start serial communication at 115200 speed

  dht.begin();  // begin dht 
  
  
  WiFi.begin(ssid, password); // start the wifi connection process

  while (WiFi.status() != WL_CONNECTED) { //
    delay(500); //wait 500ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
  }
  Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port

  client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port

  while (!client.connected()) { //wait until we connect to mqtt
    Serial.println("Connecting to MQTT server...");

    if (client.connect("SmartEspIOT", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name

      Serial.println("MQTT connection established :) ");

    } else {

      Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
      Serial.print(client.state());             // print the problem description
      // We can understand the problem according to the information returned from the clients state.
      //-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
      //-3 : MQTT_CONNECTION_LOST - network connection lost
      //-2 : MQTT_CONNECT_FAILED - network connection failed
      //-1 : MQTT_DISCONNECTED - disconnected
      //0 : MQTT_CONNECTED - connection established
      //1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
      //2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
      //3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
      //4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
      //5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect


      delay(2000);   // wait two seconds and try connecting again

    }
  }
}

void loop() {

  client.loop(); // run the mqtt data processing function
  delay(50);
  t= dht.readTemperature(); // read temp from dht
  Serial.print("Temp C:");
   Serial.println(t); // read hum from dht
   delay(50);
   h= dht.readHumidity();
   Serial.print("Hum % :");
   Serial.println(h);
  
  client.publish("devicename/temp", String(t).c_str());// send temp
  client.publish("devicename/hum", String(h).c_str());// send hum
  delay(2000); // wait two seconds and send the message again
  Serial.println("message send");
}





temp_hum_smartespiot
.zip
Download ZIP • 2KB







31 views0 comments

Recent Posts

See All