Lesson Communication in an IoT Network - Internet of Things - ثاني ثانوي

Lesson 2 Link to lesson Communication in an IoT Network www.len.edu Communication between Devices An loT network consists of many devices that send and receive data between each other. These devices have different capabilities, such as different range, data bandwidth and power consumption, and thus run different sets of commands to provide a multitude of functions. In the following project, you will build such a network and explore the various fundamental blocks it consists of Fire Surveillance and Notification In this lesson, you are going to create a project that simulates a fire surveillance system in a factory. The simulation will create random fires in the factory and the system will inform the factory's main control unit about which sector the fire is in. This will be achieved by using a variety of nodes, with different functions, that will communicate with each other to pass the messages. It will start from the outer nodes, the "edges", pass through the middle ones, the "proxies", to finally arrive at the inner node, the "main controller". The Nodes and their Functions To represent the different sectors of a factory, you will use three kinds of nodes in your project: ■In the edges a fire may occur, determined by a random number generator. In such cases, the node will print a message on itself and send a different message containing the sector number to its adjacent proxy node and then sleep for a specific interval. • The proxies read any messages they may have received and forward them to the main controller. They also print a message on themselves to inform the user of their action. The main controller likewise reads any messages may have received and prints the messages reated by the edge nodes that inform the user of the fire. Edge Edge PTONY PIOXY Controller Edge Edge Figure 8.12: Nodes and their functions وزارة التعليم 2173-1865

Lesson 2 Communication in an IoT Network

Let's start by creating a new project: To create a new project: > Click on the New Project button on the Toolbar. > Choose your desired location for the project to be stored, type "Fire Surveillance and Notification" in the field File name and click on Save. 16 CupCarbon IoT 5.0 Project East Add Display Selection Solver Simulation Map Personal Hep 1twork information Devices & Cufecti ▸ Device Parameters Radio Parameters ▸ Marker Parameter 日 ▸ Simulation Parameters CeCetine Project PC T My First Copication Semolition Fa MFSurveillance and Fistication 2 وزارة التعليم de Fo שון-נקו Figure 813 Creating a new project 3

Lesson 2 Communication in an IoT Network

Begin building the node network by placing the main controller and the proxies: To place the controller and proxy nodes: > Click on the lot Node button on the Toolbar. > Click on the Map to place the node. > Click on the All button from the State Bar. > Place two more nodes on the left and right of the first one, inside its outer circle. > Press Esc If the nodes are not placed inside the controller's radius, they will not be able to communicate. If this is the case, drag and drop them closer to the controller. until a two-directional arrow appears connecting the two nodes. 1 وزارة التعليم 3 4 2 The first node to be placed is the main controller and the others are the proxies. ! Figure 8. Placing the controller and proxy nodo п 375

Lesson 2 Communication in an IoT Network

Continue by adding the edge nodes: To place the edge nodes: > Click on the lot Node button on the Toolbar. > Place two nodes on each proxy node, inside its outer circle and not in the range of any other node. > Click on the All button from the State Bar. 3 > Press Esc 1 وزارة التعليم 3 2 Figure & 15 Placing the edge nodes 15

Lesson 2 Communication in an IoT Network

Creating the Scripts You will now take a look at the scripts that the nodes will be running. Let's start with the edges' script. First, include the necessary libraries. import time import random The randint() function takes as argument two integers and returns an integer from the rangewhich includes the two Integers. For example, in our case randint(1, 6) randomly creates an integer from 1 to 6. This will be used as the random chance a sector may catch fire in each time interval. The integer will be stored in the variable fire. while node.loop(): fire = random.randint(1, 6) Consider that when the random number generator returns 1, the sector will have caught fire. The script will check if the variable fire is equal to 1 and if so, it will run a series of commands. This includes printing on the node itself the message "FIRE!" and sending a message containing its sector ID to the proxy node it is adjacent to. The sector's ID is the same as the node's ID number, the integer that makes it unique. If the sector's ID is 5, the message sent will be "FIRE IN SECTOR 5". The node's ID and therefore the sector's can be returned by the function id(). The ID is returned as a number, so it has to be "casted" as a string first, in other words converted to the string type, before being concatenated with the remaining message. if fire == 1: node.print("FIRE!") message "FIRE IN SECTOR" + str(node.id()) The nodes can send data to each other using the send() function. With only one argument, the function takes a string and broadcasts it to all the nodes inside its range. node.send(message) صيلية التليم 3623-1985 W

Lesson 2 Communication in an IoT Network

If the random number generator produces any other integer, in our case from 2 to 6, there is no fire in the sector and the node simply has to print an empty string on itself to clear any previous prints. else: node.print("") Finally, the node will sleep for a random time interval, to more closely simulate the real life randomness of events and incidents. This will be achieved by using the uniform() function that works like the randint() function but produces real numbers and not only integers. In your project, the time intervals will be between 1 and 4 seconds. time.sleep(random.uniform(1, 4)} Complete Code (edge.py) import time import random while node.loop(): fire random.randint(1, 5) if fire == 1: node.print("FIRE!") " message "FIRE IN SECTOR + str(node, id()) node.send(message) else: node.print("") time.sleep(random.uniform(1, 4)) وزارة الكليم 173-1445 777

Lesson 2 Communication in an IoT Network

Next is the proxies' script. When a node receives data, the data is stored to its buffer until read, thus initially the buffer's size must be checked to be greater than zero, meaning it is not empty. The buffer's size can be returned by the function bufferSize(). import time while node.loop(): if node.bufferSize() > 0: Then, the data received can be read using the function read(). After the message is read, it is stored to the variable message. The node also prints an informative message "FORWARDING..." to say that it forwards the message to the main controller. message node.read() node.print("FORWARDING...") As used in the previous script, the message stored in the variable will be sent to the main controller with the function send(). This time though, apart from the message it will take an extra argument, the receiving node's ID. Because the message is specific to one node with a known ID, the message does not have to be broadcasted, it can instead be unicasted (sent to only one node). In our case, the main controller was placed first, thus has an ID equal to 1. node.send(message, 1) Next, the node will sleep for 1 second, so as to give the user time to read the informative message printed on the node and then the node clears the message by printing an empty string. time.sleep(1) node.print("") The script ends with the node sleeping for a very small interval (one hundredth of a second), so it can be responsive in case it receives a lot of data. time sleep(0.01) قرارة التعليم

Lesson 2 Communication in an IoT Network

Complete Code (proxy.py) import time white node. Loop(): if node bufferSize() > 0: message = node.read() node.print("FORWARDING...") node.send(message, 1) time.sleep(1) node.print("") time.sleep(0.01) The number parameter in the send() function is the ID number of the contreller node. The main controller's script bears some resemblance to the proxies. It also checks the buffer and reads the message received, but the the message it prints on itself is the one originally created by the edge node. import time while node.loop(): if node,bufferSize() > 0: message node.read() node.print(message) Then, like the proxy nodes, the main controller sleeps, but for 2 seconds this time and prints an empty string. Finally, it sleeps for a small interval, in the same fashion as the proxies. time.sleep(2) node.print(" time.sleep(0.01) مرارة التعليم 1123-1443

Lesson 2 Communication in an IoT Network

Complete Code (controller.py) import time while node.loop(): if node.bufferSize() > 0: message node.read() node.print(message) time.sleep(2) node.print("") time.sleep(0.01) وزارة التعليم 077-1445 Controller Proxy Figure 8.18: Complete network Edge

Lesson 2 Communication in an IoT Network

Now that you have seen what the scripts do, go on and create them. To create and apply the script for the controller node: To create a script: > Click on the Python button on the Toolbar. > Type the Python code into the field. 2 > Type controller in the File name field. > Click Save. > Close the Python Editor window. CupCarbon lo7 5.0 [CACupCarbon Projects\Fire Surv Project Edit Ada Day Selection Scriver 1 Python Editor commuter Import time 3 while node.loop(); if node.bufferSize() > 0: message node.read() pode print (Dessage) time, sleep (2) node.print("") time.aleep (0.01) وزارة الكليمر 123-1865 2 Figure 8.17. Create the script Pember 4 ㅁ X5

Lesson 2 Communication in an IoT Network

R To insert the script: > Click on the node. > Click on the Device Parameters tab on the Parameter Menu. > Click on the Script file box. > From the drop down menu, choose the controller.py script and click on the button on the right to insert the script into the node. > Click on the Save Project button on the Toolbar. Carbon TSICA Cup Carbon Projects Fire Surveillance and Notification] 日 Dec 23 4 Sing T وزارة التعليم m Figure 18 Inserting the scrip 1

Lesson 2 Communication in an IoT Network

Likewise, create the other scripts, copy their codes and apply them to their corresponding nodes, so all nodes have the script. When you are done, click the Run IoT Simulation button from the toolbar. Notice that because you used the random number generators, some sectors on the edges may catch fire more than others which may not catch fire at all. Use self-explanatory script names such as edge.py and proxy.py. وزارة التعليم Figure 8.19- The states of the simulation

Lesson 2 Communication in an IoT Network

Exercises 1 Extend your project to support an additional "edge" node on each "proxy" node, so that each "proxy" has 3 "edges". Do not forget to insert scripts into the new nodes. 2 Extend your project to support an additional "proxy" node and add to this "proxy" 2 new "edge" nodes, so that the "main controller" has 3 "proxies" and each "proxy" has 2 "edges" Do not forget to insert scripts into the new nodes. 3 State which code section in the scripts influences the frequency that the fires occur. Then, in CupCarbon, modify your project so that fires are more likely to occur than before. 4 If there is latency in the factory's network, communication between nodes may be delayed Modify the "proxy" nodes' script to make the nodes sleep for longer Are any messages delayed and/or lost? Present your observations below. 5 Extend your project to also support floods. Modify the script of the sectors that are susceptible to fires, so when the random function returns the value 2, it will mean that a Padmhas occurred in that sector and the node will print and send the appropriate message.

Lesson 2 Communication in an IoT Network