Monday, February 1, 2016

Week - 2

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))

# Initialize library.
disp.begin(contrast=60)

# Clear display.
disp.clear()
disp.display()

image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))

# 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.


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



No comments:

Post a Comment