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.
129 lines
3.3 KiB
C++
129 lines
3.3 KiB
C++
//Bibliotheken der API Anbindung
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <WiFiClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
//Bibliotheken für die Temperatursensoren
|
|
#include "DHT.h"
|
|
#define DHT_TYPE DHT22
|
|
|
|
//Bibliotheken für die Servosteuerung
|
|
#include <Servo.h>
|
|
|
|
|
|
Servo myservo;
|
|
|
|
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.170/sensordata";
|
|
|
|
// Moisture sensor variables
|
|
int sense_Pin = 0; // sensor input at Analog pin A0
|
|
int value = 0;
|
|
|
|
//Sensor Pin an dVariable
|
|
int pos = 0;
|
|
|
|
//Konstante für die ZEit
|
|
const int timer = 30;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
dht.begin();
|
|
delay(2000);
|
|
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());
|
|
|
|
myservo.attach(D4);
|
|
|
|
while(!Serial);
|
|
Serial.println("Waking up ...");
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
Serial.print("MOISTURE LEVEL : ");
|
|
float moisture = analogRead(sense_Pin);
|
|
/*value= value/10;*/
|
|
Serial.println(moisture);
|
|
delay(1000);
|
|
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("%");
|
|
/*
|
|
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
|
|
delay(15); // wartet 15 Millisekunden
|
|
}
|
|
for(pos = 180; pos>=1; pos-=1) { // und das gleiche zurück
|
|
myservo.write(pos);
|
|
delay(15);
|
|
}
|
|
*/
|
|
|
|
if(WiFi.status()== WL_CONNECTED){
|
|
WiFiClient client;
|
|
HTTPClient http;
|
|
|
|
// Your Domain name with URL path or IP address with path
|
|
http.begin(client, serverName);
|
|
// Specify content-type header
|
|
//http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
// Data to send with HTTP POST
|
|
//String httpRequestData = "moisture=" + String(moisture) + "&humidity="+ String(humidity) + "&temperature=" + String(temperature);
|
|
// Send HTTP POST request
|
|
//int httpResponseCode = http.POST(httpRequestData);
|
|
|
|
// If you need an HTTP request with a content type: application/json, use the following:
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
StaticJsonDocument<200> doc;
|
|
// Add values in the document
|
|
|
|
doc["temperature"] = temperature;
|
|
doc["moisture"] = moisture;
|
|
doc["humidity"] = humidity;
|
|
|
|
String requestBody;
|
|
serializeJson(doc, requestBody);
|
|
|
|
int httpResponseCode = http.POST(requestBody);
|
|
|
|
// If you need an HTTP request with a content type: text/plain
|
|
//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");
|
|
}
|
|
Serial.println("Going to deep sleep...");
|
|
ESP.deepSleep(timer * 1000000);
|
|
yield();
|
|
}
|