Created
June 20, 2016 04:11
-
-
Save balloob/1176b6d87c2816bd07919ce6e29a19e9 to your computer and use it in GitHub Desktop.
Sketch for the ESP8266 to publish temperature and humidity values received from a DHT22 to MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get ESP8266 going with Arduino IDE | |
// - https://github.com/esp8266/Arduino#installing-with-boards-manager | |
// Required libraries (sketch -> include library -> manage libraries) | |
// - PubSubClient by Nick ‘O Leary | |
// - DHT sensor library by Adafruit | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <DHT.h> | |
#define wifi_ssid "YOUR WIFI SSID" | |
#define wifi_password "WIFI PASSWORD" | |
#define mqtt_server "YOUR_MQTT_SERVER_HOST" | |
#define mqtt_user "your_username" | |
#define mqtt_password "your_password" | |
#define humidity_topic "sensor/humidity" | |
#define temperature_topic "sensor/temperature" | |
#define DHTTYPE DHT22 | |
#define DHTPIN 14 | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 | |
void setup() { | |
Serial.begin(115200); | |
dht.begin(); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.begin(wifi_ssid, wifi_password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
// If you do not want to use a username and password, change next line to | |
// if (client.connect("ESP8266Client")) { | |
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { | |
Serial.println("connected"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
bool checkBound(float newValue, float prevValue, float maxDiff) { | |
return !isnan(newValue) && | |
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); | |
} | |
long lastMsg = 0; | |
float temp = 0.0; | |
float hum = 0.0; | |
float diff = 1.0; | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 2000) { | |
lastMsg = now; | |
float newTemp = dht.readTemperature(); | |
float newHum = dht.readHumidity(); | |
if (checkBound(newTemp, temp, diff)) { | |
temp = newTemp; | |
Serial.print("New temperature:"); | |
Serial.println(String(temp).c_str()); | |
client.publish(temperature_topic, String(temp).c_str(), true); | |
} | |
if (checkBound(newHum, hum, diff)) { | |
hum = newHum; | |
Serial.print("New humidity:"); | |
Serial.println(String(hum).c_str()); | |
client.publish(humidity_topic, String(hum).c_str(), true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depending on their values, taking an average could work.