From bb69d68e744063651aae9a7a1d4c571a31f8db85 Mon Sep 17 00:00:00 2001 From: thunic Date: Sun, 26 Mar 2023 21:08:42 +0200 Subject: [PATCH] changes to post rest api and worker WorkerNode --- ServerNode/api.py | 13 ++++++----- WorkerNode/NodeMCWifi/NodeMCWifi.ino | 33 ++++++++++++++++------------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/ServerNode/api.py b/ServerNode/api.py index 78edaf0..21f675c 100644 --- a/ServerNode/api.py +++ b/ServerNode/api.py @@ -1,6 +1,7 @@ from flask import Flask, jsonify, request from DatabaseObject import sensordata from SQLHandler import SQLconnect +from datetime import datetime app = Flask(__name__) @@ -23,17 +24,17 @@ def get_sensordata(): -@app.route('/sensordata', methods=['POST']) +@app.post('/sensordata') def add_sensordata(): - timestamp = request.json['timestamp'] - moisture = request.json['moisture'] - humidity = request.json['humidity'] + if request.is_json: + sensordata = request.get_json() - newdata = sensordata(timestamp, moisture, humidity) + + now = datetime.now() sql = "INSERT INTO sensordata (timestamp, temperature, moisture, humidity) VALUES (%s, %s, %s, %s)" - mycursor.execute(sql, newdata) + mycursor.execute(sql, now, sensordata) mydb.commit() diff --git a/WorkerNode/NodeMCWifi/NodeMCWifi.ino b/WorkerNode/NodeMCWifi/NodeMCWifi.ino index d4651b9..a3a5232 100644 --- a/WorkerNode/NodeMCWifi/NodeMCWifi.ino +++ b/WorkerNode/NodeMCWifi/NodeMCWifi.ino @@ -2,6 +2,7 @@ #include #include #include +#include //Bibliotheken für die Temperatursensoren #include "DHT.h" @@ -19,7 +20,7 @@ DHT dht(DHT_PIN, DHT_TYPE); const char* ssid = "Andy"; const char* password = "AndyNicoChiara"; -const char* serverName = "http://192.168.178.154/sensordata"; +const char* serverName = "http://192.168.178.170/sensordata"; // Moisture sensor variables int sense_Pin = 0; // sensor input at Analog pin A0 @@ -28,7 +29,7 @@ int value = 0; //Sensor Pin an dVariable int pos = 0; -//Konstante für die Zeit in min +//Konstante für die ZEit const int timer = 30; void setup() { @@ -87,15 +88,26 @@ void loop() { // 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"); + //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); + //String httpRequestData = "moisture=" + String(moisture) + "&humidity="+ String(humidity) + "&temperature=" + String(temperature); // Send HTTP POST request - int httpResponseCode = http.POST(httpRequestData); + //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"); - //int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}"); + 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"); @@ -114,10 +126,3 @@ void loop() { ESP.deepSleep(timer * 1000000); yield(); } - - -void startDeepSleep(){ - Serial.println("Going to deep sleep..."); - ESP.deepSleep(5 * 1000000); - yield(); -}