<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 MQTT bridged LoRa networks - demo
Start a Mosquitto container first. For example
Step1:  Start client
Step2:  List of nodes
Step3:  Prepare messages
Step4:  DEMOs
Blink each node (broadcasting)
Step5:  Send LoRa Packets
Step6:  Stop the demo
<ASSISTANT_TASK:>
Python Code:
import os
import sys
import time
import json
 
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'client')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'node')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'shared')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, os.path.sep.join(['..', 'codes']), 'micropython')))
 
import client
from collections import OrderedDict
Explanation: MQTT bridged LoRa networks - demo
Start a Mosquitto container first. For example:
- Use codes\_demo\1_start_broker.sh to start a Mosquitto container on Raspberry Pi.
- Config files are in mqtt_config\mqtt.
- set allow_anonymous true in mqtt_config\mqtt\config\mosquitto.conf to allow anonymous client.
Getting Started
What this notebook does:
- Using a client on PC
- List connected nodes
- Send messages to remote nodes:
 - Return results (read GPIOs)via RPC mechanism.
 - Write data to remote nodes (write GPIOs).
 - Execute arbitrary code on remote nodes.
End of explanation
the_client = client.Client()
the_client.start()
while not the_client.status['Is connected']:            
    time.sleep(1)
    print('Node not ready yet.')
Explanation: Start client
End of explanation
# NODE1_EUI = 'f3d308fffe00'
# GATEWAY1_EUI = '32aea4fffe809528'
# NODE2_EUI = '32aea4fffe054928'
# GATEWAY2_EUI = '260ac4fffe0c1764'
gateways = ['32aea4fffe809528', '260ac4fffe0c1764']
gateways
gateway = '260ac4fffe0c1764'
gateway
Explanation: List of nodes
End of explanation
messages = OrderedDict();
Explanation: Prepare messages
End of explanation
messages['blink_led'] = {'message_type': 'command',
                         'command': 'blink led',
                         'kwargs': {'times': 3, 'on_seconds': 0.1, 'off_seconds': 0.1}}
the_client.request('Hub', messages['blink_led']);
Explanation: DEMOs
Blink each node (broadcasting)
End of explanation
# SF8_NODE_EUI = 'f3d308fffe00'
# SF8_GATEWAY_EUI = '32aea4fffe809528'
# SF9_NODE_EUI = '32aea4fffe054928'
# SF9_GATEWAY_EUI = '260ac4fffe0c1764'
pay_load = {"message": "LoRa packet sent via MQTT.", "time": int(time.time()), "to": "32aea4fffe054928", "from": the_client.node.worker.name }
pay_load_json = json.dumps(pay_load) 
pay_load_json
# transmit_payload: gateway will transmit the LoRa payload regardless of routing table.
messages['LoRa test'] = {'message_type': 'function',
                         'function': 'transmit_payload',
                         'kwargs': {'payload_string': pay_load_json}}
the_client.request(gateway, messages['LoRa test']);
# dispatch_payload_json: 
    # find the nearest gateway to the destination node.
    # forward the payload data, and deligate to the nearest gateway to transmit the LoRa payload. 
messages['LoRa test'] = {'message_type': 'function',
                         'function': 'dispatch_payload_json',
                         'kwargs': {'pay_load_json': pay_load_json}}
the_client.request(gateway, messages['LoRa test']);
Explanation: Send LoRa Packets
End of explanation
# Stopping
the_client.stop()
the_client = None
print('\n[________________ Demo stopped ________________]\n')
Explanation: Stop the demo
End of explanation
<END_TASK>