Monday, February 22, 2016

Communication with Pi via MQTT using Arduino

Experiment: Communication with Pi via MQTT using Arduino 


1. Installed all the required packages: SPI, PubSubClient
2. Tested the example program 
It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off.
3.Tried communicating with pi by using it as the server.
4. Read the messages in from the topic "outTopic" using mosquitto.


Code: https://github.com/jyothi-lanka/IoT/blob/master/mqtt/mqtt_esp8266.ino


Video:





Whom I helped:

Helped my classmates( Hema, Prashant, Sandeep) in
1. Trying to install packages.
2. Working on the Arduino programming to connect to the MQTT server.
3. Solving the issues during the connection.


Who helped me:

Prashant helped me in connecting to the Pi using the Arduino programming and also using mosquitto.


References:http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2mio


Friday, February 19, 2016

Node-RED

Node-RED is a visual tool for wiring the Internet of Things - hardware devices, APIs and online services in new and interesting ways.


INSTALL Node-RED


sudo apt-get update
sudo apt-get install nodered

To start Node-RED, you can either:

on the Desktop, select Menu -> Programming -> Node-RED.
or run node-red-start in a new terminal window.
To stop Node-RED, run the command node-red-stop


Adding nodes


To add additional nodes you must first install the npm tool, as it is not included in the default installation. The following commands install npm and then upgrade it to the latest 2.x version.

sudo apt-get install npm
sudo npm install -g npm@2.x
cd ~/.node-red
npm install node-red-{example node name}

Extra Nodes


To install extra nodes make sure you are in your user-directory, by default this is ~/.node-red.

For example the Pibrella node can be installed as follows

npm install node-red-node-pibrella

You then need to stop and restart Node-RED to load the new nodes, and then refresh the flow editor page in the browser.

node-red-stop
node-red-start


This guide will help you get Node-RED installed and running in just a few minutes.
  1. Installation
  2. Upgrading
  3. Running
  4. Creating your first flow
  5. Creating your second flow

Monday, February 15, 2016

MQTT Tutorial


MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. MQTT(formerly MQ Telemetry Transport) is an ISO standard publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited.The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message. It is also ideal for mobile applications because of its small size, low power usage, minimised data packets, and efficient distribution of information to one or many receivers.


Links:


https://www.youtube.com/watch?v=exMm-fmU5ck
https://www.youtube.com/watch?v=1GbYkCrbChw
https://www.youtube.com/watch?v=r6HEQVhgnP8
http://www.penninkhof.com/2015/05/linking-the-esp8266-a-raspberry-pi-through-mqtt/
https://www.youtube.com/watch?v=vVRy-WxNkes
https://www.youtube.com/watch?v=ae1QfoXPGQ8

Pi as WiFi Access Point


Have your ever imagine that is it possible to set up your own access point?
Don't worry. Yes, we can set up our own adorable WiFi router using Raspberry Pi.

Here in this experiment, I'm trying my hand to set up Pi as an access point more advanced than using it as a client. This tutorial will help to make it so the Pi broadcasts a WiFi service and then routes internet traffic to an Ethernet cable. Since it's all Linux we can go in and update or configure it however we like.


Things Required:


1. Raspberry Pi with Raspbian OS
2. Ethernet Cable
3. WiFi Adapter

First make sure to have Raspbian installed in Pi.
Then connect to Pi using putty.
Shutdown the Pi and plug in the WiFi module when the Pi is off.

Software Installation:


Make sure the Ethernet connection is up to have internet access to the Pi.

Install software onto Pi that will act as host access point.

sudo apt-get update
sudo apt-get install hostapd isc-dhcp-server

Setup DHCP Server


Next we will edit /etc/dhcp/dhcpd.conf, a file that sets up our DHCP server - this allows wifi connections to automatically get IP addresses, DNS, etc.

Run this command to edit the file

 sudo nano /etc/dhcp/dhcpd.conf

Comment the below lines by adding # at the beginning

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

uncomment the line below by removing #
#authoritative;

Then scroll down to the bottom and add the following lines

subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Save the file by typing in Control-X then Y then return

Run  sudo nano /etc/default/isc-dhcp-server

and scroll down to INTERFACES="" and update it to say INTERFACES="wlan0" 

close and save the file

Set up wlan0 for static IP:


If you happen to have wlan0 active because you set it up, run
sudo ifdown wlan0

Next we will set up the wlan0 connection to be static and incoming. Run
sudo nano /etc/network/interfaces to edit the file

Find the line auto wlan0 and comment all the lines from it by adding # at the beginning of each line.
Basically, just remove any old wlan0 configuration settings, we'll be changing them up.

Add the lines

  iface wlan0 inet static
  address 192.168.42.1
  netmask 255.255.255.0

Save the file (Control-X Y

Assign a static IP address to the wifi adapter by running
sudo ifconfig wlan0 192.168.42.1

Configure Access Point


Now we can configure the access point details. We will set up a password-protected network so only people with the password can connect.

Create a new file by running sudo nano /etc/hostapd/hostapd.conf

Paste the following in, you can change the text after ssid= to another name, that will be the network broadcast name. The password can be changed with the text after wpa_passphrase=

interface=wlan0
driver=rtl871xdrv
ssid=Pi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Now we will tell the Pi where to find this configuration file. Run sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF="" and edit it so it says DAEMON_CONF="/etc/hostapd/hostapd.conf"

Then save the file

Configure Network Address Translation


Setting up NAT will allow multiple clients to connect to the WiFi and have all the data 'tunneled' through the single Ethernet IP. (But you should do it even if only one client is going to connect)

Run sudo nano /etc/sysctl.conf

Scroll to the bottom and add
 net.ipv4.ip_forward=1

on a new line. Save the file. This will start IP forwarding on boot up

Also run

 sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

to activate it immediately


Run the following commands to create the network translation between the ethernet port eth0 and the wifi port wlan0

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

You can check to see whats in the tables with

sudo iptables -t nat -S
sudo iptables -S

To make this happen on reboot (so you don't have to type it every time) run

 sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

run sudo nano /etc/network/interfaces and add
 up iptables-restore < /etc/iptables.ipv4.nat
to the very end

Update hostapd


Before we can run the access point software, we have to update it to a version that supports the WiFi adapter.
First get the new version by typing in

wget http://adafruit-download.s3.amazonaws.com/adafruit_hostapd_14128.zip

to download the new version (check the next section for how to compile your own updated hostapd) then

unzip adafruit_hostapd_14128.zip

to uncompress it. Move the old version out of the way with

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG

And move the new version back with

sudo mv hostapd /usr/sbin

set it up so its valid to run with

sudo chmod 755 /usr/sbin/hostapd

First test!


Finally we can test the access point host! Run

 sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

To manually run hostapd with our configuration file. You should see it set up and use wlan0 then you can check with another wifi computer that you see your SSID show up. If so, you have successfully set up the access point.




Username : Jyothi_Pi_AP
Password: SaiJyothi
You can try connecting and disconnecting from the Pi_AP with the password you set before (probably Raspberry if you copied our hostapd config), debug text will display on the Pi console but you won't be able to connect through to the Ethernet connection yet.
Cancel the test by typing Control-C in the Pi console to get back to the Pi command line


Second Test!


Also we can test simple HTTP get webclient test using NodeMCU using the available access point.


Username : Jyothi_Pi_AP
Password: SaiJyothi

Using the Arduino programming we can establish this connection. Run below code in Arduino to test.


Code:https://github.com/jyothi-lanka/IoT/commit/48c6e008dd960fc2719650671c64c42e68305a79







Finishing up!


OK now that we know it works, time to set it up as a 'daemon' - a program that will start when the Pi boots.

Run the following commands

 sudo service hostapd start 
sudo service isc-dhcp-server start

you can always check the status of the host AP server and the DHCP server with

 sudo service hostapd status
sudo service isc-dhcp-server status

To start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')
Then to make it so it runs every time on boot

 sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable



References: https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview



Monday, February 8, 2016

Arduino Programming using NodeMCU 1.0 (ESP-12E)

 This experiment shows how easy it is to program using Arduino. Here this experiment demonstrates different programs using different sensors.




Experiment 1: Blinking of Internal LED



Things Required:


1.Laptop
2.Arduino IDE
3.Bread Board



Wiring Connection:

Simply connect the NodeMCU to the Laptop and run the blink program.

Code: https://github.com/jyothi-lanka/IoT/blob/master/Arduino/InternalLEDBlink.ino

Demo:






Experiment 2: Blinking of External LED



Things Required:


1.Laptop
2.Arduino IDE
3.LED
4.Resistor (330 ohms)
5.Jumper wires
6.Bread Board


Wiring Connection:

Connect as shown below:




1. Connect the D7 (pin 13) of NodeMCU to one end of the resistor.
2. Connect the other end of the terminal to the positive terminal of LED.
3. Connect the negative terminal of LED to GND ().

Code: https://github.com/jyothi-lanka/IoT/blob/master/Arduino/ExternalLEDBlink.ino

Demo:






Experiment 3: Temperature and Humidity measurements using DHT11 Sensor 



Things Required:


1.Laptop
2.Arduino IDE
3.DHT11 sensor
4.Resistor(10 K ohms)
5.Jumper wires
6.Bread Board


Wiring Connection:

Connect as shown below:




1. Connect pin1 of DHT11 to VCC (3.3V) pin1 of NodeMCU.
2. Connect pin2, the Digital output pin of the sensor to D7(pin 13) of NodeMCU.
3. Connect pin 4 of DHT to GND (pin of NodeMCU).
4. Connect resistor across the DHT11 output(pin2 of DHT11) and VCC(pin1 of NodeMCU).


Code: https://github.com/jyothi-lanka/IoT/blob/master/Arduino/DHT11Sensor.ino

Demo:




Monday, February 1, 2016

Arduino Programming class effort



1)Which laptop OS are you testing to program from: 

Testing from a laptop with Windows 10 Operating System 

2)What is the PI you are using and which OS is it setup with? 

Raspberry Pi2 Model B and having Raspbian OS(today experimented only on laptop not on Pi)

3)What is the hardware that is needed?

NodMCU V2 , Bread Board, Jumper wires
For  LED  blinking experiment : 330 ohmsResistor, LED,
For Temperature and Humidity Sensing Experiment : 10 k ohms Resistor, DHT11 Sensor, 

4)What is the software that is needed?

Arduino IDE installation package for windows : arduino-1.6.7-windows
(https://www.arduino.cc/en/Main/Software)

Arduino ESP8266 extension Installation:
Once you have installed Arduino, install the Arduino ESP8266 extension by selecting Preferences and entering 
" http://arduino.esp8266.com/stable/package_esp8266com_index.json " into the Additional Board Manager URLs field.  

Install Esp8266 platform:
Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select NodeMCU 1.0(ESP-12E) from Tools > Board menu after installation)

DHT Sensor Library:
Open Sketch > Include Library > Manage Libraries >select for DHT sensor library and then install.


5) How do you wire it together?

Wiring Connections:

 For  LED  blinking experiment : 

  • Connect the D7 (pin 13) of NodMCV to one end of the 330 ohms resistor.
  • Connect the other end of the resistor to Positive terminal of LED.
  • Connect the Negative terminal of LED to GND (ground pin) of NodMCV.

For Temperature and Humidity Sensing Experiment : 

  • Connect the 3v3 (pin 1)of NodMCV to pin1 of DHT11 Sensor.
  • Connect the D7(pin 13) of NodMCV to pin2 of DHT11 Sensor.
  • Connect the GND (ground pin) of NodMCV to pin4 of DHT11 Sensor.
  • Connect one end of 10 K ohms resistor to pin2 of DHT11 Sensor.
  • Other end of the resistor to 3V3 (pin 1) of NodMCV.


6) What is the logical first test?

a. LED
Logical test here is if high voltage is passed to pin13 then LED should glow and viceversa.

b. Humidity, Temperature Sensor
Logical test here is if the DHT11 Sensor is able to sense the reading.
Later that can be checked from the output displayed on the output terminal.

What were the problems that you had to solve?
Everything went well for me except one.
While performing the Humidity, temperature Sensor its hard for me to find the console.
Google helped me to find that light small icon at the top right corner where we can check the output display. 


How far did you get.
I am done with the Laptop version of the Arduino experiment.
Performed all the 3 experiments and successfully completed.




Domain of Research

INTERNET OF THINGS:


The Internet of Things (IoT) is the network of physical objects, devices, vehicles, buildings and other items which are embedded with electronics, software, sensors, and network connectivity, which enables these objects to collect and exchange data.


I feel its our responsibility to ensure a sustainable environment for the future generations to lead a balanaced life.

We can ensure this by protecting our natural resources, finding  ways to monitor various changes and their effects on environment, bringing out hidden trends from the collected data to take preventive measures.

So I intend to do my IoT research on how. to make environment smart

Topic of Research: Environment


SMART ENVIRONMENT

A SMART WORLD WHERE DIFFERENT KINDS OF SMART DEVICES ARE CONTINUOUSLY WORKING TO MAKE INHABITANTS " LIVES MORE COMFORTABLE."

Applications:

Weather Monitoring

Air Pollution Monitoring

Noise Pollution Monitoring

Forest Fire Detection

River Floods Detection




Serial Peripheral Interface

Serial Peripheral Interface(SPI) is a serial data protocol that is used by microcontrollers and small electronic devices to exchange information.

The Raspberry Pi is equipped with one SPI bus that has 2 chip selects.The SPI bus is a synchronous serial communication interface specification which can be used for communication between Pi and other hardware devices.

The SPI master driver is disabled by default on Raspian.
To enable it, remove the blacklisting for spi-bcm2708 in /etc/modprobe.d/raspi-blacklist.conf, or use raspi-config.

The SPI bus is available on the P1 Header.

SPI Pin Description on Pi Header:

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(or Chip Select)
P1-26           CE1     - Chip Enable

There are multiple modes 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):


The LoSSI standard allows issuing of commands to peripherals (LCD) and to transfer data to and from them. 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.


TRANSFER MODES:


Polled
Interrupt
DMA

To use SPI, need four pins:

MOSI   - Master Out Slave In
MISO   - Master In Slave Out
SCLK   - Serial CLocK
SS         - Slave Select

There is some variety in how different SPI devices expect the process how data communication works between master and slave to work.
Some require clock line high when not in-use other require low.

Reference : https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md

Temperature and Humidity Sensing using Raspberry Pi

This experiment shows how easy it is to have our own temperature and Humidity monitoring device using DHT11 Sensor and Pi. Here in this experiment I used the google spreadsheet to have live data of temperature and Humidity.

Things Required:

  1. Raspberry Pi Kit
  2. DHT11 Sensor
  3. 10 K ohms Resistor
  4. Bread Board
  5. Jumper Wires


DHT11 Sensor: 


             The DHT11 is a basic, ultralow-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed).

DHT11 Sensor

DHT11 Pin Description:

    Pin                 Name                Description

      1                   VDD                 Power supply 3-5.5 V DC
      2                   DATA               Serial data output
      3                   NC                    Not connected
      4                   GND                 Ground 

Wiring Connections:




Connect the Sensor to the Pi as shown below:




  1. Connect pin1 of DHT11 to VCC (3.3V), pin1 of Pi.
  2. Connect pin2, the Digital output pin of sensor to GPIO4, input pin of Pi (Pin7).
  3. Connect pin 4 of DHT to GND (pin39 of Pi).
  4. Connect resistor across DHT11 output(pin2 of DHT11) and VCC(pin1 of Pi).

Code:


Programs for the experiment are available at:

Experiment 1:

To read the temperature and humidity using DHT11.

Simply run the program AdafruitDHT.py program

Inorder to run this from the command prompt we need to give inputs from the command line arguments. First argument is sensor model number(11) and second GPIO pin number(4), this is not the actual pin number.

1. In the terminal, navigate to the program location.
2. Run sudo ./AdafruitDHT.py 11 4

Output:




Experiment 2:


To update a google spreedsheet live with the temperature and humidity data.

This experiment requires groundwork such as setting up google developer account, giving google drive access, getting service account for the google drive.
The process is well explained at https://learn.adafruit.com/downloads/pdf/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging.pdf

After all the setup run the google_spreadsheet.py

sudo ./google_spreedsheet.py

you can visualize the output on the terminal and also you can view in the spreedsheet.
Below are the demos of the results of the reading updated in terminal and spreedsheet.

You can view the output sheet at https://docs.google.com/spreadsheets/d/13vqXOYCvRSJF-jHkdAd0JfaP3wr4pGCxg-nhBk3LmWU/edit?usp=sharing


Output Demo: