From a3e3da12974b677ae16f9e96ef39fe26fa38b116 Mon Sep 17 00:00:00 2001 From: waldluis Date: Mon, 3 Apr 2023 14:59:23 +0200 Subject: [PATCH] JSON MQTT receive and Sensor-Class added --- requirements.txt | 2 +- software/roboter/raspy/mainProg.py | 35 ++++++++++++++++++++ software/roboter/raspy/raspySensors.py | 44 ++++++++++++++++++++++++++ software/roboter/raspy/receive_json.py | 20 ++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 software/roboter/raspy/mainProg.py create mode 100644 software/roboter/raspy/raspySensors.py create mode 100644 software/roboter/raspy/receive_json.py diff --git a/requirements.txt b/requirements.txt index 28e8a15..729e152 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,4 @@ Jinja2==3.1.2 MarkupSafe==2.1.2 requests==2.28.2 urllib3==1.26.14 -paho-mqtt \ No newline at end of file +paho-mqtt==1.6.1 \ No newline at end of file diff --git a/software/roboter/raspy/mainProg.py b/software/roboter/raspy/mainProg.py new file mode 100644 index 0000000..c059105 --- /dev/null +++ b/software/roboter/raspy/mainProg.py @@ -0,0 +1,35 @@ +import paho.mqtt.client as mqtt +import json +from raspySensors import RaspySensors + +#region global Varaibles +sensors = RaspySensors() + + +#endregion + +#region MQTT callbacks +def send_data_json(client, userdata, message): + strIn = str(message.payload.decode("UTF-8")) + dataDict = json.loads(strIn) + print("Received data: ", json.dumps(dataDict)) + +def drive_plant_1(clients, userdata, message): + print("Driving to plant 1") + +#endregion + + +mqttBroker = "mqtt.eclipseprojects.io" +client = mqtt.Client("Smartphone_temp") + +dataDict = {} + +client.message_callback_add("Robot/Data", send_data_json) +client.message_callback_add("Robot/Plant/1", drive_plant_1) + +client.connect(mqttBroker) +client.subscribe("Robot/Data") +client.subscribe("Robot/Plant/1") + +client.loop_forever() \ No newline at end of file diff --git a/software/roboter/raspy/raspySensors.py b/software/roboter/raspy/raspySensors.py new file mode 100644 index 0000000..25d0a41 --- /dev/null +++ b/software/roboter/raspy/raspySensors.py @@ -0,0 +1,44 @@ +from picamera import PiCamera + +class RaspySensors: + '''Class to handle all sensors''' + #Air Temp&Humidity + dht22 = 0 + + #Earth Humidity + + + #Brightness Sensor + + + #PiCamera + camera = PiCamera() + + #GPS + + #Varaibles + sensorData ={ + "Air Temperature" : 0, + "Air Humidity" : 0, + "Earth Humidity" : 0, + "Brightness" : 0 + } + image = 0 + position = 0 + + def __init__(self) -> None: + '''Init all Sensors + #Message if Error''' + dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False) + + def readSensors(self): + '''Read all Sensors and return Dictionary with data''' + return self.sensorData + + def takePicture(self): + '''Take picture and return image''' + return self.image + + def readPosition(self): + '''Read and return Position''' + return self.position \ No newline at end of file diff --git a/software/roboter/raspy/receive_json.py b/software/roboter/raspy/receive_json.py new file mode 100644 index 0000000..545c12c --- /dev/null +++ b/software/roboter/raspy/receive_json.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt +import json + +def on_message_json(client, userdata, message): + strIn = str(message.payload.decode("UTF-8")) + dataDict = json.loads(strIn) + print("Received data: ", json.dumps(dataDict)) + + +mqttBroker = "mqtt.eclipseprojects.io" +client = mqtt.Client("Smartphone_temp") + +dataDict = {} + +client.message_callback_add("Robot/Data", on_message_json) + +client.connect(mqttBroker) +client.subscribe("Robot/Data") + +client.loop_forever() \ No newline at end of file