Compare commits
No commits in common. 'f222776476d5db00ee80a6abc2498508caf9ffc6' and '0da2911e74ef054ca67d3a5db1280e27d8068b00' have entirely different histories.
f222776476
...
0da2911e74
@ -1,184 +0,0 @@
|
|||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <ESP8266HTTPClient.h>
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
//TemperaturSensor
|
|
||||||
#include "DHT.h"
|
|
||||||
#define DHT_TYPE DHT22
|
|
||||||
|
|
||||||
//WaterLevelSensor
|
|
||||||
#define sensorPower D2
|
|
||||||
#define sensorPin A0
|
|
||||||
|
|
||||||
//pump
|
|
||||||
#define pump D8
|
|
||||||
|
|
||||||
// Value for storing water level
|
|
||||||
int val = 0;
|
|
||||||
int test = 0;
|
|
||||||
|
|
||||||
const int DHT_PIN = D5;
|
|
||||||
DHT dht(DHT_PIN, DHT_TYPE);
|
|
||||||
|
|
||||||
const char* ssid = "Andy";
|
|
||||||
const char* password = "AndyNicoChiara";
|
|
||||||
|
|
||||||
const char* serverName = "http://192.168.178.45:8000/api/";
|
|
||||||
String hostname = "ESP8266NodeTemperature1";
|
|
||||||
|
|
||||||
|
|
||||||
//Konstante für die ZEit
|
|
||||||
const int timer = 30;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
dht.begin();
|
|
||||||
delay(1000);
|
|
||||||
|
|
||||||
//Waterlevel sensor
|
|
||||||
pinMode(sensorPower, OUTPUT);
|
|
||||||
digitalWrite(sensorPower, LOW);
|
|
||||||
//pump
|
|
||||||
pinMode(pump, OUTPUT);
|
|
||||||
digitalWrite(pump, LOW);
|
|
||||||
|
|
||||||
delay(2000);
|
|
||||||
WiFi.setHostname(hostname.c_str()); //define hostname
|
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
Serial.println("Connecting");
|
|
||||||
while(WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println("");
|
|
||||||
Serial.print("Connected to WiFi network with IP Address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
while(!Serial);
|
|
||||||
Serial.println("Waking up ...");
|
|
||||||
|
|
||||||
test = initiate(hostname);
|
|
||||||
if (test == 0){
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
//temperature sensor
|
|
||||||
float temperature = dht.readTemperature();
|
|
||||||
Serial.print("Temperatur: ");
|
|
||||||
Serial.print(temperature);
|
|
||||||
Serial.println("°C");
|
|
||||||
delay(1000);
|
|
||||||
|
|
||||||
//humidity sensor
|
|
||||||
float humidity = dht.readHumidity();
|
|
||||||
Serial.print("Luftfeuchtigkeit: ");
|
|
||||||
Serial.print(humidity);
|
|
||||||
Serial.println("%");
|
|
||||||
|
|
||||||
//waterlevel sensor
|
|
||||||
int waterlevel = readSensor();
|
|
||||||
Serial.print("Water level: ");
|
|
||||||
Serial.println(waterlevel);
|
|
||||||
|
|
||||||
//moisture sensor
|
|
||||||
|
|
||||||
|
|
||||||
//pump if moisture level is lower than a certain threshold
|
|
||||||
|
|
||||||
|
|
||||||
if(WiFi.status()== WL_CONNECTED){
|
|
||||||
//----------------------------------------------------------------
|
|
||||||
DynamicJsonDocument doc(2048);
|
|
||||||
// Add values in the document
|
|
||||||
doc["hostname"] = String(hostname);
|
|
||||||
doc["temperature"] = String(temperature);
|
|
||||||
doc["humidity"] = String(humidity);
|
|
||||||
|
|
||||||
String requestBody;
|
|
||||||
serializeJson(doc, requestBody);
|
|
||||||
Serial.println(requestBody);
|
|
||||||
|
|
||||||
postRequest(requestBody, "temperature/");
|
|
||||||
doc.clear();
|
|
||||||
//----------------------------------------------------------------
|
|
||||||
// Add values in the document
|
|
||||||
doc["hostname"] = String(hostname);
|
|
||||||
doc["waterlevel"] = String(waterlevel);
|
|
||||||
|
|
||||||
requestBody = "";
|
|
||||||
serializeJson(doc, requestBody);
|
|
||||||
Serial.println(requestBody);
|
|
||||||
postRequest(requestBody, "waterlevel/");
|
|
||||||
doc.clear();
|
|
||||||
|
|
||||||
Serial.println("closing connection");
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
Serial.println("WiFi Disconnected");
|
|
||||||
}
|
|
||||||
|
|
||||||
startDeepSleep(timer);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//reading water level
|
|
||||||
int readSensor() {
|
|
||||||
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
|
|
||||||
delay(10); // wait 10 milliseconds
|
|
||||||
val = analogRead(sensorPin); // Read the analog value form sensor
|
|
||||||
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
|
|
||||||
return val; // send current reading
|
|
||||||
}
|
|
||||||
|
|
||||||
//initial post request
|
|
||||||
int initiate(String hostname){
|
|
||||||
|
|
||||||
DynamicJsonDocument doc(2048);
|
|
||||||
// Add values in the document
|
|
||||||
doc["hostname"] = String(hostname);
|
|
||||||
|
|
||||||
String requestBody;
|
|
||||||
serializeJson(doc, requestBody);
|
|
||||||
|
|
||||||
postRequest(requestBody, "client/");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//post request handler
|
|
||||||
int postRequest(String requestBody, String api) {
|
|
||||||
String urlName;
|
|
||||||
urlName = serverName + api;
|
|
||||||
WiFiClient client;
|
|
||||||
HTTPClient http;
|
|
||||||
http.begin(client, urlName);
|
|
||||||
http.addHeader("Content-Type", "application/json");
|
|
||||||
int httpResponseCode = http.POST(requestBody);
|
|
||||||
if (httpResponseCode>0) {
|
|
||||||
Serial.print("HTTP Response code: ");
|
|
||||||
Serial.println(httpResponseCode);
|
|
||||||
String payload = http.getString();
|
|
||||||
Serial.println(payload);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.print("Error code: ");
|
|
||||||
Serial.println(httpResponseCode);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
http.end();
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//deepsleep function for battery saving
|
|
||||||
void startDeepSleep(int timer){
|
|
||||||
Serial.println("Going to deep sleep...");
|
|
||||||
ESP.deepSleep(timer * 1000000);
|
|
||||||
yield();
|
|
||||||
setup();
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue