worked, without pump
parent
f251a48978
commit
36d813597d
@ -0,0 +1,164 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
//TemperaturSensor
|
||||
#include "DHT.h"
|
||||
#define DHT_TYPE DHT22
|
||||
|
||||
//WaterLevelSensor
|
||||
#define sensorPower D2
|
||||
#define sensorPin A0
|
||||
|
||||
// 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);
|
||||
pinMode(sensorPower, OUTPUT);
|
||||
digitalWrite(sensorPower, 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() {
|
||||
|
||||
float humidity = dht.readHumidity();
|
||||
float temperature = dht.readTemperature();
|
||||
Serial.print("Temperatur: ");
|
||||
Serial.print(temperature);
|
||||
Serial.println("°C");
|
||||
delay(1000);
|
||||
|
||||
Serial.print("Luftfeuchtigkeit: ");
|
||||
Serial.print(humidity);
|
||||
Serial.println("%");
|
||||
|
||||
int waterlevel = readSensor();
|
||||
Serial.print("Water level: ");
|
||||
Serial.println(waterlevel);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//This is a function used to get the reading
|
||||
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
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void startDeepSleep(int timer){
|
||||
Serial.println("Going to deep sleep...");
|
||||
ESP.deepSleep(timer * 1000000);
|
||||
yield();
|
||||
setup();
|
||||
}
|
||||
Loading…
Reference in New Issue