Commit 65deb7ef authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Adds basic_ohmpi_flows_node-red. Updates architecture.pdf. Moves...

Adds basic_ohmpi_flows_node-red. Updates architecture.pdf. Moves mqtt_controller.py in examples and updates it.
Showing with 46 additions and 351 deletions
+46 -351
No preview for this file type
[{"id":"61c1655c50bd371c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2d6881abe9336fbf","type":"mqtt in","z":"61c1655c50bd371c","name":"","topic":"ohmpi_0001/exec","qos":"2","datatype":"auto-detect","broker":"6ae7e77e.04c64","nl":false,"rap":false,"inputs":0,"x":390,"y":40,"wires":[["02e94bb48ce2cded"]]},{"id":"e2590e574c551cb9","type":"mqtt in","z":"61c1655c50bd371c","name":"","topic":"ohmpi_0001/data","qos":"2","datatype":"auto","broker":"6ae7e77e.04c64","nl":false,"rap":false,"inputs":0,"x":380,"y":140,"wires":[["b9a9d56fd4fb0b8c"]]},{"id":"e2c109f78f9e714c","type":"mqtt out","z":"61c1655c50bd371c","name":"MQTT ctrl","topic":"ohmpi_0001/ctrl","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"6ae7e77e.04c64","x":820,"y":340,"wires":[]},{"id":"e1d067d8532ff36b","type":"mqtt in","z":"61c1655c50bd371c","name":"","topic":"ohmpi_0001/soh","qos":"2","datatype":"auto","broker":"6ae7e77e.04c64","nl":false,"rap":false,"inputs":0,"x":380,"y":240,"wires":[["f5c9dacaafac51d0"]]},{"id":"a7a6f6068e01c7eb","type":"ui_button","z":"61c1655c50bd371c","name":"Run sequence","group":"142ad6ae.d55e29","order":1,"width":"1","height":"1","passthru":false,"label":"⏺","tooltip":"run sequence","color":"red","bgcolor":"lightgrey","className":"","icon":"","payload":"{\"cmd_id\" :\"0\", \"cmd\":\"run_sequence_async\"}","payloadType":"str","topic":"topic","topicType":"msg","x":380,"y":340,"wires":[["e2c109f78f9e714c"]]},{"id":"d82912d9c5b122fe","type":"ui_button","z":"61c1655c50bd371c","name":"Interrupt","group":"142ad6ae.d55e29","order":2,"width":"1","height":"1","passthru":false,"label":" ◾","tooltip":"interrupt sequence","color":"black","bgcolor":"lightgrey","className":"","icon":"","payload":"{\"cmd_id\" :\"0\", \"cmd\":\"interrupt\"}","payloadType":"str","topic":"topic","topicType":"msg","x":360,"y":400,"wires":[["e2c109f78f9e714c"]]},{"id":"02e94bb48ce2cded","type":"ui_text","z":"61c1655c50bd371c","group":"64a75353.37700c","order":2,"width":"16","height":"3","name":"MQTT exec","label":"Execution","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":40,"wires":[]},{"id":"b9a9d56fd4fb0b8c","type":"ui_text","z":"61c1655c50bd371c","group":"64a75353.37700c","order":3,"width":"16","height":"3","name":"MQTT Data","label":"Data","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":140,"wires":[]},{"id":"f5c9dacaafac51d0","type":"ui_text","z":"61c1655c50bd371c","group":"64a75353.37700c","order":4,"width":0,"height":0,"name":"MQTT SOH","label":"SOH","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":240,"wires":[]},{"id":"329591d611aa2704","type":"ui_button","z":"61c1655c50bd371c","name":"","group":"64a75353.37700c","order":1,"width":0,"height":0,"passthru":false,"label":"clear messages","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":" ","payloadType":"str","topic":"topic","topicType":"msg","x":580,"y":160,"wires":[["f5c9dacaafac51d0","b9a9d56fd4fb0b8c","02e94bb48ce2cded"]]},{"id":"25d69085f401beae","type":"ui_dropdown","z":"61c1655c50bd371c","name":"","label":"command","tooltip":"","place":"Select option","group":"142ad6ae.d55e29","order":5,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"load_sequence","type":"str"},{"label":"","value":"reset_mux","type":"str"},{"label":"","value":"set_sequence","type":"str"},{"label":"","value":"update_settings","type":"str"}],"payload":"","topic":"command","topicType":"str","className":"","x":140,"y":480,"wires":[["182249692ee7502c"]]},{"id":"edbd2d507fbf085a","type":"ui_text_input","z":"61c1655c50bd371c","name":"","label":"kwargs","tooltip":"","group":"142ad6ae.d55e29","order":5,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"kwargs","sendOnBlur":true,"className":"","topicType":"str","x":140,"y":600,"wires":[["679471976db01c0e"]]},{"id":"679471976db01c0e","type":"json","z":"61c1655c50bd371c","name":"","property":"payload","action":"","pretty":false,"x":290,"y":600,"wires":[["b11654144c63d1b2"]]},{"id":"435a126aec0c424c","type":"debug","z":"61c1655c50bd371c","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":480,"wires":[]},{"id":"45c95568bb348470","type":"join","z":"61c1655c50bd371c","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":650,"y":540,"wires":[["41122cfe96dd471a","e2c109f78f9e714c"]]},{"id":"41122cfe96dd471a","type":"ui_text","z":"61c1655c50bd371c","group":"142ad6ae.d55e29","order":6,"width":"2","height":"3","name":"","label":"Command to send","format":"{{msg.payload}}","layout":"row-spread","className":"","x":870,"y":600,"wires":[]},{"id":"182249692ee7502c","type":"function","z":"61c1655c50bd371c","name":"set cmd","func":"var newMsg = { payload: {\"cmd\": msg.payload }};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":480,"wires":[["435a126aec0c424c","45c95568bb348470"]]},{"id":"1549f01b20537e60","type":"ui_button","z":"61c1655c50bd371c","name":"","group":"142ad6ae.d55e29","order":7,"width":0,"height":0,"passthru":false,"label":"Send command","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":160,"y":700,"wires":[["c81857e22b65ef26","ae78c7b9e5aaeede"]]},{"id":"d4f6486f114c987d","type":"change","z":"61c1655c50bd371c","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":700,"wires":[["45c95568bb348470"]]},{"id":"b11654144c63d1b2","type":"function","z":"61c1655c50bd371c","name":"set kwargs","func":"var newMsg = { payload: {\"kwargs\": msg.payload }};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":600,"wires":[["45c95568bb348470"]]},{"id":"c81857e22b65ef26","type":"uuid","z":"61c1655c50bd371c","uuidVersion":"v1","namespaceType":"","namespace":"","namespaceCustom":"","name":"","field":"payload","fieldType":"msg","x":310,"y":660,"wires":[["1f97edbce5f88461"]]},{"id":"ae78c7b9e5aaeede","type":"delay","z":"61c1655c50bd371c","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":390,"y":700,"wires":[["d4f6486f114c987d"]]},{"id":"1f97edbce5f88461","type":"function","z":"61c1655c50bd371c","name":"set cmd_id","func":"var newMsg = { payload: {\"cmd_id\": msg.payload }};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":660,"wires":[["45c95568bb348470"]]},{"id":"6ae7e77e.04c64","type":"mqtt-broker","name":"ohmpi_mqtt_broker","broker":"mg3d-dev.umons.ac.be","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"142ad6ae.d55e29","type":"ui_group","name":"Commands","tab":"5d888f29.07334","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"64a75353.37700c","type":"ui_group","name":"Messages","tab":"5d888f29.07334","order":2,"disp":true,"width":"16","collapse":true},{"id":"5d888f29.07334","type":"ui_tab","name":"Simple OhmPi controller","icon":"dashboard","disabled":false,"hidden":false}]
\ No newline at end of file
......@@ -22,13 +22,13 @@ settings = {
}
cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'update_settings', 'args': settings})
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'update_settings', 'kwargs': {'settings' : settings}})
print(f'Update settings setup message: {payload} to {publisher_config["topic"]} with config {publisher_config}')
publish.single(payload=payload, **publisher_config)
sequence = [[1, 2, 3, 4]]
cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'set_sequence', 'args': sequence})
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'set_sequence', 'kwargs': {'sequence': sequence}})
print(f'Set sequence message: {payload} to {publisher_config["topic"]} with config {publisher_config}')
publish.single(payload=payload, **publisher_config)
cmd_id = uuid.uuid4().hex
......@@ -38,7 +38,7 @@ publish.single(payload=payload, **publisher_config)
for i in range(4):
cmd_id = uuid.uuid4().hex
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'start'})
payload = json.dumps({'cmd_id': cmd_id, 'cmd': 'run_sequence_async'})
print(f'Publishing message {i}: {payload} to {publisher_config["topic"]} with config {publisher_config}')
publish.single(payload=payload, **publisher_config)
time.sleep(1)
......
import paho.mqtt.client as mqtt
from config import MQTT_CONTROL_CONFIG, CONTROL_CONFIG, OHMPI_CONFIG
import time
from queue import Queue
import zmq
ctrl_queue = Queue()
def on_message(client, userdata, message):
global socket
# Send the command
print(f'Sending command {message.payload.decode("utf-8")}')
socket.send(message.payload)
# Get the reply
reply = socket.recv()
print(f'Received reply {message.payload.decode("utf-8")}: {reply}')
mqtt_client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_listener', clean_session=False) # create new instance
print('connecting command listener to broker')
trials = 0
trials_max = 10
broker_connected = False
while trials < trials_max:
try:
mqtt_client.username_pw_set(MQTT_CONTROL_CONFIG['auth'].get('username'),
MQTT_CONTROL_CONFIG['auth']['password'])
mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname'])
trials = trials_max
broker_connected = True
except:
print('trying again...')
time.sleep(2)
trials += 1
if broker_connected:
print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic'])
mqtt_client.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos'])
mqtt_client.on_message = on_message
mqtt_client.loop_start()
context = zmq.Context()
# Socket to talk to server
print("Connecting to ohmpi control server")
socket = context.socket(zmq.REQ)
socket.connect(f'tcp://localhost:{CONTROL_CONFIG["tcp_port"]}')
while True:
time.sleep(.1)
else:
print("Unable to connect to control broker")
exit(1)
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment