changes to post rest api and worker WorkerNode

main
thunic 3 years ago
parent 955f5d8127
commit bb69d68e74

@ -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()

@ -2,6 +2,7 @@
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
//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();
}

Loading…
Cancel
Save