Robot Docu extended and main to mainProg added
This commit is contained in:
parent
bbc895c7dd
commit
f29e567f07
Binary file not shown.
@ -4,6 +4,7 @@ from raspySensors import RaspySensors
|
|||||||
|
|
||||||
#region global Varaibles
|
#region global Varaibles
|
||||||
sensors = RaspySensors()
|
sensors = RaspySensors()
|
||||||
|
client = mqtt.Client()
|
||||||
|
|
||||||
sensorData = {
|
sensorData = {
|
||||||
"Air Temperature [°C]" : 0,
|
"Air Temperature [°C]" : 0,
|
||||||
@ -27,9 +28,9 @@ batteryStatus = {
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region
|
#region global functions
|
||||||
|
|
||||||
def measureSendData(plantID, actionID):
|
def measure_send_data(plantID, actionID):
|
||||||
'''Measure data for one plant via sensor class and send via MQTT'''
|
'''Measure data for one plant via sensor class and send via MQTT'''
|
||||||
sensorData = sensors.readSensors()
|
sensorData = sensors.readSensors()
|
||||||
sensorData["Plant_ID"] = plantID
|
sensorData["Plant_ID"] = plantID
|
||||||
@ -37,6 +38,26 @@ def measureSendData(plantID, actionID):
|
|||||||
client.publish("Robot/Data/SensorData", json.dumps(sensorData, indent=4))
|
client.publish("Robot/Data/SensorData", json.dumps(sensorData, indent=4))
|
||||||
|
|
||||||
|
|
||||||
|
def init_mqtt():
|
||||||
|
'''Initialise MQTT client'''
|
||||||
|
mqttBroker = "mqtt.eclipseprojects.io"
|
||||||
|
client = mqtt.Client("Robot")
|
||||||
|
|
||||||
|
#Add callbacks
|
||||||
|
client.message_callback_add("Robot/Data", send_data_json) #Testing
|
||||||
|
client.message_callback_add("Robot/Action/Drive", drive_plant)
|
||||||
|
client.message_callback_add("Robot/Action/GetPosition", get_position)
|
||||||
|
client.message_callback_add("Robot/Action/GetBattery", get_BatteryStatus)
|
||||||
|
|
||||||
|
#Subscribe to topics
|
||||||
|
client.subscribe("Robot/Data") #Testing
|
||||||
|
client.subscribe("Robot/Action/Drive")
|
||||||
|
client.subscribe("Robot/Action/GetPosition")
|
||||||
|
client.subscribe("Robot/Action/GetBattery")
|
||||||
|
|
||||||
|
client.connect(mqttBroker)
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region MQTT callbacks
|
#region MQTT callbacks
|
||||||
@ -56,12 +77,12 @@ def drive_plant(clients, userdata, message):
|
|||||||
#[ ]TODO Start drive forward -> Thread
|
#[ ]TODO Start drive forward -> Thread
|
||||||
|
|
||||||
print(f"Measuring data at Plant {message}")
|
print(f"Measuring data at Plant {message}")
|
||||||
measureSendData() # With threads not here
|
measure_send_data() # With Signal not here
|
||||||
|
|
||||||
print("Driving back to start position")
|
print("Driving back to start position")
|
||||||
#[ ]TODO Start Drive Back Function in Thread
|
#[ ]TODO Start Drive Back Function in Thread
|
||||||
|
|
||||||
print("Back at starting Position")
|
print("Back at starting Position") #Signal
|
||||||
|
|
||||||
|
|
||||||
def get_position(clients, userdata, message):
|
def get_position(clients, userdata, message):
|
||||||
@ -74,8 +95,8 @@ def get_position(clients, userdata, message):
|
|||||||
client.publish("Robot/Data/Position", json.dumps(gpsPosition, indent=4))
|
client.publish("Robot/Data/Position", json.dumps(gpsPosition, indent=4))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_BatteryStatus(clients, userdata, message):
|
def get_BatteryStatus(clients, userdata, message):
|
||||||
|
|
||||||
'''Callback function for battery status request
|
'''Callback function for battery status request
|
||||||
Function to read battery status from ev3 and send via MQTT'''
|
Function to read battery status from ev3 and send via MQTT'''
|
||||||
|
|
||||||
@ -91,23 +112,18 @@ def get_BatteryStatus(clients, userdata, message):
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
mqttBroker = "mqtt.eclipseprojects.io"
|
|
||||||
client = mqtt.Client("Robot")
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
init_mqtt()
|
||||||
dataDict = {} #Testing
|
dataDict = {} #Testing
|
||||||
|
|
||||||
#Add callbacks
|
client.loop_start()
|
||||||
client.message_callback_add("Robot/Data", send_data_json) #Testing
|
|
||||||
client.message_callback_add("Robot/Action/Drive", drive_plant)
|
|
||||||
client.message_callback_add("Robot/Action/GetPosition", get_position)
|
|
||||||
client.message_callback_add("Robot/Action/GetBattery", get_BatteryStatus)
|
|
||||||
|
|
||||||
client.connect(mqttBroker)
|
while True:
|
||||||
|
pass
|
||||||
|
|
||||||
#Subscribe to topics
|
|
||||||
client.subscribe("Robot/Data") #Testing
|
|
||||||
client.subscribe("Robot/Action/Drive")
|
|
||||||
client.subscribe("Robot/Action/GetPosition")
|
|
||||||
client.subscribe("Robot/Action/GetBattery")
|
|
||||||
|
|
||||||
client.loop_forever()
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user