MQTT: MQTT is described as a machine-to-machine(M2M)/IoT connectivity protocol. MQTT is so lightweight that it can be supported by some of the smallest measuring and monitoring devices, and it can transmit data over far reaching, sometimes intermittent networks. MQTT is a publish/subscribe messaging transport protocol that is optimized to connect physical world devices and events with enterprise servers and other consumers. MQTT is designed to overcome the challenges of connecting the rapidly expanding physical world of sensors, actuators, phones, and tablets with established software processing technologies. These principles also turn out to make this protocol ideal for the emerging M2M or IoT world of connected devices where bandwidth and battery power are at a premium. Web Links: Following are the web links which are references for tutorial related to usage and software of MQTT protocol along with ESP8266 and Raspberry Pi. http://www.penninkhof.com/2015/05/linking-the-esp8266-a-raspberry-pi-through-mqtt/ http://iot-playground.com/blog/2-uncategorised/76-easyiot-cloud-mqtt-api http://iot-playground.com/build http://mqtt.org/tag/arduino https://github.com/tuanpmt/esp_mqtt http://tech.scargill.net/mqtt-on-arduino/ https://github.com/tuanpmt/espduino http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/?ALLSTEPS http://www.makeuseof.com/tag/meet-arduino-killer-esp8266/ http://blog.thingstud.io/recipes/how-to-make-your-raspberry-pi-the-ultimate-iot-hub/ https://www.ibm.com/developerworks/community/blogs/5things/entry/5_things_to_know_about_mqtt_the_protocol_for_internet_of_things?lang=en
Equipment: Arduino LED Procedure: Connect Arduino to the GUI interface from a windows system. This configuration is initially done and all the required settings are made. Connect anode of LED to Ground and cathode to Input pin in Arduino D7(PIN NO : 13). This initial set ups is to be made. Then we can use in build in program to blink LED. Go to File and then select Examples. Here different kinds of examples are given, we can use them directly or modify according to our use. Go to Basics folder there select Blink file. This will open a new window with blink program. Check if the port number to which LED is connected is correctly give in the file or not. If not change it to correct port number. Upload the program. This is the process of writing program to Arduino. This will put Blink file to Arduino. This will take quite long time to load. Once the program is being written to Arduino we can observe blinking light. After that the program is loaded/written to Arduino LED starts blinking. LED will blink with time lapse mentioned in Blink file. This time lapse can be changed and blinking speed can be varied. Video:
Experiment 2: DHT11 with Arduino Equipment: Arduino DHT11 Resistor Breadboard Connectors Procedure: DHT11 is a sensor used for measuring Temperature and Humidity. I have used this with Arduino and measured temperature and humidity and stored results. Connect DHT11on breadboard. Now connect ground pin of DHT11 to Arduino ground. Data pin is connected one of the IO pins of Arduino. Other pin of DHT11 is connected to one end of resistor. Other end of resistor is connected to power supply of 3.3v. This is the required connection set up. Then we have to open file. Go to file in Arduino GUI and select examples. Then select DHT Sensor Library and select file DHTtester. Upload this file into Arduino. This will start taking temperature and humidity values from DHT11 and report in Arduino GUI. To see result we have to open window from Tools->Serial Plotter. In this window we can see temperature and humidity values. Picture:
Results:
Code: DHTtester from DHT Sensor Library in Examples Reference: http://playground.arduino.cc/Main/DHT11Lib
Experiment 3: Raspberry Pi setup with Arduino Raspberry Pi can be connected to Arduino and operated. This will be same as connecting Arduino to a windows system. This is simple process to do. Initially we install Arduino GUI in Raspberry Pi by following the command: sudo apt-get install Arduino This will install Arduino interface in Raspberry Pi. In addition to this I have installed packages which will be useful for programing with Arduino. sudo pip install nanpy sudo pip install pyserial I tried to operate LED light with program from Raspberry Pi but I faced many problems. I read many pages and blogs and finally found that Arduino which I am working on is not a supporter of the latest version of the software. I was not able to continue forward and operate LED with Arduino. I did not find a proper solution for this problem. I am giving links of all the pages which I referred regarding this problem. References: https://www.raspberrypi.org/blog/raspberry-pi-and-arduino/ http://www.akeric.com/blog/?p=2420 https://www.youtube.com/watch?v=Jw06KLLkapU Experiment 4: Multiple LED This is the experiment in which multiple LED are connected and each light will blink one after other. Looping is done and so all the lights will glow continuously till the program is terminated on Arduino. For this experiment i have used Arrays program file from experiments in Arduino. Equipment: Arduino Breadboard 6 LED 6 Resistors Connectors Procedure: Connect all the 6 lights to one resistor each as shown below in the video. Each LED will be connected to resistor at one end and ground at other end. Now each resistor is connected to different IO pins in Arduino. We make use of IO pins and program the logic for blinking LED. One light will blink after other. After making connections open the file Arrays from Controls in Examples from file in Arduino GUI. This will pop up a new window. This contains multiple lights blink program. Check the IO pin numbers for which each resistor is connected and make changes in port numbers in the opened file. I needed we can change blinking time for the LED. After all the required changes are done Upload the program. This will operate LED on breadboard. As a result we can see 6 blinking LED. Video:
Code: Arrays program file from Controls in Examples. Experiment 5: LED control with Button In this experiment I tried controlling operations of LED with inputs from push button. Equipment: Arduino LED Push Button Breadboard Connectors Procedure: Connect one button on breadboard. To one pin of button connect resistor. To other end of resistor connect it to IO pin (D4). Connect other pin from button to ground. Below is the circuit of connection and how it works. Once the file is written to Arduino LED will not glow. When the button is pushed down it is taken as input and the mode of LED is changed from LOW to HIGH. Then LED starts glowing. This works as interaction with user through button and operating LED from user input.
Video:
Code: int input=2; // to store on or off value void setup() { // put your setup code here, to run once: pinMode(2,INPUT); pinMode(13,OUTPUT); } void loop() { // put your main code here, to run repeatedly: input=digitalRead(2); if(input==0) { digitalWrite(13,HIGH); } else { digitalWrite(13,LOW); }
Smart Cities Internet availability to people and connectivity to electronic devices and sensors can make many things happen in a simple way. Internet of Things is all about doing that. Internet of Things plays a major role in transforming into smart cities at present and in future. Implementing IoT-driven services improves the quality of life of people through measures that promote an eco-friendly, sustainable environment. IOT-driven services gather large amount of data and this data is processed, trends are derived and resources are better used in all the ways. Real time data is most of the times associated with privacy and security implications. A smart city is in which there are necessary intelligent, robust and reliable functions to integrate and synthesize these big data and filter out any unwanted 'noise' for the purpose of improving city efficiency, equity, sustainability and quality of life. I want to investigate on projects taken up by Cisco in tie up with Barcelona. Cisco implemented different projects like smart bus stops, information on the flow of citizens, wirelessly-connected garbage bins. How all these projects are related to improve quality of people's life? I want to find out in what ways the current projects can be extended so that more facilities can be provided to citizens. What are the other inter-related projects that can be taken up to uplift the living quality? How eco-friendly environment can be maintained. What can be made to maintain balance in traffic on roads? What optimization can be done so that people can lead a high quality life?
Temperature and Humidity Measure: To measure temperature and humidity I used DHT11 Sensor. DHT11 measures both temperature and humidity and gives results. Basic structure of DHT11 looks like below:
By making use of DHT11 we can get Temperature and Humidity values to Raspberry Pi and can display, store, make operations on these values. To use DHT11 with Raspberry Pi we have to install packages which can connect to sensor and report values to Pi. These packages are installed using following commands: Initially we need to install dependency packages into Pi. Commands for these are: sudo apt-get update sudo apt-get install build-essential python-dev python-openssl sudo python setup.py install git clone https://github.com/adafruit/Adafruit_Python_DHT.git Then we go the installed folder using the command: cd Adafruit_Python_DHT Go to example folder and run the sample program to obtain temperature and humidity values. cd examples To run program: sudo ./AdafruitDHT.py 2302 4 Experiment 1: Light alert on low temperature This acts as a temperature alert system at your house. If you want to get alerts about low temperature you can use this. If the room temperature goes down than what you would like to be then the alert light glows and you can set the room temperature as you like. Equipment: DHT11 sensor Raspberry Pi Beadboard resistor connectors Connect the DHT11 to Raspberry Pi as shown below. By using the DHT package we installed in python we can monitor temperature and humidity values and prepare alert system. Connect 2 different color LED lights in the circuit. One will be glowing for the range above normal temperature and the other glows when temperature comes below normal temperature. Values will be reported continuously and the alert light glows immediately when the temperature falls down.
Circuit Diagram:
Experiment 2: Display Temperature and Humidity values on LCD In this experiment we will display temperature and humidity values on Nokia LCD display. For this we need to install Adafruit_Nokia_LCD package to monitor LCD display using Raspberry Pi using python. Follow the below commands to install package: Initially we should install dependencies using these commands : sudo apt-get install python-pip python-dev build-essential sudo pip install RPi.GPIO Then we should install image display package : sudo apt-get install python-imaging Now installing LCD display library sudo apt-get install git git clone https://github.com/adafruit/Adafruit_Nokia_LCD.git (http://adafru.it/dvC) cd Adafruit_Nokia_LCD sudo python setup.py install We have to follow below steps to enable SPI for Raspberry Pi: Click on Application Launcher a window will open Type command sudo apt-get install python3-dev After installation give following command: git clone https://github.com/doceme/py-spidev Go to folder spidev cd py-spidev Install setup using following command: sudo python3 setup.py install These steps are to enable SPI pins in Raspberry Pi : sudo raspi-config Window will be opened then "SPI" option should be selected. Then select "Yes" for all the steps nad after all the steps select "Finish" option. Then reboot system usein command : sudo reboot After completing this process SPI pins will be activated and LCD can be connected to Raspberry Pi and used. Equipment: Raspberry Pi Nokia 5110/3310 LCD DHT11 Connectors Using the Experiment 1 connections and add additional connections for the LCD and connect it to SPI pins of Raspberry Pi. Connect all SPI pins and make sure that correct pin numbers are used in python script for giving output to LCD.
References: https://learn.adafruit.com/downloads/pdf/nokia-5110-3310-lcd-python-library.pdf http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/ Code for Experiment-1 and Experiment-2: import sys import RPi.GPIO import Adafruit_DHT import time RPi.GPIO.setmode(RPi.GPIO.BCM) RPi.GPIO.setup(2,RPi.GPIO.OUT) RPi.GPIO.setup(3,RPi.GPIO.OUT) RPi.GPIO.output(3,False) RPi.GPIO.output(2,False) import Adafruit_Nokia_LCD as LCD import Adafruit_GPIO.SPI as SPI import Image import ImageDraw import ImageFont # Raspberry Pi hardware SPI config: DC = 23 RST = 24 SPI_PORT = 0 SPI_DEVICE = 0 # Hardware SPI usage: disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))
# Get drawing object to draw on image. draw = ImageDraw.Draw(image) # Draw a white filled box to clear the image. draw.rectangle((0,0,LCD.LCDWIDTH,LCD.LCDHEIGHT), outline=255, fill=255) # Load default font. font = ImageFont.load_default() while(True): sensor_args = { '11': Adafruit_DHT.DHT11, '22': Adafruit_DHT.DHT22, '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#' print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4' sys.exit(1) humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print 'Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity) draw.text((1,30), 'Temp={0:0.1f}*'.format(temperature/100), font=font) draw.text((1,1), 'Humid={0:0.1f}%'.format(humidity/100), font=font) # Display image. disp.image(image) disp.display() if temperature > 450 : RPi.GPIO.output(3,False) RPi.GPIO.output(2,True) else: RPi.GPIO.output(2,False) RPi.GPIO.output(3,True) time.sleep(10) disp.clear() disp.display() else: print 'Failed to get reading. Try again!'
sys.exit(1)
Experiment 3: Updating Temperature and Humidity sensor in Google sheets We can take temperature and humidity values from DHT11 and continuously update and maintain records in constant time lapse. These values are updated into google spreadsheet using python libraries. Create google spread sheet with name DHT Humidity Logs and delete all the empty rows from it. Go to google developers console and create new project. Then we need to get credentials for the spread sheet which we will be using in the script to connect to spreadsheet. A JSON file will be downloaded after select 'Enable and Manage APIs', under 'Google Apps APIs' and then select 'Drive API' and select 'Enable API' then select 'Go to Credentials' from the drop down select 'Compute Engine default service account' this will automatically download JSON file. Take client email from JSON file and share spreadsheet with that emailid. We need python packages to connect Raspberry Pi to google spread sheet. Follow below commands to install packages: sudo apt-get update sudo apt-get install python-pip sudo pip install gspread oauth2client goto cd Adafruit_Python_DHT cd examples We can find google_spreadsheet.py in this folder. Enter correct port numbers in this file and run the python script. Make sure that JSON file which is downloaded is in same folder. This will read temperature and pressure from the DHT11 sensor and update values in google spreadsheet along with current date and time.
Serial Peripheral Interface:
The Serial Peripheral Interface (SPI) is a synchronous serial communication interface specification used for short distance communication. This prototype is used in Raspberry Pi for communicating with hardware devices to take input or to give output. SPI on Raspberry Pi is associated with pin numbers 19, 21, 23, 24, 25, 26 on the P1 header. Names of each pin are as specified below:
P1-19 MOSI - Master Out Slave In
P1-21 MISO - Master In Slave Out
P1-23 SCLK - Serial CLocK
P1-25 GND - Ground
P1-24 CE0 - Chip Enable (often called Chip Select)
P1-26 CE1 - Chip Enable
By default SPI mode is off on Raspberry Pi. To enable it remove the blacklisting for spi-bcm2708 in /etc/modprobe.d/raspi-blacklist.conf
Different protocols used in SPI are:
MOSI - Master Out Slave In
MISO - Master In Slave Out
MOMI - Master Out Master In
MIMO - Master In Master Out
There are multiple nodes available in SPI they are:
Standard Mode:
In Standard SPI master mode the peripheral implements the standard 3 wire serial protocol (SCLK, MOSI and MISO).
BIDIRECTIONAL MODE:
In bidirectional SPI master mode the same SPI standard is implemented except that a single wire is used for data (MIMO) instead of two as in standard mode (MISO and MOSI).
LOSSI MODE (LOW SPEED SERIAL INTERFACE):
This is used for communication with LCD devices to give input to the LCD.LoSSI commands and parameters are 8 bits long, but an extra bit is used to indicate whether the byte is a command or parameter/data. This extra bit is set high for a data and low for a command. The resulting 9-bit value is serialized to the output. LoSSI is commonly used with MIPI DBI type C compatible LCD controllers. Some commands trigger an automatic read by the SPI controller, so this mode can't be used as a multipurpose 9-bit SPI.
Different types of TRANSFER MODES are:
Polled
Interrupt
DMA
SPEED:
The CDIV (Clock Divider) field of the CLK register sets the SPI clock speed: SCLK = Core Clock / CDIV
CHIP SELECT
Setup and Hold times related to the automatic assertion and de-assertion of the CS lines when operating in DMA mode are as follows:
The CS line will be asserted at least 3 core clock cycles before the msb of the first byte of the transfer.
The CS line will be de-asserted no earlier than 1 core clock cycle after the trailing edge of the final clock pulse.
NOOBS OS installation in Raspberry Pi: I will explain my first experience with Raspberry Pi. I got my kit, opened it and took out all the different things from box. I understood most of things that are included in the box and their functionality. I started reading first 2 chapters from users Manuel understood history, purpose, basic organization, usage of Raspberry Pi. Installed NOOBS operating system in Raspberry Pi. Procedure for installation: Equipment: Raspberry Pi kit HDMI Monitor Keyboard and Mouse SD card with NOOBS OS Procedure: Connect the Raspberry Pi kit to TV/Monitor using HDMI cable, insert SD card into the kit. Connect power cable and turn it on. Screen will be displayed with options, select first option which is Raspbian for NOOBS os installation. It will take a while for installing os, after 100% procedure is completed it will display Finish message on screen. Enter startx command and give username and password to get GUI for current os. This will take you to Graphical User Interface which means installation is completed. How to connect to WIFI??? Connect WIFI dongle to Raspberry Pi. You can see the network connectivity symbol on the right top of the screen. Select the option, we will get the list of all the available connections, connect to WIFI by entering the password. Remote desktop connection for Raspberry Pi: I used very interesting and simple protocol "XRDP" for remote desktop connection. Install package in Raspberry Pi by following command: sudo apt-get install xrdp First we need to find Physical address of Raspberry Pi so that we can find out IP Address from windows system and connect to Raspberry Pi using remote desktop connection. We can search IP address for corresponding Physical address using command: arp -a Open Remote desktop connection from windows and enter IP address obtained using Physical address. This will take you to login page where you have to enter username and password this will take to Raspberry Pi GUI on the windows system.
Experiment 1: Standalone light and Blinking light Equipment: Raspberry Pi kit 1 LED 1 330 ohms Resistor Connectors Breadboard Connect the T board on the breadboard and make connection in such a way that LED, resistor, Raspberry Pi are all connected in series. Pic below shows all the connections. Used GPIO2 and Ground pins for connecting LED output pin and the other circuit. I followed instructions from chapter 7 and made connections. This is very easy to do the setup. I did this successfully, finally I was able to programmatically make the light blinking.
Experiment 2: 3 LED lights whose frequency of blinking can be changed by push down button Equipment: Raspberry Pi kit 3 LED 3 330 ohms resistor Push down button Connectors Breadboard Connect 3 LEDs with same GPIO and different resistors, connect one push down button to the breadboard. In this experiment we will read input from button and then change the blinking frequency of the lights. This is a process of taking input from user and changing the frequency of output. By default LED will blink at a frequency of 0.25 seconds. When the button is pushed down then the blinking frequency will increase to 2 seconds. Till you keep pressing the button output will remain same. Once the button is released again the frequency changes to 0.25 seconds. This remains blinking until the program is interrupted. Attached code of project.
In this nature surrounding us, the moment which happens elsewhere decides the fate of the another moment elsewhere. Everything is interlinked. So do happen everything in internet of things. You are in your workplace operating light or fan at your house, decide your route before you start to place based on time and distance, water your plant on time wherever you are. All these things explain how versatile Internet of Things could be. I have great interest in developing application which have real world interaction. Internet of Things gives me an opportunity to learn and explore how useful applications could be made. I am curiously looking forward to learn programming with sensors and develope an application which I can use in daily life. It's fascinating to learn how cloud server could be built and manage data using Raspberry Pi and Arduino. I will be posting my assignments and projects in this blog for course "CSC-9010" "Internet of Things" at Villanova University under the guidance of professor Dr. Paul Schragger.