database fixed and added
This commit is contained in:
parent
a6259e5392
commit
56f9065fc7
Binary file not shown.
23
software/backend/createdatabase_and_test.py
Normal file
23
software/backend/createdatabase_and_test.py
Normal 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)
|
@ -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)
|
||||
|
@ -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()
|
@ -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:
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 = [
|
||||
|
Loading…
x
Reference in New Issue
Block a user