Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeMCU Publish DHT22 Temperature and Humidity to MQTT
#include <DHT.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <math.h>
const char* essid = "my_wifi_ssid";
const char* password = "my_wifi_password";
const char* topic = "esp8266";
const char* server = "my_server_hostname_or_ip";
const int PIN_LED = BUILTIN_LED;
const int PIN_DHT22 = D2;
DHT dht(PIN_DHT22, DHT22);
uint8_t mac[6];
float fTemperature = 0.0f;
float fHumidity = 0.0f;
void callback(char* topic, byte* payload, unsigned int length)
{
// Do Nothing
}
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
String macToStr(const uint8_t *mac)
{
String result;
for(int i = 0; i < 6; ++i)
{
result += String(mac[i], 16);
if (i < 5)
result += '-';
}
return result;
}
void reportStatus() {
float fReading = dht.readTemperature(true);
while(isnan(fReading))
{
delay(100);
fReading = dht.readTemperature(true);
}
fTemperature = fReading;
fReading = dht.readHumidity();
while(isnan(fReading))
{
delay(100);
fReading = dht.readHumidity();
}
fHumidity = fReading;
Serial.print("Temperature: "); Serial.println(fTemperature);
Serial.print("Humidity: "); Serial.println(fHumidity);
String t;
t += topic;
t += "/";
t += macToStr(mac);
String payload;
payload += "{";
payload += "\"temperature\":";
payload += String(fTemperature);
payload += ", ";
payload += "\"humidity\":";
payload += String(fHumidity);
payload += "}";
Serial.println("Sending payload");
Serial.println(payload);
Serial.println("To topic:");
Serial.println(t);
if (client.publish(t.c_str(), payload.c_str(), true))
Serial.println("Publish ok");
else
{
Serial.print("Publish failed, rc=");
Serial.println(client.state());
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientName;
clientName += "esp8266_";
clientName += macToStr(mac);
clientName += "_";
clientName += String(micros() & 0xff, 16);
if (client.connect((char*) clientName.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
dht.begin();
Serial.begin(115200);
Serial.setTimeout(2000);
while (!Serial) { }
pinMode(BUILTIN_LED, OUTPUT); // set onboard LED as output
Serial.println("Connecting to home network");
WiFi.mode(WIFI_STA);
WiFi.begin(essid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
digitalWrite(BUILTIN_LED, HIGH);
delay(250);
digitalWrite(BUILTIN_LED, LOW);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setCallback(callback);
while(!client.connected()) {
reconnect();
}
WiFi.macAddress(mac);
String clientName;
clientName += "esp8266_";
clientName += macToStr(mac);
clientName += "_";
clientName += String(micros() & 0xff, 16);
if (client.connect((char*) clientName.c_str()))
{
Serial.println("Connected to MQTT broker as ");
Serial.println(clientName);
Serial.print("Topic is: ");
Serial.println(topic);
}
}
void loop() {
delay(1000); // allow network to come up
reportStatus();
delay(10000); // allow time for TCP packet to complete transaction
Serial.println("Going into deep sleep for 10 minutes");
ESP.deepSleep(589e6, WAKE_RF_DEFAULT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.