Академический Документы
Профессиональный Документы
Культура Документы
The goal of this project is to be able to send data between the boards and further familiarize
ourselves with the Python Programming language.
So far, in all the projects we have built, we read data either from the environment, by using the
temperature and humidity sensor, or write data to external devices such as LEDs and the LCD
display, or in the console of the Wyliodrin Lab interface. The purpose of this project is to make
the previous projects more interactive and have our devices communicate.
In order to send data between the boards, we will use a few functions/code blocks which
facilitate the process, but before going into that, we will need to understand a few parameters
used by the communication process:
● Topic
A board can receive multiple messages from different other boards, for different purposes and
therefore, in order to able to better interpret and send messages between devices, each
message that is sent through the network must provide a topic. By default, the topic which is
provided by the code blocks and by the functions (if no topic is explicitly defined) is the “default”
topic.
● Sender/Receiver
In order to be able to trace messages sent and received, the messages sent through the
network specify by whom they were sent and to whom they were sent. However, when we
program the boards in order to communicate with each other, we do not have to specify both the
sender or the receiver, since one of them is clear (when i am the sender, the sender_id is my id
and when i am the receiver, the receiver_id is my id). Therefore, when writing the code/using
the code blocks for communicating with a specific board, we will only specify the other id: the
one to whom we are sending or from which we are receiving.
Exception: When we are expecting messages from multiple boards, we can avoid specifying
the sender when writing the code so that we can receive from more than just one device.
However, the topic parameter is mandatory. We will further see this aspect when we are
discussing the code blocks.
● Text Message
And of course, the message sent must also specify the payload or the text message that we
need to send from one board to another.
Now, getting to the code blocks and the functions required for the exchange of messages, we
will have two types of functions: functions used for sending messages and functions used for
receiving messages.
1. Sending
In order to send data from one board to another one on a specific topic, we have the following
code block :
myBoard = LabNetwork()
myBoard.sendMessage(“message”, “board”, “default”)
with which we define the object for the communication and then we use the sendMessage
method to send the message “message”, to the device which is identified by the board_id
“board”, on the topic “default”.
2. Receiving
In order to listen for messages sent by a specific board on a specific topic, we have the
following code block:
The equivalent python code will be:
myBoard = LabNetwork()
message = AwayInfo( topic=’default’, board=’boardId”)
message.getAvailable()
with which we define the object for the communication, then tell the board to listen for messages
received by it from the network on the topic called “default” and from the board identified by the
id “boardId” and finally, return the message received using message.getAvailable() .
Pay attention! In order to use the value returned by the function message.getAvailable(), you
have to store it first in a variable.
In order to listen for messages sent by any board on a specific topic, we have the following
code block:
myBoard = LabNetwork()
message = AwayInfo( topic=’default’, board=None)
message.getAvailable()
with which we define the object for the communication, then tell the board to listen for messages
received by it from the network on the topic called “default”, with the “board” parameter set as
“None”, in order to make the device accept messages from any board and, finally, return the
message received using message.getAvailable() .
In which we tell the device to send the “Hello from the other board” message to the board
identified by the id “c8f3dede” on the topic “default” every 1 second.
Remember to change the board id shown in the code above to the corresponding id of the
receiver board.
In which we tell the device to repeat the following process each second: listen to any message
received on the topic “default” from the board with the board id “2e162eaa” and if such message
is received, to store it in the “receivedMessage” variable and finally, write it in the console of the
platform.
Remember to change the board id shown in the code above to the corresponding id of the
receiver board.
We chose to create infinite loops in both sender and receiver, so that we do not need to
synchronise the sending and receiving process in order to see the result.
After creating the projects on both of the devices and writing the code, it is time to run both of
the projects and see the result.
Finally, let’s take a look at the code. For the sender, the generated code for the blocks is:
# the following two lines are for the device to know how to send the data
import os
os.environ[“BROKER_ADRESS”] = “192.168.1.68”
# import the Timer module needed in order to execute code every given number of seconds
from threading import Timer
# send the message to the board with id equal to ‘c8ef3dede’ on topic ‘default’
myBoard.sendMessage(‘Hello from the other board!’, ‘c8ef3dede’, ‘default’)
# tell the device to run the loopCode function after exactly one second has passed
Timer(1, loopCode).start()
# the following two lines are for the device to know how to send the data
import os
os.environ[“BROKER_ADRESS”] = “192.168.1.68”
# import the Timer module needed in order to execute code every given number of seconds
from threading import Timer
# i nitialise the variable in which we will get the message received from the other board
receivedMessage = None
# use the AwayInfo in order to tell the device to wait for a message from the board with the id
# equal to “2e162eaa”, on the topic “default”
message = AwayInfo(topic = ‘default’, board = ‘2e162eaa’ )
# we tell python that the variable used below is the one defined before the function
# and not a new one created in the function
global receivedMessage
# tell the device to run the loopCode function after exactly one second has passed
Timer(1, loopCode).start()
7.4 Exercises
1. Connect the light sensor to a board and display the value detected by the sensor on
another board.
You will use code blocks for this exercise.
3. Turn the onboard LEDs on one board on based on the light detected by another board.
You will use python code for this exercise.
4. Use two boards in order to create a pedestrian semaphore. On one board, build the
semaphore and on the other we will use a button which will control the semaphore
You will use code blocks for this exercise.
5. On one board, build the intelligent house and on the other print the temperature and the
humidity levels on the LCD Display.
You will use python code for this exercise.
6. Using smart energy. On one board connect the Solar Panel and the Windmill and on the
other board the smart house.
You can use either python code or c ode blocks for this exercise
7. For this exercise, you will all work together: everybody uses an LED on the board,
except one person which turns on all the LEDs.
You can use either python code or c ode blocks for this exercise