2023-04-03 14:59:23 +02:00
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import json
|
|
|
|
from raspySensors import RaspySensors
|
|
|
|
|
|
|
|
#region global Varaibles
|
|
|
|
sensors = RaspySensors()
|
|
|
|
|
|
|
|
|
2023-04-07 16:05:15 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region
|
|
|
|
|
|
|
|
def measureSendData():
|
|
|
|
'''Measure Data via sensor class and send via MQTT'''
|
|
|
|
sensorData = sensors.readSensors()
|
|
|
|
client.publish("Robot/Data", json.dumps(sensorData, indent=4))
|
|
|
|
|
2023-04-03 14:59:23 +02:00
|
|
|
#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))
|
|
|
|
|
2023-04-07 16:05:15 +02:00
|
|
|
#Function for all Plants or one which changes according to request
|
2023-04-03 14:59:23 +02:00
|
|
|
def drive_plant_1(clients, userdata, message):
|
2023-04-07 16:05:15 +02:00
|
|
|
#TODO Start drive forward
|
2023-04-03 14:59:23 +02:00
|
|
|
print("Driving to plant 1")
|
2023-04-07 16:05:15 +02:00
|
|
|
measureSendData()
|
|
|
|
#TODO Start Drive Back Function
|
|
|
|
print("Driving back to start position")
|
|
|
|
|
|
|
|
|
2023-04-03 14:59:23 +02:00
|
|
|
|
|
|
|
#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()
|