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
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]:
remote_nodes = ['n_Alpha', 'n_Lambda', 'n_Beta']
remote_nodes
Out[3]:
In [4]:
targeted_node = 'n_Lambda'
targeted_node
Out[4]:
In [4]:
messages = OrderedDict()
In [6]:
messages['write_GPIOs'] = {'message_type': 'command',
'command': 'write GPIOs',
'kwargs': {'pins_and_values': [(2, 0),]}}
the_client.request(targeted_node, messages['write_GPIOs']);
In [7]:
messages['write_GPIOs'] = {'message_type': 'command',
'command': 'write GPIOs',
'kwargs': {'pins_and_values': [(2, 1),]}}
the_client.request(targeted_node, messages['write_GPIOs']);
In [5]:
messages['blink_led'] = {'message_type': 'command',
'command': 'blink led',
'kwargs': {'times': 3, 'on_seconds': 0.1, 'off_seconds': 0.1}}
for remote_node in remote_nodes:
the_client.request(remote_node, messages['blink_led']);
In [19]:
the_client.request('Hub', messages['blink_led']);
In [ ]:
messages['read_GPIOs'] = {'message_type': 'command',
'command': 'read GPIOs',
'kwargs': {'pins': [5, 12, 13, 14, 15, 16]},
'need_result': True}
_, result = the_client.request(targeted_node, messages['read_GPIOs'])
print('\nGPIO status for {}: {}\n'.format(targeted_node, result.get()));
In [ ]:
rpc = the_client.request(targeted_node, messages['read_GPIOs'])[1].get
rpc()
In [ ]:
# for remote_node in remote_nodes:
# _, result = the_client.request(remote_node, messages['read_GPIOs'])
# print('\nGPIO status for {}: {}\n'.format(remote_node, result.get()))
In [ ]:
status = []
for remote_node in remote_nodes:
_, result = the_client.request(remote_node, messages['read_GPIOs'])
status.append((remote_node, result))
status
In [ ]:
for remote_node, result in status:
print('\nGPIO status for {}: {}\n'.format(remote_node, result.get()))
In [ ]:
messages['test_eval'] = {'message_type': 'eval',
'to_evaluate': '2+3',
'need_result': True}
_, result = the_client.request(targeted_node, messages['test_eval'])
print('result:', result.get());
In [ ]:
messages['test_exec'] = {'message_type': 'exec',
'to_exec': 'print("Hello World!")'}
the_client.request(targeted_node, messages['test_exec']);
In [ ]:
# messages['test_function'] = {'message_type': 'function',
# 'function': 'blink_led',
# 'kwargs': {'times': 3, 'on_seconds': 0.1, 'off_seconds': 0.1}}
# the_client.request(targeted_node, messages['test_function']);
In [ ]:
with open('functions_def.py') as f:
script = f.read()
the_client.request(targeted_node, {'message_type': 'script',
'script': script})
the_client.request(targeted_node, {'message_type': 'exec',
'to_exec': "import script"})
the_client.request(targeted_node, {'message_type': 'exec',
'to_exec': "script.function_to_test('_______ Testing dynamic function ______')"});
In [ ]:
# with open('script_to_deploy.py') as f:
# script = f.read()
# messages['test_upload_script'] = {'message_type': 'script',
# 'script': script}
# the_client.request(targeted_node, messages['test_upload_script']);
In [3]:
# Stopping
the_client.stop()
the_client = None
print('\n[________________ Demo stopped ________________]\n')
In [ ]: