Start a Mosquitto container first. For example:
codes\_demo\1_start_broker.sh
to start a Mosquitto container on Raspberry Pi.mqtt_config\mqtt
.allow_anonymous true
in mqtt_config\mqtt\config\mosquitto.conf
to allow anonymous client.
In [1]:
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
In [2]:
the_client = client.Client()
the_client.start()
while not the_client.status['Is connected']:
time.sleep(1)
print('Node not ready yet.')
In [3]:
# NODE1_EUI = 'f3d308fffe00'
# GATEWAY1_EUI = '32aea4fffe809528'
# NODE2_EUI = '32aea4fffe054928'
# GATEWAY2_EUI = '260ac4fffe0c1764'
gateways = ['32aea4fffe809528', '260ac4fffe0c1764']
gateways
Out[3]:
In [4]:
gateway = '260ac4fffe0c1764'
gateway
Out[4]:
In [5]:
messages = OrderedDict();
In [6]:
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']);
In [29]:
# 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
Out[29]:
In [38]:
# 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']);
In [41]:
# 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']);
In [ ]:
# Stopping
the_client.stop()
the_client = None
print('\n[________________ Demo stopped ________________]\n')