Differences

This shows you the differences between two versions of the page.

Link to this comparison view

iot:labs:04 [2015/07/23 11:38]
madalina.tanea
iot:labs:04 [2017/09/11 09:35] (current)
alexandru.radovici created
Line 1: Line 1:
-====== Lab 04Programming with Streams====== +====== Lab 4Programming with Streams====== 
-===== Multicolor lamp ===== +===== Traffic Lights ​=====
- +
-Let's replace the simple blinking lamp with a multicolor one. +
- +
-The purpose of this project is to light up an RGB LED in different colors. In order to achieve this, you need three PWM pins to control the LED and have a say on each of its colors. To work with them, you can add three sliders in the Dashboard and, according on the value they send, to set the intensity of each color.  +
 ===== What you need ===== ===== What you need =====
-  *  One Edison ​connected to Wyliodrin;​ +  * One Raspberry Pi connected to Wyliodrin ​STUDIO
-  *  One RGB LED+  * Five LEDs; 
-  *  ​Three ​200 Ohm resistors;​ +  One button
-  *  ​Male-male jumper ​wires.+  * Six 200 Ohms resistors;​ 
 +  * Jumper ​wires
 +  * Breadboard.
  
 ===== The Setup ===== ===== The Setup =====
-Connect the RGB LED to the Edison ​following the schematics in the figure. +Connect the LEDs and the button ​to the Raspberry Pi following the schematics in the figure. 
-{{ :iot:labs:rgbled.png?400 |}} +{{ :iot:labs:traffic-lights.png?600 |}} 
-The RGB LED is simply an LED consisting of three simple LEDs: red onea green one and a blue one. Each of these LEDs is controlled ​by a PWM pinso that it can light brighter or dimmerAs result, the color of the RGB LED will be the result of these three colors.+The button ​is connected in voltage divider circuit. You can notice that the button has 4 legs. Usually, one leg from one side is connected to the one in front of it. However, there might be the case that the legs care connected side by side, this is why we encourage you to use measuring device in order to identify how the legs are connected. Once the button is pressedall the legs get connected. 
 +{{:​iot:​labs:​connected.png?​200|Button}} 
 + 
 +In the schematics, the button is connected to pull-up resistorthat means that the GPIO pin will read the value 1 if the button is pressed and 0 otherwise.
 ===== The Code ===== ===== The Code =====
-You go to the Wyliodrin Applications page and create ​a new application. you name it and select Streams as programming language.  +Create ​a new Wyliodrin STUDIO ​application ​and name it **traffic lights**. 
-{{ :iot:labs:rgb_code.png?350 |}} + 
-The most important thing to do is to choose the node that receives ​the value given by your sliders. This node connects your application ​to the dashboard by making ​it subscribe to signals ​to be received. Basically, each time signal ​is received, the code inside this node gets executedYou can see that the //receive ​signal// node also has two parameters: //​name// ​and //signal////Name// is of no difficulty, ​it represents ​the name of the node and //​signal// ​the name of the signal ​the application subscribes ​to.  ​As ​you can imply from the node's looksthere is no place in this node where you get a valueWhy is that? Because ​the value in the message received changes its value each time a new signal comes. The dashboard has to send the application ​value and this is how they interactthrough ​messages. ​You can think of this message as of container. The dashboard places ​the value insidethen it notifies ​the application, ​which gets the value from the container ​and can simply use it.+{{  :iot:labs:semaphore_button_streams.png?600  ​|Traffic Lights Applicatio ​}} 
 + 
 +The previous shows the nodes you need to use and how to connect them. Now let's see what each node does. 
 + 
 +First of all, notice ​the **semaphore** function node. This node is connected ​to the nodes controlling the GPIO pins connected to the LEDs and it outputs the values that need to be written on each pin. 
 + 
 +The figure below shows the code of the function. Basically, ​it verifies the value of the payload it received and depending on the value, it describes a certain state. The value 0 corresponds to the initial state when the cars lights have their green light turned on and the rest turned off and the pedestrian lights have the red light turned on and the green one turned off. This is why the function returns a list containing the values [0,​0,​1,​1,​0]. The block automatically knows to distribute ​each of these values in message on the corresponding output (the first value is transmitted on the first output, the second value on the second output and so on)As a result, ​the **digital write** nodes (red, yellow, green, ped-red and ped-greed) ​receive ​the values 0,​0,​1,​1 ​and respectively 0. 
 + 
 +{{  :​iot:​labs:​semaphore_function.png?​400 ​ |Semaphore function}} 
 + 
 +The **digital write** nodes have names according to the LED they control (the red node controls the red LED of the cars' semaphore and so on). Each node controls a pin by either setting ​it to HIGH or to LOW. The pin will be set to HIGH if the payload ​of the message is 1 and to LOW if it equals 0. For each node, you need to select ​the number ​of the pin they are controlling. For the **red** node you need to type the number 2 for the pin, as the LED representing the red light is connected to GPIO 2. 
 + 
 +Another important aspect are the two **run** nodes. The one directly connected to the **set 0** node will get triggered once the application start running and then it will send a message every 48 hours. For this, you need to double click the node and mark the **Fire once at start?**. Afterwardsyou also need to set the interval to every 48 hours. 
 + 
 +The node connected to **run**. **set 0** is a **change** ​node and it allows ​you to change the payload of the messageAs the first state of the system ​is 0, the payload is set to 0 and sent to the function. Basically, the run-set 0- function connection resets the traffic lights every 48 hours. All the other **set** nodes work the same, but they set some other values to the payload. 
 +{{  :​iot:​labs:​change_edit.png?​400 ​ |}} 
 + 
 +The other **run** node is connected to **digital read**, so each second ​a new reading is done. 
 + 
 +The **digital read** node reads the value coming from the button. You have to set the number of the pin the reading should be done from. In this case, the button is connected to pin 6, so go to the node's properties and set 6 for the pin.  
 + 
 +If the value read is 1 (HIGH), you need to change the states of the lights, otherwise you can simply ignore the reading. The node named **if button pressed** is a **switch** node that routes messages based on their properties. This specific node has only one output which will send further on any message that has the payload 1, basically it will act as filter which sends a message only when the button ​is pressed. The figure below depicts ​how to filter the values. 
 + 
 +{{  ​:iot:​labs:​if_button_pressed.png?​400 ​ |}} 
 + 
 +The **switch** node is then connected to a **trigger** node. This node creates two messages ​whenever a message arrives on the input and it outputs the messages separated by a distance specified in the propertiesFor this project the node allows you to automatically send message after 30 seconds from the pushed button so that the traffic lights system returns to its initial state. The first message has the payload 1 and the second has the payload 0, thus identifying the states the lights need to get to. 
 + 
 +{{  :​iot:​labs:​trigger.png?​200 ​ |Trigger node properties}} 
 + 
 +Further on, depending on the value the **trigger** node outputsthe **filter input** node will set the lights to the intermediate state and after 5 seconds (**delay** node) it will send another message with the value of final state stored in the payload. 
 + 
 +The **filter input** node is also a **switch** node which has two outputs, in this case. The first output is for the value 0 (triggered when the button has just been presses) ​and the second for the value 1 (triggered when the system needs to get back to its initial state). This way you can differentiate between the two.
  
-Once you have the value from the slider, you write it on the PWM pin, using the analog write function. +{{  :iot:labs:filter_input.png?​400 ​ |Filter input node properties}}
-Now consider the sliders. In their settings you have to name them and make sure their scale is from 0 to 255, as those are the values a PWM pin expects. +
-{{ :iot:labs:rgbsliders.png?400 |}} +
-Once you click the //Run the application//​ button, the project will get deployed on the board.+
  
 ===== Exercises ===== ===== Exercises =====
-  - Make a Streams application and use it to create a project that will light up a LED for 3 seconds at one press of a button.  +  - Use a button ​in the dashboard ​to control ​the traffic lights; 
-  - Do the same, using a switch from the dashboard ​instead of the button. +  - Display ​in the dashboard ​the state of the traffic lights using the **extra widget****Note:​** ​you can use the following pictures:  
-  - Connect a button that you will have to press in order to turn on an LED and press again to turn the LED off. +  * red light on: https://​dl.dropbox.com/​s/​6xl3vmpk8p4wgfc/​trafic-lights-red.png?​dl=0 
-  - The same, only that if you have pressed ​the button once, for the next two seconds, pressing again the button will have no effectIn a nutshell, once you press the button, it will become inactive for the next two seconds+  ​* yellow light on: https://​dl.dropbox.com/​s/​5mg4wecyi5xi6md/​trafic-lights-yellow.png?dl=0 
-  - The same, but using the switch from the dashboard instead of the button+  ​* green light on: https://​dl.dropbox.com/​s/​83d90kghg0d53k4/​trafic-lights-green.png?​dl=0 
-  -  ​Make a chat in Python with your neighbour'​boardYou read the messages from the keyboard in Python and the messages received will be shown in Python as well. Every time a new message comes, you turn on an LED for a few secondsThis part of the project should be made using streams nodes.+  * pedestrians red light on: https://​dl.dropbox.com/​s/​sjr52ohr8pxb9he/​pedestrian-lights-red.png?dl=0 
 +  * pedestrians green light on: https://dl.dropbox.com/​s/​m1ritf34ethvgt2/​pedestrian-lights-green.png?dl=0
iot/labs/04.1437640713.txt.gz · Last modified: 2015/07/23 11:38 by madalina.tanea
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0