Differences

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

Link to this comparison view

iot:labs:04 [2015/07/21 15:11]
madalina.tanea [The Setup]
iot:labs:04 [2017/09/11 09:35] (current)
alexandru.radovici created
Line 1: Line 1:
-===== Lab 04. Programming with Streams===== +====== Lab 4: Programming with Streams====== 
-==== Multicolor lamp ====+===== Traffic Lights ​===== 
 +===== What you need ===== 
 +  * One Raspberry Pi connected to Wyliodrin STUDIO; 
 +  * Five LEDs; 
 +  * One button; 
 +  * Six 200 Ohms resistors;​ 
 +  * Jumper wires; 
 +  * Breadboard.
  
-Let's replace ​the simple blinking lamp with multicolor ​one.+===== The Setup ===== 
 +Connect ​the LEDs and the button to the Raspberry Pi following the schematics in the figure. 
 +{{ :​iot:​labs:​traffic-lights.png?​600 |}} 
 +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 a measuring device in order to identify how the legs are connected. Once the button is pressed, all the legs get connected. 
 +{{:​iot:​labs:​connected.png?​200|Button}}
  
-The purpose of this project is to light up an RGB LED in different colors. ​In order to achieve thisyou need three PWM pins to control the LED and have say on each of its colors. To work with themyou can add three sliders in the Dashboard and, according on the value they send, to set the intensity of each color+In the schematicsthe button is connected ​to a pull-up resistorthat means that the GPIO pin will read the value 1 if the button is pressed and 0 otherwise. 
 +===== The Code ===== 
 +Create a new Wyliodrin STUDIO application and name it **traffic lights**.
  
-==== What you need ==== +{{  :​iot:​labs:​semaphore_button_streams.png?​600 ​ |Traffic Lights Applicatio }}
-   One Edison connected to Wyliodrin;​ +
-  *  One RGB LED; +
-  *  Three 200 Ohm resistors;​ +
-  *  Male-male jumper wires.+
  
-==== The Setup ==== +The previous shows the nodes you need to use and how to connect themNow let'​s ​see what each node does.
-Connect ​the RGB LED to the Edison following the schematics in the figure. +
-{{ :​iot:​labs:​rgbled.png?​400 |}} +
-The RGB LED is simply an LED consisting of three simple LEDs: a red one, a green one and a blue one. Each of these LEDs is controlled by a PWM pin, so that it can light brighter or dimmer. As a result, the color of the RGB LED will be the result of these three colors. +
-==== The Code ==== +
-You go to the Wyliodrin Applications page and create a new application. ​you name it and select Streams as programming language.  +
-{{ :​iot:​labs:​rgb_code.png?​300 |}} +
-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 a signal is received, the code inside this node gets executed. You 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 ​looks, there is no place in this node where you get a value. Why 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 a value and this is how they interact: through messages. You can think of this message as of a container. The dashboard places the value inside, then it notifies the application,​ which gets the value from the container and can simply use it.+
  
-Once you have the value from the slider, you write it on the PWM pinusing the analog write function. +First of allnotice ​the **semaphore** ​function ​nodeThis 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.
-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?​300 |}} +
-Once you click the //Run the application//​ button, ​the project will get deployed ​on the board.+
  
-==== Exercises ==== +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 ​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 ​list containing the values [0,0,1,1,0]The block automatically knows to distribute each of these values in a message on the corresponding output (the first value is transmitted on the first outputthe second value on the second output and so on). As result, ​the **digital write** nodes (red, yellow, green, ped-red and ped-greed) receive ​the values 0,0,1,1 and respectively 0
-  - Make a Streams application ​and use it to create ​project that will light up a LED for 3 seconds at one press of button + 
-  - Do the sameusing switch from the dashboard instead of the button+{{  :​iot:​labs:​semaphore_function.png?​400 ​ |Semaphore function}} 
-  ​- Connect a button that you will have to press in order to turn on an LED and press again to turn the LED off+ 
-  ​- ​The sameonly that if you have pressed ​the button ​once, for the next two secondspressing again the button ​will have no effect. In a nutshellonce you press the button, ​it will become inactive ​for the next two seconds+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
-  - The samebut using the switch ​from the dashboard instead of the button. + 
-  ​ Make chat in Python with your neighbour'​s boardYou read the messages ​from the keyboard in Python ​and the messages ​received will be shown in Python as wellEvery time new message ​comesyou turn on an LED for a few seconds. This part of the project should be made using streams nodes.+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 message. As the first state of the system is 0the 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 6so 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 a 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 **trigger** nodeThis 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 a 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 0thus 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 outputs, the **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. 
 + 
 +{{  :​iot:​labs:​filter_input.png?​400 ​ |Filter input node properties}} 
 + 
 +===== Exercises ===== 
 +  - Use a button in the dashboard to control the traffic lights; 
 +  - Display in the dashboard the state of the traffic lights ​using the **extra widget****Note:** you can use the following pictures:  
 +  * red light on: https://​dl.dropbox.com/​s/​6xl3vmpk8p4wgfc/​trafic-lights-red.png?​dl=0 
 +  * yellow light on: https://​dl.dropbox.com/​s/​5mg4wecyi5xi6md/​trafic-lights-yellow.png?​dl=0 
 +  * green light on: https://​dl.dropbox.com/​s/​83d90kghg0d53k4/​trafic-lights-green.png?​dl=0 
 +  * 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.1437480668.txt.gz · Last modified: 2015/07/21 15:11 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