You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
4.9 KiB
C++
208 lines
4.9 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
//TemperaturSensor
|
|
#include "DHT.h"
|
|
#define DHT_TYPE DHT22
|
|
|
|
//WaterLevelSensor
|
|
#define waterlevel_pin D2
|
|
#define waterlevelread_pin A0
|
|
|
|
//moistureSensor
|
|
#define moisture_pin D3
|
|
|
|
//pump
|
|
#define pump D8
|
|
|
|
// Value for storing water level
|
|
int val = 0;
|
|
int test = 0;
|
|
|
|
//Temperature and humidity sensor
|
|
const int DHT_PIN = D5;
|
|
DHT dht(DHT_PIN, DHT_TYPE);
|
|
|
|
//Wlan configuration
|
|
const char* ssid = "Andy";
|
|
const char* password = "AndyNicoChiara";
|
|
|
|
//api and host configuration
|
|
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);
|
|
|
|
//temperature and humidity sensor
|
|
dht.begin();
|
|
delay(1000);
|
|
|
|
//Waterlevel sensor
|
|
pinMode(waterlevel_pin, OUTPUT);
|
|
digitalWrite(waterlevel_pin, LOW);
|
|
//pump
|
|
pinMode(pump, OUTPUT);
|
|
digitalWrite(pump, LOW);
|
|
|
|
delay(2000);
|
|
|
|
//Wifi setup
|
|
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
|
|
float moisture = analogRead(moisture_pin);
|
|
Serial.print("MOISTURE LEVEL : ");
|
|
Serial.println(moisture);
|
|
//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();
|
|
|
|
//----------------------------------------------------------------
|
|
// Add values in the document
|
|
doc["hostname"] = String(hostname);
|
|
doc["moisture"] = String(moisture);
|
|
|
|
requestBody = "";
|
|
serializeJson(doc, requestBody);
|
|
Serial.println(requestBody);
|
|
postRequest(requestBody, "moisture/");
|
|
doc.clear();
|
|
|
|
Serial.println("closing connection");
|
|
}
|
|
|
|
else {
|
|
Serial.println("WiFi Disconnected");
|
|
}
|
|
|
|
startDeepSleep(timer);
|
|
|
|
}
|
|
|
|
|
|
//reading water level
|
|
int readSensor() {
|
|
digitalWrite(waterlevel_pin, HIGH); // Turn the sensor ON
|
|
delay(10); // wait 10 milliseconds
|
|
val = analogRead(waterlevelread_pin); // Read the analog value form sensor
|
|
digitalWrite(waterlevel_pin, 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();
|
|
} |