From 281585a408217a1572f793c5b271ce3a9c7166c3 Mon Sep 17 00:00:00 2001 From: thunic Date: Thu, 30 Mar 2023 21:02:48 +0200 Subject: [PATCH] WorkerNode should work and api fixed --- ServerNode/api.py | 7 ++++++- WorkerNode/NodeMCWifi/NodeMCWifi.ino | 31 +++++++++------------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/ServerNode/api.py b/ServerNode/api.py index 21f675c..2e39e63 100644 --- a/ServerNode/api.py +++ b/ServerNode/api.py @@ -2,6 +2,7 @@ from flask import Flask, jsonify, request from DatabaseObject import sensordata from SQLHandler import SQLconnect from datetime import datetime +import json app = Flask(__name__) @@ -32,9 +33,13 @@ def add_sensordata(): now = datetime.now() + timestamp = {"timestamp":now} + + sensordata.update(timestamp) + sql = "INSERT INTO sensordata (timestamp, temperature, moisture, humidity) VALUES (%s, %s, %s, %s)" - mycursor.execute(sql, now, sensordata) + mycursor.execute(sql, sensordata) mydb.commit() diff --git a/WorkerNode/NodeMCWifi/NodeMCWifi.ino b/WorkerNode/NodeMCWifi/NodeMCWifi.ino index 6e77211..9c40812 100644 --- a/WorkerNode/NodeMCWifi/NodeMCWifi.ino +++ b/WorkerNode/NodeMCWifi/NodeMCWifi.ino @@ -1,7 +1,5 @@ -//Bibliotheken der API Anbindung #include #include -#include #include //Bibliotheken für die Temperatursensoren @@ -22,6 +20,7 @@ const char* password = "AndyNicoChiara"; const char* serverName = "http://192.168.178.170:3564/sensordata"; + // Moisture sensor variables int sense_Pin = 0; // sensor input at Analog pin A0 int value = 0; @@ -32,6 +31,9 @@ int pos = 0; //Konstante für die ZEit const int timer = 30; + + + void setup() { Serial.begin(9600); dht.begin(); @@ -96,29 +98,16 @@ void loop() { WiFiClient client; HTTPClient http; - - - // Your Domain name with URL path or IP address with path - http.begin(client, serverName); - // If you need an HTTP request with a content type: application/json, use the following: - //http.addHeader("Content-Type", "application/json"); - - + http.begin(client, serverName); + http.addHeader("Content-Type", "application/json"); 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); - - // Read response - Serial.print(http.getString()); - - // Free resources http.end(); - } + + Serial.println("closing connection"); + } + else { Serial.println("WiFi Disconnected"); }