added deep sleep mode to save battery life

main
thunic 3 years ago
parent 88bed674bb
commit cb07f28c3d

@ -21,14 +21,6 @@ const char* password = "AndyNicoChiara";
const char* serverName = "http://192.168.178.154/sensordata"; const char* serverName = "http://192.168.178.154/sensordata";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
// Moisture sensor variables // Moisture sensor variables
int sense_Pin = 0; // sensor input at Analog pin A0 int sense_Pin = 0; // sensor input at Analog pin A0
int value = 0; int value = 0;
@ -36,6 +28,9 @@ int value = 0;
//Sensor Pin an dVariable //Sensor Pin an dVariable
int pos = 0; int pos = 0;
//Konstante für die Zeit in min
const int timer = 30;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
dht.begin(); dht.begin();
@ -50,9 +45,10 @@ void setup() {
Serial.print("Connected to WiFi network with IP Address: "); Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
myservo.attach(D4); myservo.attach(D4);
while(!Serial);
Serial.println("Waking up ...");
} }
@ -67,12 +63,13 @@ void loop() {
float temperature = dht.readTemperature(); float temperature = dht.readTemperature();
Serial.print("Temperatur: "); Serial.print("Temperatur: ");
Serial.print(temperature); Serial.print(temperature);
Serial.println("%"); Serial.println("°C");
delay(1000); delay(1000);
Serial.print("°C, Luftfeuchtigkeit: "); Serial.print("Luftfeuchtigkeit: ");
Serial.print(humidity); Serial.print(humidity);
Serial.println("%");
/*
for(pos = 0; pos < 180; pos += 1) { // von 0 bis 180 Grad, in Schritten von einem Grad for(pos = 0; pos < 180; pos += 1) { // von 0 bis 180 Grad, in Schritten von einem Grad
myservo.write(pos); // sagt dem Servomotor, in welche Position sich drehen soll myservo.write(pos); // sagt dem Servomotor, in welche Position sich drehen soll
delay(15); // wartet 15 Millisekunden delay(15); // wartet 15 Millisekunden
@ -81,42 +78,46 @@ void loop() {
myservo.write(pos); myservo.write(pos);
delay(15); delay(15);
} }
*/
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
/* // Your Domain name with URL path or IP address with path
if ((millis() - lastTime) > timerDelay) { http.begin(client, serverName);
//Check WiFi connection status // Specify content-type header
if(WiFi.status()== WL_CONNECTED){ http.addHeader("Content-Type", "application/x-www-form-urlencoded");
WiFiClient client; // Data to send with HTTP POST
HTTPClient http; String httpRequestData = "moisture=" + String(moisture) + "&humidity="+ String(humidity) + "&temperature=" + String(temperature);
// Send HTTP POST request
// Your Domain name with URL path or IP address with path int httpResponseCode = http.POST(httpRequestData);
http.begin(client, serverName);
// If you need an HTTP request with a content type: application/json, use the following:
// Specify content-type header //http.addHeader("Content-Type", "application/json");
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");
// Data to send with HTTP POST
String httpRequestData = "moisture=" + String(moisture) + "&humidity="+ String(humidity) + "&temperature=" + String(temperature); // If you need an HTTP request with a content type: text/plain
// Send HTTP POST request //http.addHeader("Content-Type", "text/plain");
int httpResponseCode = http.POST(httpRequestData); //int httpResponseCode = http.POST("Hello, World!");
// If you need an HTTP request with a content type: application/json, use the following: Serial.print("HTTP Response code: ");
//http.addHeader("Content-Type", "application/json"); Serial.println(httpResponseCode);
//int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");
// Free resources
// If you need an HTTP request with a content type: text/plain http.end();
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
} }
*/ else {
Serial.println("WiFi Disconnected");
}
Serial.println("Going to deep sleep...");
ESP.deepSleep(timer * 1000000);
yield();
}
void startDeepSleep(){
Serial.println("Going to deep sleep...");
ESP.deepSleep(5 * 1000000);
yield();
} }

Loading…
Cancel
Save