From aff05088ed6c61b5e1943f77a557f269d1fe8e8b Mon Sep 17 00:00:00 2001 From: thunic Date: Fri, 24 Mar 2023 13:57:53 +0100 Subject: [PATCH] backend should work next WorkerNode --- SQLHandler/SQLconnect.py | 24 ++--- .../__pycache__/SQLconnect.cpython-310.pyc | Bin 842 -> 993 bytes WorkerNode/NodeMCWifi/NodeMCWifi.ino | 84 ++++++++++++++++++ requirements.txt | Bin 236 -> 336 bytes 4 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 WorkerNode/NodeMCWifi/NodeMCWifi.ino diff --git a/SQLHandler/SQLconnect.py b/SQLHandler/SQLconnect.py index a9d1a0e..5d68e38 100644 --- a/SQLHandler/SQLconnect.py +++ b/SQLHandler/SQLconnect.py @@ -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"], - autocommit=True - ) + 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 \ No newline at end of file diff --git a/SQLHandler/__pycache__/SQLconnect.cpython-310.pyc b/SQLHandler/__pycache__/SQLconnect.cpython-310.pyc index 4f3588d92a21081d0338bbd45bcbc8efc37b1c1c..3d5ccd1a9e2dd5a04752f4f1609c6e321c208e47 100644 GIT binary patch delta 463 zcmYLFO-lnY5S__pKU~}5=RvKqA__fv(1R55^QZ`dViB}9t*o>g$wsurs^D30=}AP; zqkqMp5WINwFDMqX+lm?Hy9d?fAkaMH<*L7oNT?j6` z5)XoMN&3ODC%Ly#6n=4S#oMYDWrd5{g7=B~dYd_0*|oUFj+EVPQI;kZ|5%yHNy=VqCW_MMfpY(*aVJn%eg)Cr+93z?6M6=*C|cbh=zLSR?lv{X*M#@Si5ycoYC?96dAcEo|6t{ z0?M-ku`>`CI{=9ihAhS`rV{2F21%ecNd_p33CLnuzzXFtOEN5EgtA$H>R40QdRc23 zYZ!wWG}$L-F#d|uWV^+hTUlI~6UCXFpO=@KT#{dOiyg|m#Rg=gWu`}QrKBboRTh+F z=I6oL-l>(EOi{cL6{SUqAjw-?P@O>bN`@lt$uF7WO$30hWn<%Fdh}IVxL^ctZc^#R0CF6#0_F02p%B!7Kcr4eoARhsvRSc VSIh?_I2bt?d02p$QRW{H8vsUoSH1uM diff --git a/WorkerNode/NodeMCWifi/NodeMCWifi.ino b/WorkerNode/NodeMCWifi/NodeMCWifi.ino new file mode 100644 index 0000000..e6ef62d --- /dev/null +++ b/WorkerNode/NodeMCWifi/NodeMCWifi.ino @@ -0,0 +1,84 @@ +#include +#include +#include + +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(); + } +} diff --git a/requirements.txt b/requirements.txt index 6ffd8946e6bc202e47dc9ea8bb238856c9694a91..708dd37a9d5eaa9d09471732fafa386690220ff9 100644 GIT binary patch delta 105 zcmaFEc!6ocsrX!mN`_*FLWUd$U4~?ae1<$AOa-z^fb1fmSOHMJgdqbgW6NO6V8Nir nU;xC%3`PvR3|v6@BA^^dbrM4^oDW5smXebN&p$}1O5O2