# Differences

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

Link to this comparison view

iot:labs:05 [2017/09/11 09:34]
iot:labs:05 [2017/09/11 09:35] (current)
Line 1: Line 1:
-====== Lab 5: Linux ====== +====== Lab 5: Sensors and Dashboard======
-Embeded boards run Linux, this is why when interracting with them you might need some basic notions concerning this operating system in case you haven'​t come across it so far. +

-===== The Shell===== +For the following applications,​ you will read data coming from sensors, which means that you will also need to use the Arduino board. As the Raspberry Pi has no ADCs, you cannot read values coming from analog sensors.
-The Shell is a window that allows you to interact with the board. It waits for you to enter a command and it executes it. Once you open a Shell, you will see the prompt. That means everything works fine and the Shell is waiting for your command.+===== Arduino Setup =====

-The prompt also offers you some information. First of all, it shows you the user currently logged in. The user's name is that you see before //@//. It also shows the host name of your board. +==== What you need ====
-The most important information the prompt displays is the working directory. That is the directory where you are currently working in. It is displayed right after the colon in the prompt. You will notice that the default working directory is //~//. That is the user's home directory and its equivalent is ///​home/​username//​. +
-===== Paths =====+

-In order to access a certain file or directory, you have to take into account the path to it. There are two different paths you can use: absolute and relative.\\ +  * One Raspberry Pi board connected ​to Wyliodrin STUDIO;
-In Linux, the directories'​ structure ​is like tree. The root directory ​is ///// and it contains all the other directories ​and files.\\ +  * One Arduino board;
-If you use an absolute path to a file or a directory, that means that you build the path to it starting with the root directory. Thus, you can say that any path that starts with // / // is an absolute path.\\ +  * One USB cable
-On the other hand, you can use a relative path, which means that you build it starting ​from the directory you are working in, your working directory. Thus, all the files and directories are relative ​to it.\\ +==== The setup ====
-When building paths, there are three symbols you should be familiar with:+As previously stated, the classical Arduino board is basically ​micro-controller,​ which is capable of running one piece of software at once and that has little processing power and no network connectivitySo you will use the board to gather data from the environment ​and then pass it on to the Raspberry Pi

-  *  - current directory +The Raspberry Pi is a computer that is capable of processing data and communicating with other smart devicesFor instance, you could visualise the temperature on your smart phone.
-  * .. -parent directory +
-  * // ~ //  - home directory (/​home/​username)+

-===== pwd =====+The two boards need to be connected in order to send data between them. You can connect them via the USB cable and a serial connection will be established between the two. Once this is done, they can exchange data and the Arduino board can be controlled via the Raspberry Pi. This is done by the **firmata** protocol. The protocol allows the Raspberry Pi to send the Arduino messages in which it requests for a certain action or information and the Arduino will respond accordingly.

-The //pwd// command makes the Shell print the working directory. It is important to know which directory ​you are working in and sometimes it is difficult ​to get it from the prompt. So, anytime you feel lost, use //pwd//.+In order to implement ​the protocol, ​you need to flash the Arduino with the **StandardFirmata** firmware.

-===== ls =====+In the Wyliodrin STUDIO interface you have two tabs referring to **SOFTWARE** and **FIRMWARE**. So far, you used only the **SOFTWARE** tab, as you wrote applications for the Raspberry Pi solely. Now, select the **FIRMARE** tab and there is where you can write code which will be run on the Arduino. In this case, you will import an existing project.

-//ls// makes the Shell print all the files and directories located in the working directory. If you want to see la contents of some other directory, you can pass that directory as an argument to the command. For instance, if you want to print all the files and directories in /, you will write //ls / // .\\+{{ :iot:labs:select-firmata.png?300 |}}

-===== cd =====+Hit the **Show examples** button and select **Arduino**. Then **use** the **Firmata/​StandardFirmata** example. Now that you have the software to run on the Arduino, once you hit the **run** button, you will be asked the tyype of the Arduino board and if you want to flash it or not. Select the board you are using and **RUN AND FLASH**. The **StandardFirmata** firmware will be deployed on the Arduino.

-You already ​know that once you open a Shell, the working directory ​is your home directoryHowever, you will need to work in other directories too. In order to change the working directory, you will have to use // cd// followed by the directory ​you want to go to.\\ +As you know, any micro-controller,​ including the Arduino, ​once flashedruns the same firmware until another one is uploaded on the boardThus, you don'​t ​have to flash the board each time you run a new Raspberry Pi applicationIf you are confident ​that the Arduino is running **StandardFirmata**, you can skip this step.
-For example, if your home directory contains a directory called //​homework//​ and you want to have that as the working directory, you use //cd homework//. You can notice that you used an absolute pathSome other alternatives would be //cd /​home/​pi/​homework//​ or //cd ~/​homework//​. In the last two examples you used an absolute path to refer to //​homework//​ directory.\\+{{ :​iot:​labs:​flash-arduino.png?300 |}}

-===== cat=====+===== Street Lightning ​=====

-//cat// asks the Shell to print the contents of a file. Howeverit must be clear that you can only see its contentsyou cannot modify them. For that you need an editor.+In this application,​ you will build a system ​to monitor ​the light level and if there is the case, you will turn on the street lights brighter or dimmerdepending on the amount of light.

-Just like with the //cd// command, //cat// gets as an argument the file it should display.+==== What you need =====
+  * One Raspberry Pi connect to \Wyliodrin;​
+  * One Arduino connected to the Raspberry Pi;
+  * One photocell;​
+  * One LED;
+  * One 220 $\Omega$ resistor;
+  * One 10 k$\Omega$ resistor;
+  * Jumper wires

-Example: //cat /​etc/​passwd//​+==== The Setup ====

-===== htop =====+{{ :​iot:​labs:​light_sensor_schematics.png?​600 |}}

-By using the //htop// command you can see real-time all the processes that run on your board. Once you entered ​the command you will notice that the prompt does not appearthat is because you cannot enter another command until you are finished with displaying the processesSoif you want to go back to what you were doingjust hit the //q// key.+The photocell works just like resistor with a variable resistance. Depending ​on the amount of light it receivesits resistance gets higher or lowerAs a result, you can connect it in a voltage divideras well.

-{{ :​iot2015:​labs:​htop.png?​500 |}} +The sensor is connected to the 5V via the 10 K Ohms resistor ​and to the GND pin on the other side. In order to read the sensor'​s value, the yellow jumper wire is connected to the A0 pin of the board. The A0-A5 pins can be used for reading digital values, in this case values ranging from 0 to 1024
-For each process displayed, you can see its PID (Process ID), the user who launched ​the process, how much CPU and memory it is using, ​the command that started ​the process and other information.+

-What you are most interested in is the PID. That is because each process can be identified by its PID and if you want to interact ​with ityou have to know its process ID. +For the photocell, the resistance decreases proportionally ​with the amount of lightso for this schematics, the brighter the environment,​ the higher is the read value.
-===== kill =====+

-You know that you can use //htop// to find process'​ ID in order to be able to interact ​with it//​kill// ​is the command that allows us to interact with another process.+In addition, the photocell has resistance varying from hundred of ohms to mega ohms and in order for the voltage divider ​to work, you should use the sensor as a pull-down resistor, while the actual resistor needs to have a resistance comparable ​with the sensor'​s,​ otherwise its effect is not visibleThis is why you need a 10 k Ohms resistor.

-Two processes ​can interact ​by using signalsA signal is a number ​process sends to anotherBoth processes know that each number represents an action. ​you can refer to a signal either by the number ​or by its name. +The other part of the setup, the LED is connected similar to the previous schematics except that you can notice that its behaviour is controller ​by pin 3That pin has tilde next to itThat means that it is a PWM pin so you can control if the LED should light up brighter ​or dimmerYou can write values ranging from 0 to 255 on these pins, 0 being the equivalent of digital 0 and 255 the equivalent of digital 1
-{{ :​iot2015:​labs:​kill.png?500 |}}+==== The Code ====

-The format of the //kill// command is the following//kill -signal pid//, where signal is the number representing the action you want to process to do and pid is the process ID.+{{ :iot:​labs:​street_lights_code.png?400 |}}

-The two signals ​you are most interested ​in are //SIGTERM// (number 15) and //SIGKILL// (number 9)+First of all, you need to use an **Arduino ​in** node in order to read the values from the sensor. This node gets triggered each time the value on the pin it reads from changes. To actually configure the node's behavior you need to set its properties

-SIGTERM tells the process to stop its execution. Normally, the process should save all its data and stop running. However, this signal can be ignored by the process. There are times when you cannot kill a process by using SIGTERM.+{{ :​iot:​labs:​arduino_in_properties.png?400 |}}

-On the other hand, SIGKILL, kills the process no matter what. The downside is that the process does not have the opportunity ​to save its data, so killing ​it like this can result in loss of dataNeverthlessif something happened and your process must be forced ​to stop, you have to use SIGKILL.+The first property allows you to set the Raspberry Pi connection ​to the Arduino board so the Raspberry Pi knows the port through which it should send the messagesUsuallythe port is **/​dev/​ttyACM0**. However, in order to make surego to the **Shell** tab and type **dmesg**. There you should see messages stating that an FTDI cable has been connected ​to the board and the port it is connected to. Concatenate that value to **/dev/** and place the value as the first property of the node.

-In case the running process has a Shell attached ​and you can access it, you can simply ​use a key combination ​to send the SIGTERM signal ​to it and make it stop//​Ctrl+C// ​.+Further on, you need to specify which pin you want to read from. In this casethe pin is 0 and is an analog pin. Afterwards, you will use a **range** node in order to map the read values ​to values that should light up the LED. For thisyou need to adapt the system to your environment. Depending on the regular amount of light, usually you will read a value of around 400. So in this case, the range scales values going from 400 to 1024 to 0 to 255. However, if the regular value is different, you will need to scale a different range.

-===== killall =====+{{ :​iot:​labs:​range-light.png?​400 |}}

-//killall// has the same effect as //kill//except that you do not have to know the PID of the process, but its nameInstead of passing ​the process ID as an argument, you have to pass the process name.+The last node to use is **Arduino out**. This node acts just like the **digital write** node used in the previous chapters. However, you still need to select ​the port for the Arduino board and you can choose from analog or digital writeAs you want to make the LED light up brighter or dimmer, you will set the type to **analog** and the **pin** property has to be 3

-===== Tips & Tricks =====+<​note>​
+If you use more than one Arduino node in the application,​ add a new Raspberry Pi board only for one node. For the rest of the nodes, choose the previously added board from the drop-down.
+</​note>​

-Getting used to working with a Linux Shell is not difficult, especially if you know the following tricks: +Now you can run the application ​and watch how the LED gets brighter as you cover up the sensor
-  *  Whenever you are typing a command use the //TAB// key. It will auto complete what you wanted to type, thus eliminating spelling errors. In case there are multiple possibilities,​ press TAB once more and they will be displayed. If by pressing TAB the command or the argument ​you want to type is not automatically filled in, it means the command is not valid. +
-  * The most important command you should know is //man//. By using //man// followed by another command name, you have access to that command'​s manual and you can find how to use it and all it can do. +
-===== Exercises =====+

-  ​Create a new folder // Folder1 // and a file inside it //My file //. Create a new folder // Folder 2//. Move it inside ​the first folder and rename it to // Folderception//​. List the content of Folder1. Delete ​the first folder and its content ​using a single command. \\ **Hint** you can use the manual pages to find and understand the commandsAlso use apropos to find the needed command (eg. \$ apropos "move files" ) +===== Exercises=====
-  - Open a new project ​in Wyliodrin and run it. Find the process ​and its PID. Kill the process\\ **Hint** Some commands may need root permissions to runUsing //sudo// in the beginning of the respective command gives you the needed permissions+  ​Display ​the values coming from the light sensor in the dashboard ​using the **gauge** graph
+  - Connect all the sensors ​in the kit to the Arduino ​and display the values you read in the dashboard. **Note:** Do not connect the Hall sensor, which works in a different way.
+  - Search ​the temperature sensor'​s datasheet online (TMP36) and transform ​the read values to celsius degrees.