Created
September 1, 2016 00:29
-
-
Save kuc-arc-f/6ac62375066d4fc541ae56ee80138515 to your computer and use it in GitHub Desktop.
esp8266, Deep Sleep+ DHT11
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
/* | |
esp8266_sleepDHT_v6.ino | |
DHT11+ esp8266 (Deep Sleep) | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <DHT.h> | |
extern "C" { | |
#include <user_interface.h> | |
} | |
#define DHTPIN 4 // what digital pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = "api.thingspeak.com"; | |
String mAPI_KEY=""; | |
static uint32_t mTimerTmp; | |
static uint32_t mTimerPost; | |
float mTemp=0; | |
float mHum=0; | |
// | |
void proc_http(String sTemp, String sHum){ | |
//Serial.print("connecting to "); | |
//Serial.println(host); | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String url = "/update?key="+ mAPI_KEY + "&field1="+ sTemp +"&field2=" + sHum; | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
int iSt=0; | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
} | |
// | |
void read_dhtSensor(){ | |
Serial.print("millis.Sensor.Start: "); | |
Serial.println(millis() ); | |
int iMax=10; | |
int iCtRead=0; | |
float fTempTotal=0; | |
float fHumTotal=0; | |
for(int i=0;i< iMax; i++){ | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
if (isnan(h) || isnan(t) ) { | |
Serial.println("Failed to read from DHT sensor!"); | |
}else{ | |
fTempTotal += t; | |
fHumTotal += h; | |
iCtRead++; | |
} | |
delay(100); | |
} | |
Serial.print("millis.Sensor.End: "); | |
Serial.println(millis() ); | |
mTemp= fTempTotal / iCtRead; | |
mHum= fHumTotal / iCtRead; | |
Serial.print("iCtRead="); | |
Serial.print(iCtRead ); | |
Serial.print(", fTempTotal="); | |
Serial.print(fTempTotal ); | |
Serial.print(", fHumTotal="); | |
Serial.println(fHumTotal ); | |
} | |
// | |
void setup() { | |
//pinMode(mLedPin, OUTPUT); | |
Serial.begin( 9600 ); | |
Serial.println("#Start-setup"); | |
Serial.print("millis.Start: "); | |
Serial.println(millis() ); | |
dht.begin(); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("millis.Connected: "); | |
Serial.println(millis() ); | |
// Serial.println("IP address: "); | |
// Serial.println(WiFi.localIP()); | |
} | |
// | |
void loop() { | |
delay( 100 ); | |
Serial.print("millis.Loop: "); | |
Serial.println(millis() ); | |
if (millis() > 3000 ){ | |
//read-DHT | |
mTemp= 0; | |
mHum= 0; | |
read_dhtSensor(); | |
String sTmp=String(mTemp); | |
String sHum=String(mHum); | |
Serial.print("sTmp="); | |
Serial.print(sTmp); | |
Serial.print(", Hum="); | |
Serial.println(sHum); | |
proc_http(sTmp , sHum); | |
Serial.print("millis.SleepStart: "); | |
Serial.println(millis() ); | |
//ESP.deepSleep( 30 * 1000 * 1000); | |
ESP.deepSleep( 600 * 1000 * 1000); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment