backend should work next WorkerNode

main
thunic 3 years ago
parent b10cf7e2b9
commit aff05088ed

@ -1,10 +1,10 @@
# This file contains the function to connect to a secified database.
import json
import mysql.connector
import mariadb
import sys
# Open the config file and readout the data
with open('C:\Users\Nico\Desktop\Gitea\PlantSensorAPI\SQLHandler\config.json', 'r') as file:
with open(r'C:\Users\Nico\Desktop\Gitea\PlantSensorAPI\SQLHandler\config.json', 'r') as file:
data = file.read()
config = json.loads(data)
@ -16,11 +16,15 @@ def createConnection(configuration):
:param configuration: This specifies which configuration should be used
:return: It returns the database connection
"""
connection = mysql.connector.connect(
host=config[configuration]["host"],
user=config[configuration]["user"],
password=decryption.decryptKey(config[configuration]["passwd"]),
database=config[configuration]["dbname"],
try:
connection = mariadb.connect(
host=config["host"],
user=config["user"],
password=config["passwd"],
database=config["dbname"],
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
return connection

@ -0,0 +1,84 @@
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "Andy";
const char* password = "AndyNicoChiara";
const char* serverName = "http://192.168.178.154/sensordata";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
// Moisture sensor variables
int sense_Pin = 0; // sensor input at Analog pin A0
int value = 0;
void setup() {
Serial.begin(9600);
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());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
Serial.print("MOISTURE LEVEL : ");
value= analogRead(sense_Pin);
/*value= value/10;*/
Serial.println(value);
delay(1000);
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
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=" + value;
// 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");
//int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");
// 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");
}
lastTime = millis();
}
}

Binary file not shown.
Loading…
Cancel
Save