10
Making the IoT In this lab we will use the Raspberry Pi and the Arduino. We will go through the setup that is necessary in order to use them. After setting up the devices, we will implement some sample applications using both the Arduino and the Raspberry Pi boards which will communicate using HTTP and MQTT protocols. Raspberry Pi Setting Raspberry Pi can be used in Standalone Setup or in Headless Setup. In Standalone setup, peripheral devices are connected to the Pi as with standard desktop computers: Power Supply Monitor Mouse and Keyboard (through a USB hub) Connectivity (either wireless or ethernet) In Headless setup, the Pi interfaces with an external computer (e.g. a laptop) and a software, both in the Pi and in the laptop, is used for communication. In this lab we will load the Operating System into the Raspberry Pi with the raw image file. Then we will setup the device wireless interface in standalone mode so that the rest of the lab can be done in headless mode. Loading the OS Download the latest image file of Raspbian from https://www.raspberrypi.org/downloads/raspbian/ Download an SD formatting tool such as https://www.sdcard.org/downloads/formatter_4/ in case the SD needs to be formatted At this point, you cannot simply unzip the file onto an SD card. It won' t work as the image must be transferred in a very specific manner for it to properly boot and function as an OS. There is a free open-source program named Win32DiskImager that can be used on a Windows computer to transfer the unzipped image to a formatted SD card. This program is available from the sourceforge site at http://sourceforge.net/projects/win32diskimager/files/latest/download Connectivity setup Now, it is time to provide connectivity to the raspberry in order to be able to use it in headless mode. Connectivity can be provided with an

Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

  • Upload
    lydien

  • View
    213

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

Making the IoTIn this lab we will use the Raspberry Pi and the Arduino. We will go through the setup that is necessary in order to use them. After setting up the devices, we will implement some sample applications using both the Arduino and the Raspberry Pi boards which will communicate using HTTP and MQTT protocols.

Raspberry Pi SettingRaspberry Pi can be used in Standalone Setup or in Headless Setup.

In Standalone setup, peripheral devices are connected to the Pi as with standard desktop computers:

Power Supply Monitor Mouse and Keyboard (through a USB hub) Connectivity (either wireless or ethernet)

In Headless setup, the Pi interfaces with an external computer (e.g. a laptop) and a software, both in the Pi and in the laptop, is used for communication.

In this lab we will load the Operating System into the Raspberry Pi with the raw image file. Then we will setup the device wireless interface in standalone mode so that the rest of the lab can be done in headless mode.

Loading the OSDownload the latest image file of Raspbian from https://www.raspberrypi.org/downloads/raspbian/

Download an SD formatting tool such as https://www.sdcard.org/downloads/formatter_4/ in case the SD needs to be formatted

At this point, you cannot simply unzip the file onto an SD card. It won' t work as the image must be transferred in a very specific manner for it to properly boot and function as an OS. There is a free open-source program named Win32DiskImager that can be used on a Windows computer to transfer the unzipped image to a formatted SD card. This program is available from the sourceforge site at http://sourceforge.net/projects/win32diskimager/files/latest/download

Connectivity setupNow, it is time to provide connectivity to the raspberry in order to be able to use it in headless mode. Connectivity can be provided with an ethernet cable or using wifi. While ethernet does not need any expicit setting, wifi needs to be set manually changing the interface file in the /etc/wpa_supplicant path or from the GUI desktop as usually done with a normal laptop. Plug a keyboard, mouse and the screen using the USB and HDMI ports to setup the wireless interface in this last manner.

Then, go to the upper right corner and select the desired Access Point. Put the password of the Wifi. Once connected you will be able to see the address associated with the Wifi interface just opening a terminal and typing the "ifconfig" command. The IP address is the one of wlan0 under the name "inet addr".

Now the device is reachable from another computer. Therefore you can unplug peripheral devices. To use the Raspberry Pi in headless mode, what you need to do is to attach another computer to your network running a program that can connect to the Pi and remotely run it. Such a program is PuTTY and is freely available for download at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Page 2: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

PuTTY uses the SSH protocol (port 22) to communicate with the Pi. You will need to put the address previously gathered in standalone setup.

Once username and password are entered (username:"pi" password:"raspberry"), you will be able to interact with the Pi in exactly the same way as if you were sitting in front of a standalone workstation.

The one major limitation with the SSH protocol is that it is text only and you cannot open a GUI desktop. In the next subsection we will see how to overcome this problem.

Headless Operation with GraphicsA virtual networking connection (VNC) server named tightvncserver exists, which can be installed and functions in a similar manner to SSH except it handles both text and graphics. Type the following command to install it into the Pi:

>> sudo apt-get install xrdp

The VNC server is started by entering the following at the command line:

>> vncserver

Every time you start the VNC server, you will see the following line:

New ‘X’ desktop is raspberrypi:x

where the lowercase x represents a number. The first time you start, it should be a 1. You need to remember this number as it is an important parameter when you run the client on the remote computer. Also, at the first startup, you will be prompted to enter a password that can be up to eight characters in length. You will need to input this password when you authenticate the remote computer with the Pi. Now we have all that is required on the Pi (or server) side; it is time to focus on the client side.

You will need to download a free VNC suite from http://tightvnc.com/download.php. This download includes both server and client VNC packages, but only the client package is needed for this configuration. The website has two Windows installers (.msi files), one for 32-bit machines and another for 64-bit machines. Select the appropriate one for your computer and install it.

Once installation has finished, open the TightVNC Viewer menu item. Enter your Pi's IP address in the Remote Host text box. Also append a colon with the number that you saw when you started the Pi' s VNC server (e.g.":1").

Then click the Connect button next to the text box. If everything goes smoothly, you should see the Pi's VNC server authentication dialog box appear. Enter the password you created when you first configured the Pi' s VNC server. Click OK, and you should see the classic Raspbian GUI desktop.

Installing Mosquitto on the Raspberry Pi Now that our Raspberry is ready for use, we can install the necessary packets for remote communication. First of all we will install Mosquitto, a light and easy-to-use program for MQTT communication using the command line.

Type the following command on the terminal

>> sudo apt-get install mosquitto-clients python-mosquitto

This will install the mosquitto client. If you want to use also the mosquitto server type >> sudo apt-get install mosquitto

Page 3: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

You can now try to subscribe and publish messages from the command line with the following simple commands

Example: >> mosquitto_sub -h test.mosquitto.org -t "CMOB/test">> mosquitto_pub -h test.mosquitto.org -t "CMOB/test" -m "test_message"

For more information about publishing and subscribing using mosquitto commands visithttp://mosquitto.org/man/mosquitto_pub-1.htmlandhttp://mosquitto.org/man/mosquitto_sub-1.html

Installing pahoApart from executing MQTT commands from the terminal, it is possible to write programs to establish MQTT communications. However, Mosquitto libraries in Python have been donated to the Eclipse Paho project.

Therefore, to use those libraries it is necessary to install some extra packages. First of all, we must have pip, a tool to install python modules.

>> sudo apt-get install python-pip

Now the paho-mqtt package can be installed

>> sudo pip install paho-mqtt

GROUPS WITH ARDUINOIn this part of the lab, we will make a simple light system application on the Arduino and we will use the serial port in order to make the Arduino communicate with the Raspberry Pi and with the Internet. Without a shield, Arduino Mega 2560 does not have network connectivity. Therefore we are using the Raspberry Pi as a gateway.

Installing Arduino IDEYou can download and install the IDE on your laptop from https://www.arduino.cc/en/Main/Software

Once installed, plug the Arduino to your laptop using the USB cable

On Tools, select "Arduino Mega 2560"

Select the port to which the Arduino is connected

YOU CAN ALSO INSTALL THE ARDUINO IDE IN THE RASPBERRY PI WITH

>> sudo apt-get install arduino

Page 4: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

However, for the moment we will write our Arduino application on the laptop

Making a light system in Arduino - Hardware partPlace the shield of the Grove Starter Kit. Then connect the LED block to the Digital Port 2 (D2).

Connect the light sensor and the knob respectively to Port A1 and A3.

Connect the Arduino with the laptop using the USB cable.

Making a light system in Arduino - Software part

Both the knob and the light sensor can vary from 0 to 1023. You should see the values read from the sensors on the Serial Monitor (go to: "Tools > Serial Monitor" on the main Arduino window). Moreover, you should see the LED switch on if the value set by the knob is below the value sensed by the light sensor.

Connecting the Arduino and Raspberry using the USB interfaceIn order to read from the serial interface of the Arduino (USB) and print that information you can use the following lines of python code on your Raspberry

Page 5: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

Sending Light System Data received from the Arduino to an MQTT broker using Raspberry as the gatewayData gathered from the Arduino via the serial port, can be sent to an MQTT broker with the following lines of code

Sending a command to the ArduinoIn order to enable the Arduino to listen to commands coming from the serial interface, add the following lines in the space dedicated to variable declaration (at the start of the Arduino file)

then change the delay line into

and add the following function which listens for numbers from the serial port

Page 6: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

To change the delay of the data acquisition, type a number in the command line of the Arduino serial monitor in the Raspberry (delay in milliseconds) followed by a dot (or whatever no numeric digit).

You can also put you command in the python program of the Raspberry Pi using the ser.write() method which passes a string.

GROUPS WITHOUT ARDUINOUsing the Python library for the GPIO to control a LED with a button

Now, we will try to control a LED on a breadboard connected to the Raspberry Pi.

To do so, connect in series PIN18, the LED and the RESISTOR (or the RESISTOR and the LED) and the GND, as shown in the Figure below.

Then with python open (using "sudo python") write "import RPi.GPIO as GPIO" and press enter

Page 7: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

Set the mode of the GPIO writing "GPIO.setmode(GPIO.BCM)" and press enter

Set PIN 18 as an output by writing "GPIO.setup (18, GPIO.OUT)"

You can now turn on and off the LED with the commands "GPIO.output(18,True)" and " GPIO.output(18,False)"

It is also possible to write a .py program to execute the same set of instructions.

#!/usr/bin/python

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(18,GPIO.OUT)

GPIO.output(18, True)

Using a temperature and humidity sensorTo use the humidity and temperature sensor connect it as in the figure. If you have any doubts, ask the teacher.

Now we need to install some libraries in order to use is

A reboot is necessary, in order for the drivers to properly work

Page 8: Raspberry Pi Setting - Home - people.unica.it - …people.unica.it/michelenitti/files/2012/04/MakingTheIoT.docx · Web viewYou should see the values read from the sensors on the Serial

Now the following lines can be written in order to read the temperature and the humidity values from the sensor

Installing flask to use the Raspberry Pi as an HTTP server

To use the Raspberry Pi as an HTTP server, install

>> sudo apt-get install python-pip>> sudo pip install flask

Now, we will write a program which uses flask to make the Raspberry an HTTP server. The foolowing program will return the temperature and humidity values sensed if a GET request is made. If a POST request is made at the same address, the status of the LED on the breadboard will be changed and a message will be returned to the HTTP client.

To make both GET and POST requests, you can install postman.