Created
November 18, 2016 11:27
Star
You must be signed in to star a gist
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
// CPU 80MHZ, FLASH 4M/1M | |
/* | |
*** Sample using esp-01, D16 is connected to RST | |
*** If DHT22 is powered by a gpio( VCC of DHT22 is connected to a gpio) and OUTPUT of DHT22 is connected to D2, boot will fail. | |
*** Power off ----> D2 is in LOW( == DHT22 is in LOW) ==> SDIO boot mode. | |
Temperature and humidity values are each read out the results of the last measurement. | |
For real-time data that need continuous read twice, we recommend repeatedly to read sensors, | |
and each read sensor interval is greater than 2 seconds to obtain accuratethe data. | |
--> read twice !!!! | |
--> sampling period : 2 sec | |
3.3V - 5.5V is needed, min is 3.3V | |
So if DHT22 is powered by battery(2 * AA), boost module is needed. | |
http://www.ebay.com/itm/2-in-1-DC-DC-Step-Down-Step-Up-Converter-1-8V-5V-3V-3-7V-to-3-3V-Power-Module-/271873956073 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PietteTech_DHT.h> | |
// ap setting | |
#include "config.h" | |
/* contents of congi.h should be | |
* const char* privateKey = "EMONCMSKEY"; | |
* //emoncoms | |
* const char* host = "emoncms.org"; | |
* const char* nodeId = "21"; | |
*/ | |
extern "C" { | |
#include "user_interface.h" | |
} | |
// to check battery voltage using internal adc | |
ADC_MODE(ADC_VCC); | |
// rtc | |
#define RTC_MAGIC 1234 | |
typedef struct _tagPoint { | |
uint32 magic; | |
uint32 salt; | |
uint32 wifi_err_cnt; | |
uint32 temp_err_cnt; | |
uint32 report_cnt; | |
} RTC_TEST; | |
RTC_TEST rtc_mem_test; | |
float t, h; | |
#include <DNSServer.h> | |
#include <ESP8266WebServer.h> | |
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager | |
// | |
int vdd; | |
WiFiClient wifiClient; | |
// system defines | |
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 | |
#define DHTPIN 2 // Digital pin for communications | |
#define REPORT_INTERVAL 300 // in sec | |
#define DHT_SMAPLING_INTERVAL 2100 // in msec | |
#define DHT_GND_PIN 0 // to control npn tr | |
unsigned long startMills, checkMillis; | |
bool bDHTstarted; | |
int acquirestatus; | |
int loopcount; | |
void ICACHE_RAM_ATTR dht_wrapper(); | |
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); | |
void goingtosleep() { | |
pinMode(DHTPIN, OUTPUT); | |
digitalWrite(DHTPIN, LOW); //- See more at: http://www.esp8266.com/viewtopic.php?p=43435#p43435 | |
digitalWrite(DHT_GND_PIN, HIGH); | |
delay(100); | |
system_rtc_mem_write(100, &rtc_mem_test, sizeof(rtc_mem_test)); | |
ESP.deepSleep((REPORT_INTERVAL * 1000 * 1000 ), WAKE_RF_DEFAULT); | |
yield(); | |
} | |
void dorestart() { | |
pinMode(DHTPIN, OUTPUT); | |
digitalWrite(DHTPIN, LOW); //- See more at: http://www.esp8266.com/viewtopic.php?p=43435#p43435 | |
digitalWrite(DHT_GND_PIN, HIGH); | |
delay(100); | |
system_rtc_mem_write(100, &rtc_mem_test, sizeof(rtc_mem_test)); | |
ESP.restart(); | |
yield(); | |
} | |
void rtc_check() { | |
// system_rtc_mem_read(64... not work, use > 64 | |
system_rtc_mem_read(100, &rtc_mem_test, sizeof(rtc_mem_test)); | |
if (rtc_mem_test.magic != RTC_MAGIC) { | |
rtc_mem_test.magic = RTC_MAGIC; | |
rtc_mem_test.salt = 0; | |
rtc_mem_test.wifi_err_cnt = 0; | |
rtc_mem_test.temp_err_cnt = 0; | |
rtc_mem_test.report_cnt = 0; | |
} | |
rtc_mem_test.salt++; | |
} | |
void ICACHE_RAM_ATTR dht_wrapper() { | |
DHT.isrCallback(); | |
} | |
void wifi_connect() { | |
WiFiManager wifiManager; | |
wifiManager.setTimeout(120); | |
if (!wifiManager.autoConnect("Cashula")) { | |
rtc_mem_test.wifi_err_cnt++; | |
goingtosleep(); | |
} | |
} | |
void sendData() { | |
Serial.println("connecting to "); | |
Serial.println(host); | |
WiFiClient emoClient; | |
const int httpPort = 80; | |
if (!emoClient.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String json = "{temperature:"; | |
json += t; | |
json += ",humidity:"; | |
json += h; | |
json += ",vcc:"; | |
json += vdd; | |
json += "}"; | |
String url = "/input/post.json?node="; | |
url += nodeId; | |
url += "&apikey="; | |
url += privateKey; | |
url += "&json="; | |
url += json; | |
Serial.println("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
emoClient.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
while (emoClient.available()) { | |
String line = emoClient.readStringUntil('\r'); | |
Serial.println(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(""); | |
Serial.println("Setup started"); | |
rtc_check(); | |
vdd = ESP.getVcc() * 0.96; | |
acquirestatus = loopcount = 0; | |
pinMode(DHT_GND_PIN, OUTPUT); | |
digitalWrite(DHT_GND_PIN, LOW); | |
Serial.println("wifi on"); | |
if (WiFi.status() != WL_CONNECTED) { | |
wifi_connect(); | |
} | |
Serial.println("wifi connected"); | |
bDHTstarted = false; | |
startMills = checkMillis = millis(); | |
} | |
void loop() { | |
if ( loopcount < 2 ) { | |
if (bDHTstarted) { | |
acquirestatus = DHT.acquiring(); | |
if (!acquirestatus) { | |
Serial.println("dht started"); | |
if (DHT.getStatus() != 0) { | |
rtc_mem_test.temp_err_cnt++; | |
dorestart(); | |
//goingtosleep(); | |
} | |
bDHTstarted = false; | |
loopcount++; | |
} | |
} | |
if ((millis() - checkMillis) > DHT_SMAPLING_INTERVAL ) { | |
checkMillis = millis(); | |
if (acquirestatus == 1) { | |
DHT.reset(); | |
} | |
if (!bDHTstarted) { | |
DHT.acquire(); | |
bDHTstarted = true; | |
Serial.println("starting dht"); | |
} | |
} | |
} else { | |
Serial.println("reporting -->"); | |
//printEdgeTiming(&DHT); | |
t = DHT.getCelsius(); | |
h = DHT.getHumidity(); | |
sendData(); | |
Serial.println("reporting done --> "); | |
Serial.println("going to sleep --> "); | |
goingtosleep(); | |
} | |
if ((millis() - startMills) > 25000) { | |
Serial.println("timed out"); | |
goingtosleep(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment