|
|
|
|
@ -7,8 +7,11 @@
|
|
|
|
|
#define DHT_TYPE DHT22
|
|
|
|
|
|
|
|
|
|
//WaterLevelSensor
|
|
|
|
|
#define sensorPower D2
|
|
|
|
|
#define sensorPin A0
|
|
|
|
|
#define waterlevel_pin D2
|
|
|
|
|
#define waterlevelread_pin A0
|
|
|
|
|
|
|
|
|
|
//moistureSensor
|
|
|
|
|
#define moisture_pin D3
|
|
|
|
|
|
|
|
|
|
//pump
|
|
|
|
|
#define pump D8
|
|
|
|
|
@ -17,12 +20,15 @@
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
@ -30,19 +36,25 @@ 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(sensorPower, OUTPUT);
|
|
|
|
|
digitalWrite(sensorPower, LOW);
|
|
|
|
|
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");
|
|
|
|
|
@ -64,7 +76,7 @@ void setup() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
void loop() {
|
|
|
|
|
//temperature sensor
|
|
|
|
|
float temperature = dht.readTemperature();
|
|
|
|
|
@ -85,8 +97,9 @@ void loop() {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -115,6 +128,17 @@ void loop() {
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -129,10 +153,10 @@ void loop() {
|
|
|
|
|
|
|
|
|
|
//reading water level
|
|
|
|
|
int readSensor() {
|
|
|
|
|
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
|
|
|
|
|
digitalWrite(waterlevel_pin, 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
|
|
|
|
|
val = analogRead(waterlevelread_pin); // Read the analog value form sensor
|
|
|
|
|
digitalWrite(waterlevel_pin, LOW); // Turn the sensor OFF
|
|
|
|
|
return val; // send current reading
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|