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]:
import pandas as pd
from pandas import DataFrame
from time import sleep
REFRACTORY_PERIOD = 0.1 # 0.1 seconds
In [3]:
neurons = ['n_Alpha', 'n_Beta', 'n_Lambda']
# neurons = ['n_Alpha']
neurons
Out[3]:
In [4]:
the_client = client.Client()
the_client.start()
while not the_client.status['Is connected']:
time.sleep(1)
print('Node not ready yet.')
In [5]:
# # Ask Hub for a list of connected nodes
# def list_nodes():
# the_client.node.worker.roll_call()
# time.sleep(2)
# remote_nodes = sorted(the_client.node.worker.contacts.keys())
# print('\n[____________ Connected nodes ____________]\n')
# print('\nConnected nodes:\n{}\n'.format(remote_nodes))
# return remote_nodes
In [5]:
def reset_node(node):
message = {'message_type': 'exec',
'to_exec': 'import machine;machine.reset()'}
the_client.request(node, message)
In [6]:
def fire(node):
message = {'message_type': 'function',
'function': 'fire'}
the_client.request(node, message)
def addConnection(node, neuron):
message = {'message_type': 'function',
'function': 'addConnection',
'kwargs': {'neuron_id': neuron}}
the_client.request(node, message)
def set_connections(node, connections):
message = {'message_type': 'function',
'function': 'setConnections',
'kwargs': {'connections': connections}}
the_client.request(node, message)
def get_connections(node):
message = {'message_type': 'function',
'function': 'getConnections',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def setWeight(node, neuron, weight):
message = {'message_type': 'function',
'function': 'setWeight',
'kwargs': {'neuron_id': neuron,
'weight': weight,}}
the_client.request(node, message)
def setThreshold(node, threshold):
message = {'message_type': 'function',
'function': 'setThreshold',
'kwargs': {'threshold': threshold}}
the_client.request(node, message)
def getConfig(node):
message = {'message_type': 'function',
'function': 'getConfig',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def getLog(node):
message = {'message_type': 'function',
'function': 'getLog',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def emptyLog(node):
message = {'message_type': 'function',
'function': 'emptyLog'}
the_client.request(node, message)
def emptyLogs():
for neuron in neurons:
emptyLog(neuron)
def mergeLogs():
logs = []
for neuron in neurons:
if neuron != the_client.node.worker.name: # exclude client self
currentLog = getLog(neuron)
if currentLog:
logs += currentLog
df = DataFrame(list(logs), columns = ['time', 'neuron', 'message'])
df.set_index('time', inplace = True)
df.sort_index(inplace = True)
return df
In [7]:
def printConfig(neuron):
print('{0:_^78}\n {1}\n'.format(neuron + " config:", getConfig(neuron)))
In [8]:
# reset_node('Hub');
In [9]:
messages = {}
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 [10]:
addConnection('n_Alpha', 'n_Lambda');
addConnection('n_Beta', 'n_Lambda');
setWeight('n_Lambda', 'n_Alpha', 1);
setWeight('n_Lambda', 'n_Beta', 1);
In [13]:
setThreshold('n_Lambda', 2.8); # input enough to trigger Lambda
fire('n_Alpha');
fire('n_Beta');
In [14]:
setThreshold('n_Lambda', 2.8); # input not enough to trigger Lambda
fire('n_Alpha');
fire('n_Beta');
In [13]:
setThreshold('n_Lambda', 1.8); # input not enough to trigger Lambda
fire('n_Alpha');
In [15]:
setThreshold('n_Lambda', 1.8); # input enough to trigger Lambda
fire('n_Alpha');
fire('n_Beta');
In [371]:
# setThreshold('n_Lambda', 1.8);
# emptyLogs()
# sleep(REFRACTORY_PERIOD)
# fire('n_Alpha')
# fire('n_Beta')
# sleep(2)
# mergeLogs()
In [372]:
# for neuron in reversed(neurons): printConfig(neuron)
In [15]:
# Stopping
the_client.stop()
the_client = None
print ('\n[________________ Demo stopped ________________]\n')
In [ ]: