diff --git a/software/backend/backend_database.db b/software/backend/backend_database.db index 665a62c..cb236c6 100644 Binary files a/software/backend/backend_database.db and b/software/backend/backend_database.db differ diff --git a/software/backend/createdatabase_and_test.py b/software/backend/createdatabase_and_test.py new file mode 100644 index 0000000..454c505 --- /dev/null +++ b/software/backend/createdatabase_and_test.py @@ -0,0 +1,23 @@ +import random + +from plantdatabase import PlantDataBase + +mydatabase = PlantDataBase() +mydatabase.create_table() + +for i in range(1,6): + mydatabase.insert_plant(_gps='gps', plantname=f"Pflanze{i}") + +for i in range(1,7): + plant_id = i + temp = random.random() + humidity = random.random() + soil_moisture = random.random() + pest_infestation = 0 + light_intensity = random.random() + mydatabase.insert_measurement_data(plant_id=plant_id, + sensordata_temp=temp, + sensordata_humidity=humidity, + sensordata_soil_moisture=soil_moisture, + pest_infestation=pest_infestation, + light_intensity=light_intensity) diff --git a/software/backend/dev_test_examples/mqtt_publisher.py b/software/backend/dev_test_examples/mqtt_publisher.py index 47f00da..e154265 100644 --- a/software/backend/dev_test_examples/mqtt_publisher.py +++ b/software/backend/dev_test_examples/mqtt_publisher.py @@ -1,15 +1,36 @@ - import paho.mqtt.client as mqtt +from software.defines import MQTT_BROKER_LOCAL from random import randrange, uniform import time +import json +from software.defines import Topics, PLANTDATA +mqttBroker = "mqtt.eclipseprojects.io" -mqttBroker ="mqtt.eclipseprojects.io" -client = mqtt.Client("Temperature_Inside") +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected") + else: + print("Connection failed") + + +client = mqtt.Client() +client.on_connect = on_connect + client.connect(mqttBroker) -while True: - client.publish("ROBOT/ACTION/DRIVE") - print("Just published to topic TEMPERATURE") - time.sleep(0.5) +plantdata = { + "AirTemperature": 20.4, + "AirHumidity": 7.0, + "SoilMoisture": 5.0, + "Brightness": 39, + "PlantID": 2, + "Timestamp": "hallo", + "MeasurementID": 187 +} +print(type(PLANTDATA)) +while True: + client.publish(Topics["BACKEND_DATA_SENSORDATA"], json.dumps(plantdata)) + print(json.dumps(plantdata)) + time.sleep(2) diff --git a/software/backend/dev_test_examples/mqtt_subscriber.py b/software/backend/dev_test_examples/mqtt_subscriber.py index 3eed0c6..68b152b 100644 --- a/software/backend/dev_test_examples/mqtt_subscriber.py +++ b/software/backend/dev_test_examples/mqtt_subscriber.py @@ -1,18 +1,28 @@ import paho.mqtt.client as mqtt import time +from software.defines import Topics + def on_message(client, userdata, message): - print("received message: " ,str(message.payload.decode("utf-8"))) + print("received message: ", str(message.payload.decode("utf-8"))) -mqttBroker ="mqtt.eclipseprojects.io" -client = mqtt.Client("Smartphone") -client.connect(mqttBroker) +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected") + else: + print("Connection failed") + + +mqttBroker = "192.168.0.199" + +client = mqtt.Client() +client.connect(mqttBroker, 1883) client.loop_start() +client.on_message = on_message -client.subscribe("TEMPERATURE") -client.on_message=on_message - +client.subscribe(Topics["BACKEND_DATA_SENSORDATA"]) +client.subscribe("Bilal") time.sleep(30) -client.loop_stop() \ No newline at end of file +client.loop_stop() diff --git a/software/backend/main.py b/software/backend/main.py index 41e7d02..63bbb2f 100644 --- a/software/backend/main.py +++ b/software/backend/main.py @@ -16,7 +16,7 @@ import data_functions # inits mydatabase = PlantDataBase() mydatabase.create_table() -order_handler = [] # will contain UUIDS with Order IDs +order_handler = [] # will contain UUIDS with Order IDs def on_connect(_client: mqtt.Client, _userdata, _flags, _rc): @@ -60,7 +60,7 @@ def on_connect(_client: mqtt.Client, _userdata, _flags, _rc): _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery) _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA']) - _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata) + _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], lambda client, userdata, message: data_functions.action_getalldata(client, userdata, message, mydatabase)) # END TOPIC SUBSCRIPTIONS else: diff --git a/software/backend/plantdatabase.py b/software/backend/plantdatabase.py index 8160e1b..b3ee7c4 100644 --- a/software/backend/plantdatabase.py +++ b/software/backend/plantdatabase.py @@ -19,7 +19,8 @@ class PlantDataBase: def create_table(self): table_config = "CREATE TABLE IF NOT EXISTS plants " \ "(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \ - " gps TEXT)" + " gps TEXT," \ + "plantName TEXT)" self.cur.execute(table_config) table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \ @@ -35,8 +36,8 @@ class PlantDataBase: " REFERENCES plants (plant_ID) )" self.cur.execute(table_config) - def insert_plant(self, gps: str, plant_type: str): - self.cur.execute(f"INSERT INTO plants (gps, plant_type) VALUES ({gps}, {plant_type})") + def insert_plant(self, _gps: str, plantname): + self.cur.execute("INSERT INTO plants (gps, plantName) VALUES (?,?)", (_gps, plantname)) self.conn.commit() def insert_measurement_data(self, plant_id, @@ -59,15 +60,19 @@ class PlantDataBase: """ self.cur.execute(f"SELECT * FROM measurement_values where plant_ID = {plant_id} ORDER BY Timestamp DESC LIMIT 1") data = self.cur.fetchone() + self.cur.execute(f"SELECT plantName FROM plants where plant_ID = {plant_id}") + name = self.cur.fetchone() + print(data) + print(name[0]) json_file = { - "measurement_id": data[0], - "plant_id": data[2], - "timestamp": data[1], - "sensordata_temp": data[3], - "sensordata_humidity": data[4], - "sensordata_soil_moisture": data[5], - "pest_infestation": data[6], - "light_intensity": data[7] + "MeasurementID": data[0], + "PlantID": data[2], + "Timestamp": data[1], + "AirTemperature": data[3], + "AirHumidity": data[4], + "SoilMoisture": data[5], + "Brightness": data[7], + "PlantName": name } return json_file diff --git a/software/defines.py b/software/defines.py index 9eef7ae..682e6db 100644 --- a/software/defines.py +++ b/software/defines.py @@ -4,7 +4,7 @@ created by caliskan at 19.04.2023 contains all constants for the backend architecture of the smart garden project """ -MQTT_BROKER_LOCAL = "lorem ipsum" +MQTT_BROKER_LOCAL = "192.168.0.199" MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" RASPI_CLIENT_ID = "smart_farming_raspi" BACKEND_CLIENT_ID = "smart_farming_server" @@ -78,7 +78,8 @@ PLANTDATA = { "Brightness": 0, "PlantID": 0, "Timestamp": "", - "MeasurementID": 0 + "MeasurementID": 0, + "PlantName": "" } ALLPLANTDATA = [