Browse Source

database fixed and added

master
caliskan 1 year ago
parent
commit
56f9065fc7

BIN
software/backend/backend_database.db View File


+ 23
- 0
software/backend/createdatabase_and_test.py View File

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)

+ 28
- 7
software/backend/dev_test_examples/mqtt_publisher.py View File


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"


def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected")
else:
print("Connection failed")


mqttBroker ="mqtt.eclipseprojects.io"


client = mqtt.Client("Temperature_Inside")
client = mqtt.Client()
client.on_connect = on_connect

client.connect(mqttBroker) 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)

+ 18
- 8
software/backend/dev_test_examples/mqtt_subscriber.py View File

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")
client.connect(mqttBroker)
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected")
else:
print("Connection failed")


client.loop_start()


client.subscribe("TEMPERATURE")
client.on_message=on_message
mqttBroker = "192.168.0.199"

client = mqtt.Client()
client.connect(mqttBroker, 1883)

client.loop_start()
client.on_message = on_message


client.subscribe(Topics["BACKEND_DATA_SENSORDATA"])
client.subscribe("Bilal")
time.sleep(30) time.sleep(30)
client.loop_stop()
client.loop_stop()

+ 2
- 2
software/backend/main.py View File

# inits # inits
mydatabase = PlantDataBase() mydatabase = PlantDataBase()
mydatabase.create_table() 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): 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:

+ 16
- 11
software/backend/plantdatabase.py View File

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 " \
" 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):
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() self.conn.commit()


def insert_measurement_data(self, plant_id, def insert_measurement_data(self, plant_id,
""" """
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],
"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 return json_file



+ 3
- 2
software/defines.py View File

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"
"Brightness": 0, "Brightness": 0,
"PlantID": 0, "PlantID": 0,
"Timestamp": "", "Timestamp": "",
"MeasurementID": 0
"MeasurementID": 0,
"PlantName": ""
} }


ALLPLANTDATA = [ ALLPLANTDATA = [

Loading…
Cancel
Save