database fixed and added

This commit is contained in:
caliskan 2023-05-06 20:52:18 +02:00
parent a6259e5392
commit 56f9065fc7
7 changed files with 90 additions and 30 deletions

Binary file not shown.

View File

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

View File

@ -1,15 +1,36 @@
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
from software.defines import MQTT_BROKER_LOCAL
from random import randrange, uniform from random import randrange, uniform
import time 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) client.connect(mqttBroker)
while True: plantdata = {
client.publish("ROBOT/ACTION/DRIVE") "AirTemperature": 20.4,
print("Just published to topic TEMPERATURE") "AirHumidity": 7.0,
time.sleep(0.5) "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)

View File

@ -1,18 +1,28 @@
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import time import time
from software.defines import Topics
def on_message(client, userdata, message): 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") def on_connect(client, userdata, flags, rc):
client.connect(mqttBroker) 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.loop_start()
client.on_message = on_message
client.subscribe("TEMPERATURE") client.subscribe(Topics["BACKEND_DATA_SENSORDATA"])
client.on_message=on_message client.subscribe("Bilal")
time.sleep(30) time.sleep(30)
client.loop_stop() client.loop_stop()

View File

@ -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.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
_client.subscribe(Topics['BACKEND_ACTION_GETALLDATA']) _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 # END TOPIC SUBSCRIPTIONS
else: else:

View File

@ -19,7 +19,8 @@ class PlantDataBase:
def create_table(self): def create_table(self):
table_config = "CREATE TABLE IF NOT EXISTS plants " \ table_config = "CREATE TABLE IF NOT EXISTS plants " \
"(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \ "(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \
" gps TEXT)" " gps TEXT," \
"plantName TEXT)"
self.cur.execute(table_config) self.cur.execute(table_config)
table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \ table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \
@ -35,8 +36,8 @@ class PlantDataBase:
" REFERENCES plants (plant_ID) )" " REFERENCES plants (plant_ID) )"
self.cur.execute(table_config) self.cur.execute(table_config)
def insert_plant(self, gps: str, plant_type: str): def insert_plant(self, _gps: str, plantname):
self.cur.execute(f"INSERT INTO plants (gps, plant_type) VALUES ({gps}, {plant_type})") self.cur.execute("INSERT INTO plants (gps, plantName) VALUES (?,?)", (_gps, plantname))
self.conn.commit() self.conn.commit()
def insert_measurement_data(self, plant_id, 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") self.cur.execute(f"SELECT * FROM measurement_values where plant_ID = {plant_id} ORDER BY Timestamp DESC LIMIT 1")
data = self.cur.fetchone() 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 = { json_file = {
"measurement_id": data[0], "MeasurementID": data[0],
"plant_id": data[2], "PlantID": data[2],
"timestamp": data[1], "Timestamp": data[1],
"sensordata_temp": data[3], "AirTemperature": data[3],
"sensordata_humidity": data[4], "AirHumidity": data[4],
"sensordata_soil_moisture": data[5], "SoilMoisture": data[5],
"pest_infestation": data[6], "Brightness": data[7],
"light_intensity": data[7] "PlantName": name
} }
return json_file return json_file

View File

@ -4,7 +4,7 @@ created by caliskan at 19.04.2023
contains all constants for the backend architecture of the smart garden project 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" MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io"
RASPI_CLIENT_ID = "smart_farming_raspi" RASPI_CLIENT_ID = "smart_farming_raspi"
BACKEND_CLIENT_ID = "smart_farming_server" BACKEND_CLIENT_ID = "smart_farming_server"
@ -78,7 +78,8 @@ PLANTDATA = {
"Brightness": 0, "Brightness": 0,
"PlantID": 0, "PlantID": 0,
"Timestamp": "", "Timestamp": "",
"MeasurementID": 0 "MeasurementID": 0,
"PlantName": ""
} }
ALLPLANTDATA = [ ALLPLANTDATA = [