Created
April 7, 2017 13:10
-
-
Save hastarin/b8e3fa3ebda3099eccb8b6d895621310 to your computer and use it in GitHub Desktop.
ESP8266 Feather HUZZAH with DHT22 using WiFiManager and PubSub for MQTT publishing with Home Assistant
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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino | |
#include <DNSServer.h> | |
#include <ESP8266WebServer.h> | |
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient | |
#define DHTPIN 2 | |
#define DHTTYPE DHT22 | |
#define mqtt_server "192.168.1.50" | |
#define mqtt_user "esp1" | |
#define mqtt_password "Xjxy273Et3nHbRqv" | |
#define temperature_topic "office/sensor/temperature" | |
#define humidity_topic "office/sensor/humidity" | |
// DHT sensor | |
DHT_Unified dht(DHTPIN, DHTTYPE); | |
uint32_t delayMS; | |
// Create an ESP8266 WiFiClient class to connect to the MQTT server. | |
WiFiClient espClient; | |
// MQTT vis PubSub | |
PubSubClient client(espClient); | |
void configModeCallback (WiFiManager *myWiFiManager) { | |
Serial.println("Entered config mode"); | |
Serial.println(WiFi.softAPIP()); | |
//if you used auto generated SSID, print it | |
Serial.println(myWiFiManager->getConfigPortalSSID()); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
// wait for serial monitor to open | |
while (! Serial); | |
//WiFiManager | |
//Local intialization. Once its business is done, there is no need to keep it around | |
WiFiManager wifiManager; | |
Serial.println("WiFiManager Magic..."); | |
//reset saved settings | |
//wifiManager.resetSettings(); | |
//set custom ip for portal | |
wifiManager.setAPStaticIPConfig(IPAddress(10, 0, 1, 1), IPAddress(10, 0, 1, 1), IPAddress(255, 255, 255, 0)); | |
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode | |
wifiManager.setAPCallback(configModeCallback); | |
//fetches ssid and pass and tries to connect | |
//if it does not connect it starts an access point with the specified name | |
//and goes into a blocking loop awaiting configuration | |
if (!wifiManager.autoConnect("HASTesp1", "password")) { | |
Serial.println("failed to connect and hit timeout"); | |
//reset and try again, or maybe put it to deep sleep | |
ESP.reset(); | |
delay(1000); | |
} | |
//if you get here you have connected to the WiFi | |
Serial.println("connected...yeey :)"); | |
dht_setup(); | |
client.setServer(mqtt_server, 1883); | |
} | |
long lastMsg = 0; | |
float temp = 0.0; | |
float hum = 0.0; | |
float diff = 1.0; | |
void dht_setup() { | |
dht.begin(); | |
Serial.println("DHTxx Unified Sensor Example"); | |
// Print temperature sensor details. | |
sensor_t sensor; | |
dht.temperature().getSensor(&sensor); | |
Serial.println("------------------------------------"); | |
Serial.println("Temperature"); | |
Serial.print ("Sensor: "); Serial.println(sensor.name); | |
Serial.print ("Driver Ver: "); Serial.println(sensor.version); | |
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); | |
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C"); | |
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C"); | |
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C"); | |
Serial.println("------------------------------------"); | |
// Print humidity sensor details. | |
dht.humidity().getSensor(&sensor); | |
Serial.println("------------------------------------"); | |
Serial.println("Humidity"); | |
Serial.print ("Sensor: "); Serial.println(sensor.name); | |
Serial.print ("Driver Ver: "); Serial.println(sensor.version); | |
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); | |
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); | |
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); | |
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); | |
Serial.println("------------------------------------"); | |
// Set delay between sensor readings based on sensor details. | |
Serial.print ("Min Delay: "); Serial.println(sensor.min_delay); | |
delayMS = sensor.min_delay / 1000; | |
Serial.print ("delayMS: "); Serial.println(delayMS); | |
diff = sensor.resolution * 4; | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("HASTesp1", 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); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
// Delay between measurements. | |
delay(delayMS); | |
client.loop(); | |
// Get temperature event and print its value. | |
sensors_event_t event; | |
dht.temperature().getEvent(&event); | |
if (isnan(event.temperature)) { | |
Serial.println("Error reading temperature!"); | |
} | |
else { | |
float newTemp = event.temperature; | |
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); | |
} | |
} | |
// Get humidity event and print its value. | |
dht.humidity().getEvent(&event); | |
if (isnan(event.relative_humidity)) { | |
Serial.println("Error reading humidity!"); | |
} | |
else { | |
float newHum = event.relative_humidity; | |
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