Created
August 26, 2016 18:34
-
-
Save teemow/cc867ad3962ea32d90a9c77561200e6f to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#include <ESP8266httpUpdate.h> | |
#include <PubSubClient.h> | |
#include <DHT.h> | |
#include <EEPROM.h> | |
const char *ssid = ""; | |
const char *pass = ""; | |
const char *room = ""; | |
const char *update_server = "192.168.15.1"; | |
const char *mqtt_server = "192.168.15.1"; | |
const char *version = "0.0.1"; | |
const char *device_type = "dht22"; | |
WiFiClient wclient; | |
PubSubClient client(wclient, mqtt_server); | |
#define DHTPIN D2 // what pin we're connected to | |
#define FORCE_DEEPSLEEP | |
// Uncomment whatever type you're using! | |
//#define DHTTYPE DHT11 // DHT 11 | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
//#define DHTTYPE DHT21 // DHT 21 (AM2301) | |
// Connect pin 1 (on the left) of the sensor to +5V | |
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 | |
// to 3.3V instead of 5V! | |
// Connect pin 2 of the sensor to whatever your DHTPIN is | |
// Connect pin 4 (on the right) of the sensor to GROUND | |
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor | |
// Initialize DHT sensor. | |
// Note that older versions of this library took an optional third parameter to | |
// tweak the timings for faster processors. This parameter is no longer needed | |
// as the current DHT reading algorithm adjusts itself to work on faster procs. | |
DHT dht(DHTPIN, DHTTYPE); | |
// the current address in the EEPROM (i.e. which byte | |
// we're going to write to next) | |
int addr = 0; | |
void connect(); | |
void setup(void) { | |
EEPROM.begin(512); | |
// Setup DHT22 | |
dht.begin(); | |
// Setup console | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println("Booting..."); | |
// Setup Wifi | |
WiFi.mode(WIFI_AP_STA); | |
WiFi.begin(ssid, pass); | |
connect(); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println("Ready!"); | |
t_httpUpdate_return ret = ESPhttpUpdate.update(update_server, 8266, "/update/", device_type + String("-") + room + String("-") + version); | |
switch (ret) { | |
case HTTP_UPDATE_FAILED: | |
Serial.println("[update] Update failed."); | |
break; | |
case HTTP_UPDATE_NO_UPDATES: | |
Serial.println("[update] Update no Update."); | |
break; | |
case HTTP_UPDATE_OK: | |
Serial.println("[update] Update ok."); // may not called we reboot the ESP | |
break; | |
} | |
} | |
/* | |
EEPROM Write | |
Stores values read from analog input 0 into the EEPROM. | |
These values will stay in the EEPROM when the board is | |
turned off and may be retrieved later by another sketch. | |
*/ | |
void writeEEPROM() { | |
// need to divide by 4 because analog inputs range from | |
// 0 to 1023 and each byte of the EEPROM can only hold a | |
// value from 0 to 255. | |
int val = analogRead(A0) / 4; | |
// write the value to the appropriate byte of the EEPROM. | |
// these values will remain there when the board is | |
// turned off. | |
EEPROM.write(addr, val); | |
// advance to the next address. there are 512 bytes in | |
// the EEPROM, so go back to 0 when we hit 512. | |
// save all changes to the flash. | |
addr = addr + 1; | |
if (addr == 512) | |
{ | |
addr = 0; | |
EEPROM.commit(); | |
} | |
delay(100); | |
} | |
String readTemperatureAndHumidity() { | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return ""; | |
} | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
String payload = "{\"Humidity\":"; | |
payload += h; | |
payload += ",\"Temperature\":"; | |
payload += t; | |
payload += ",\"HeatIndex\":"; | |
payload += hic; | |
payload += "}"; | |
return payload; | |
} | |
void loop() { | |
String payload; | |
connect(); | |
payload = readTemperatureAndHumidity(); | |
if (client.connected()) { | |
Serial.print("Payload: "); | |
Serial.println(payload); | |
if (payload != "") { | |
client.publish(device_type + String("/") + room, (char*) payload.c_str()); | |
} | |
client.publish(String("reset/") + room, ESP.getResetReason()); | |
} | |
client.loop(); | |
#ifdef FORCE_DEEPSLEEP | |
Serial.println("Force deepsleep 5min!"); | |
ESP.deepSleep(5 * 60 * 1000000); | |
delay(100); | |
#endif | |
} | |
void connect() { | |
while (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
WiFi.begin(ssid, pass); | |
Serial.println("WiFi failed, retrying."); | |
} | |
if (!client.connected()) { | |
if (client.connect(device_type + String("-") + room)) { | |
Serial.println("Connected to MQTT"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment