Unit Iv
Unit Iv
Sensors
sensors of various types will allow the Raspberry Pi to measure
temperature, light, and more.
Compared to a board like the Arduino, the Raspberry Pi lacks analog
inputs.
This means that for many sensors, it is necessary to use additional
analog-to-digital (ADC) converter hardware.
• It is also possible to use resistive sensors with a capacitor and a
couple of resistors
Using Resistive Sensors
• Problem
• You want to connect a variable resistor to a Raspberry Pi and measure
its resistance so that you can use the position of the variable resistor’s
knob in your Python program.
Solution
• You can measure resistance on a Raspberry Pi using nothing more
than a capacitor, a couple of resistors, and two GPIO pins.
• In this case, you will be able to estimate the position of the knob on a
small variable resistor (trimpot) by measuring its resistance from its
slider contact to one end of the pot.
To make this , you will need:
• Breadboard and jumper wires
• 10kΩ trimpot
• Two 1kΩ resistors
• 330 nF capacitor
• Open an editor (nano or IDLE) and paste in the following code. As
with all the pro‐ gram examples in this book, you can also download
the program from the Code sec‐ tion of the Raspberry Pi Cookbook
website, where it is called pot_step.py.
• When you run the program, you should see some output like this:
• $ sudo python pot_step.py
• 10049.2157936
• 10105.1559448
• 10158.6098671
• 11331.0049375
• 10162.6154582
• 10156.8142573
• 9501.27855937
• 8216.17444356
• The reading will vary as you rotate the knob of the trimpot. Ideally, the
reading of resistance would vary between 0 and 10,000Ω but in practice,
there will be some error
Measuring Light
• Problem
• You want to measure light intensity with a Raspberry Pi and a
photoresistor.
• Solution
• Use the same basic recipe and code a, but replace the trimpot with a
photoresistor.
To make this recipe, you will need:
• Breadboard and jumper wires
• Photoresistor
• Two 1kΩ resistors
• 330 nF capacitor
• All these parts are included in the Electronics Starter Kit for Raspberry
Pi from Monk Makes.
• Using the same program (pot_step.py),
• you will see the output vary as you move your hand over the
photoresistor to cut out some of the light.
• This solution provides relatively reliable readings of light levels. As an
adaptation of the general solution for using resistive sensors
• it also copes with measuring a resistance of 0Ω without any risk of
damaging the GPIO pins of the Rasp‐ berry Pi.
• Since a photoresistor is never going to dip to zero resistance, you can
take a few shortcuts and do away with both 1kΩ resistors and one of
the GPIO pins and make the simpler schematic shown in Figure 13-5.
Detecting Methane
• Problem
You want to measure gas levels using a methane sensor.
Solution
Low-cost resistive gas sensors are available that can easily be wired to a
Raspberry Pi to detect gases such as methane.
You can use the step response method.
To make this recipe, you will need:
• Breadboard and jumper wires
• Methane sensor
• Two 1kΩ resistors
• 330 nF capacitor
• The sensor contains a heating element that requires 5V at up to
150mA.
• The Rasp‐ berry Pi is capable of providing this as long as its power
supply can supply the extra 150mA.
• The sensor module has rather thick legs—too thick to fit into
breadboard holes.
• One way around this is to solder short lengths of solid core wire to
each lead (Figure 13-7).
• Another is to buy SparkFun’s gas sensor breakout board.
Measuring a Voltage
Problem
• You want to measure an analog voltage.
Solution
• The Raspberry Pi GPIO connector has only digital inputs.
• If you wish to measure a voltage, you need to use a separate analog-to-digital converter (ADC).
• Use the MCP3008 eight-channel ADC chip.
• This chip actually has eight analog inputs, so you can connect up to eight sensors to one of these
and interface to the chip using the Raspberry Pi SPI interface.
• To make this recipe, you will need:
• • Breadboard and jumper wires
• • MCP3008 eight-channel ADC IC
• • 10kΩ trimpot
• The variable resistor has one end connected to 3.3V and the other to
ground, which allows the middle connection to be set to any voltage
between 0 and 3.3V.
• Before trying the program, make sure you have SPI enabled and the
SPI Python library installed (Recipe 9.5).
• Open an editor (nano or IDLE) and paste in the following code. As
with all the pro‐ gram examples in this book, you can also download
the program from the Code sec‐ tion of
http://www.raspberrypicookbook.com, where it is called adc_test.p
• The interesting part of the program is contained in the analog_read
function.
• This takes a parameter that should be between 0 and 7 and specifies
which of the eight analog inputs down the lefthand side of the chip
should be read.
• The bit manipulation sets up a request for the appropriate channel
and then sends the bits to the MCP3008, which reads the resultant
data:
• $ sudo python adc_test.py
• Reading=0 Voltage=0.000000
• Reading=126 Voltage=0.406055
• Reading=221 Voltage=0.712207
• Reading=305 Voltage=0.982910
• Reading=431 Voltage=1.388965
• Reading=527 Voltage=1.698340
• Reading=724 Voltage=2.333203
• Reading=927 Voltage=2.987402
• Reading=1020 Voltage=3.287109
• Reading=1022 Voltage=3.293555
• The MCP3008 has 10-bit ADCs, so when you take a reading, it gives
you a number between 0 and 1023.
• The test program converts this into a voltage reading by multi‐ plying
the reading by the voltage range (3.3V) and then dividing it by 1,024
Using Resistive Sensors with an ADC
Problem
You have a resistive sensor that you wish to use with an MCP3008 ADC chip.
Solution
Use a potential divider with one fixed resistor and the resistive sensor to convert
the resistance of the sensor into a voltage that can be measured with the ADC.
As an example, you can remake the light sensor project of Recipe 13.2 to use the
MCP3008 instead of the step response technique.
To try this recipe, you will need:
• Breadboard and jumper wires
• MCP3008 eight-channel ADC IC
• 10kΩ resistor
• Photoresistor
• You also need to set up SPI on your Raspberry Pi,
• Sensors $ sudo python adc_test.py
• Reading=341 Voltage=1.098926
• Reading=342 Voltage=1.102148
• Reading=227 Voltage=0.731543
• Reading=81 Voltage=0.261035
• Reading=86 Voltage=0.277148
• These readings may be quite a bit different, depending on photoresistor,
but the important thing is that the figure changes as the light level
changes.
Measuring Temperature with an ADC
• Problem
• You want to measure temperature using a TMP36 and an analog-to-
digital converter.
• Solution
• Use an MCP3008 ADC chip. However, unless you need more than one
analog channel, you should consider using the DS18B20 digital
temperature sensor, which is more accurate and doesn’t require a
separate ADC chip.
• To try this recipe, you will need:
• Breadboard and jumper wires
• MCP3008 eight-channel ADC IC
• TMP36 temperature sensor
• You will need to set up SPI on your Raspberry Pi.
• Open an editor (nano or IDLE) and paste in the following code. As
with all the pro‐ gram examples in this book, you can also download
the program from the Code sec‐ tion of the Raspberry Pi Cookbook
website, where it is called adc_tmp36.py
import spidev, time
spi = spidev.SpiDev()
spi.open(0,0)
def analog_read(channel):
r = spi.xfer2([1, (8 + channel) << 4, 0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
while True:
reading = analog_read(0)
voltage = reading * 3.3 / 1024
temp_c = voltage * 100 - 50
temp_f = temp_c * 9.0 / 5.0 + 32
print("Temp C=%f\t\tTemp f=%f" % (temp_c, temp_f))
time.sleep(1)
The program is based on .
A little bit of additional math calculates the temperature in degrees Celsius and Fahrenheit:
$ sudo python adc_tmp36.py
Temp C=19.287109 Temp f=66.716797
Temp C=18.642578 Temp f=65.556641
Temp C=18.964844 Temp f=66.136719
Temp C=20.253906 Temp f=68.457031
Temp C=20.898438 Temp f=69.617188
Temp C=20.576172 Temp f=69.037109
Temp C=21.865234 Temp f=71.357422
Temp C=23.154297 Temp f=73.677734
Temp C=23.476562 Temp f=74.257812
Temp C=23.476562 Temp f=74.257812
Temp C=24.121094 Temp f=75.417969
Temp C=24.443359 Temp f=75.998047
Temp C=25.087891 Temp f=77.158203
Measuring the Raspberry Pi CPU Temperature
Problem
You want to know just how hot your Raspberry Pi’s CPU is getting.
Solution
Use the os library to access the temperature sensor built into the
Broadcom chip.
Open an editor (nano or IDLE) and paste in the following code.
you can also download the program from the Code section of the
Raspberry Pi Cookbook website, where it is called cpu_temp.py
import os, time
while True:
dev = os.popen('/opt/vc/bin/vcgencmd measure_temp')
cpu_temp = dev.read()
print(cpu_temp) time.sleep(1)
• When you run this program, it will report the temperature.
• Note that the message printed is actually a string with temp= before
the temperature and 'C after it.
$ python cpu_temp.py
temp=33.6'C
temp=33.6'C
Measuring Temperature, Humidity, and
Pressure with a Sense HAT
• Problem
You want to measure temperature, humidity, and pressure, but don’t
really want to have to attach three separate sensors.
Solution
• Use a Raspberry Pi Sense HAT (Figure 13-16). That way you get all
those sensors plus some extras like a displa
• Open an editor (nano or IDLE) and paste in the following code. As
with all the pro‐ gram examples in this book, you can also download
the program from the Code sec‐ tion of the Raspberry Pi Cookbook
website, where it is called sense_hat_thp.py.
from sense_hat import SenseHat
import time
hat = SenseHat()
while True:
t = hat.get_temperature()
h = hat.get_humidity()
p = hat.get_pressure()
print('Temp C:{:.2f} Hum:{:.0f} Pres:{:.0f}'.format(t, h, p))
time.sleep(1)
• When you run the program, the terminal will display something like this:
$ sudo python sense_hat_thp.py
Temp C:27.71 Hum:56 Pres:1005
Temp C:27.60 Hum:55 Pres:1005
The temperature is in degrees C, the humidity is the percentage of relative
humidity, and the atmospheric pressure is in millibars.
You will find that the temperature readings from the Sense HAT are on the
high side.
This is because the temperature sensor is built into the humidity sensor and
is on the Sense HAT PCB.
The Sense HAT generates very little heat (unless you use the display) but the
Raspberry Pi under the Sense HAT does get warm and will increase the tem‐
perature of the HAT.
The best way to avoid this problem is to use a 40-way ribbon cable to move
the Sense HAT away from the Raspberry Pi.
Measuring Temperature Using a Digital
Sensor
• Problem
You want to measure temperature using an accurate digital sensor.
Solution
Use the DS18B20 digital temperature sensor.
This device is more accurate than the TMP36 and uses a digital interface, so it
doesn’t require an ADC chip.
Although the interface to this chip is called one-wire, this just refers to the data pin.
You do need at least one other wire to connect to a one-wire device.
To make this recipe, you will need:
• Breadboard and jumper wires
• DS18B20 temperature sensor
• 4.7kΩ resistor
• Fit the components onto the breadboard as shown in Figure 13-17.
Make sure that you get the DS18B20 facing the right way.
• The latest version of Raspbian has support for the one-wire interface
used by the DS18B20 but you do have to enable it.
• To enable 1-wire support, edit the file /boot/config.txt using the
command sudo nano /boot/config.txt and add the line shown below
to the end of the file.
• Then reboot your Raspberry Pi for the change to take effect.
• $ dtoverlay=w1-gpio
import glob, time
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines() f.close()
return lines def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print("temp C=%f\ttemp F=%f" % read_temp())
time.sleep(1)
• When the program is run, it will report the temperature once a
second in both degrees Celsius and Fahrenheit: $ python
temp_DS18B20.py
• temp C=25.187000 temp F=77.336600
• temp C=25.125000 temp F=77.225000
• temp C=25.062000 temp F=77.111600
• temp C=26.312000 temp F=79.361600
• temp C=27.875000 temp F=82.175000
• temp C=28.875000 temp F=83.975000
Measuring Distance
• Problem
• You want to measure distance using an ultrasonic rangefinder. Solution
• Use a low-cost SR-04 rangefinder.
• These devices need two GPIO pins: one to trigger the pulse of ultrasound and the
other to monitor how long it takes for the echo to return.
• To make this recipe, you will need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 474)
• SR-04 rangefinder (eBay)
• 470Ω resistor
• 270Ω resistor
• Fit the components onto the breadboard as shown in Figure 13-23. The resistors
are necessary to reduce the echo output of the rangefinder from 5V to 3.3V
Unit 6
IoT Physical Servers, Cloud
Offerings & IoT Case Studies
Content
• Introduction to Cloud Storage Models
• Communication API
• WAMP: AutoBahn for IoT
• Xively Cloud for IoT
• Python Web Application Framework: Djanjo
• Amazon Web Services for IoT
• SkyNet IoT Messaging Platform.
• Case Studies:
• Home Intrusion Detection,
• Weather Monitoring System,
• Air Pollution Monitoring,
• Smart Irrigation.
Introduction to Cloud Storage Models
• Popular Models are
• Amazon Web Service (AWS)
• Xively Cloud (PAAS)
Communication API
• Cloud Models are relied on Communication API
• Communication API facilitate data transfer, control information
transfer from application to cloud, one service to another
• It also exist in the form of Communication Protocols
• It supports RPC, PUBSUB and WAMP
• Eg. Popular API is RESTful API (communication in cloud model)
• Django web framework is used to implement Communication API
WAMP: AutoBahn for IoT
• WAMP : Web Application Messaging Protocol
• Mainly used in cloud storage model for IoT & other messaging services
• WAMP is a routed protocol, with all components connecting to a WAMP Router,
where the WAMP Router performs message routing between the component
• It is protocol for Web Socket (PUBSUB based protocol) : uses RPC Messaging Pattern
• Some Important Key Terminologies
• Transport
• Session
• Clients (Publisher & Subscriber)
• Router
• Broker
• Dealer
• Application Code
WAMP for IoT
• The EC2 region, AWS access key and AWS secret REGION="us-east-1"
key are passed to this function. After connecting AMI_ID = "ami-d0f89fb9"
EC2_KEY_HANDLE = "<enter key handle>"
to EC2 , a new instance is launched using the INSTANCE_TYPE="t1.micro"
conn.run_instances function. SECGROUP_HANDLE="default"
scaling_adjustment=-1,
cooldown=180)
conn.create_scaling_policy(scale_up_policy)
conn.create_scaling_policy(scale_down_policy)
conn = boto.connect_s3(aws_access_key_id='<enter>',
aws_secret_access_key='<enter>')
• The schema includes the hash key and range key table_schema = conn.create_schema(
hash_key_name='msgid',
names and types. hash_key_proto_value=str,
range_key_name='date',
• A DynamoDB table is then created by calling the range_key_proto_value=str
)
conn.create_table function with the table schema,
read units and write units as input parameters. #Creating table with schema
table = conn.create_table(
name='my-test-table',
schema=table_schema,
read_units=1,
write_units=1
)
• JSON
• JavaScript Object Notation (JSON) is an easy to read and write data-interchange format. JSON is used as an alternative to XML
and is easy for machines to parse and generate. JSON is built on two structures: a collection of name–value pairs (e.g., a Python
dictionary) and ordered lists of values (e.g., a Python list).
• XML
• XML (Extensible Markup Language) is a data format for structured document interchange. The Python minidom library provides
a minimal implementation of the Document Object Model interface and has an API similar to that in other languages.
• HTTPLib & URLLib
• HTTPLib2 and URLLib2 are Python libraries used in network/internet programming.
• SMTPLib
• Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending email and routing email between mail servers. The
Python SMTPLib module provides an SMTP client session object that can be used to send email.
• NumPy
• NumPy is a package for scientific computing in Python. NumPy provides support for large multi-dimensional arrays and
matrices.
• Scikit-learn
• Scikit-learn is an open source machine learning library for Python that provides implementations of various machine learning
algorithms for classification, clustering, regression and dimension reduction problems.
• Django is an open source web application framework for developing web applications in Python.
• A web application framework in general is a collection of solutions, packages and best practices
that allows development of web applications and dynamic websites.
• Django is based on the Model–Template–View architecture and provides separation of the data
model from the business rules and the user interface.
• Django provides a unified API to a database backend.
• Thus, web applications built with Django can work with different databases without requiring any
code changes.
• With this flexibility in web application design combined with the powerful capabilities of the Python
language and the Python ecosystem, Django is best suited for cloud applications.
• Django consists of an object-relational mapper, a web templating system and a regular-expression-
based URL dispatcher.
• Model
• The model acts as a definition of some stored data and handles the interactions with the database. In a web
application, the data can be stored in a relational database, non-relational database, an XML file, etc. A Django model is
a Python class that outlines the variables and methods for a particular type of data.
• Template
• In a typical Django web application, the template is simply an HTML page with a few extra placeholders. Django’s
template language can be used to create various forms of text files (XML, email, CSS, Javascript, CSV, etc.).
• View
• The view ties the model to the template. The view is where you write the code that actually generates the web pages.
View determines what data is to be displayed, retrieves the data from the database and passes the data to the
template.
def question_list(request):
questions = Question.objects.all()
return render_to_response(‘question_list.html’, {
‘questions’:questions})
Quick CRUD Operations with Generic Views
• ListView
• UpdateView
• CreateView
• If Model is specified, automagically creates a matching ModelForm
• Form will save the Model if data passes validation
• Override form_valid() method to provide custom logic (i.e sending
email or setting additional fields)
Sample – As Class Based View
from .models import Question
from django.views.generic import ListView
class QuestionList(ListView):
model = Question
context_object_name = ‘questions’
Django Templates
• Very simple syntax:
variables = {{variable_name}}
template tags = {%tag%}
• Flexible – can be used to render html, text, csv, email, you name it!
• Dot notation – template engine attempts to resolve by looking for
matching attributes, hashes and methods
Question List Template
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>List of Questions</title>
</head>
<body>
{%if questions%}
<ul>
{%for q in questions%}
<li>{{q.question_text}}</li>
{%endfor%}
</ul>
{%else%}
<p>No questions have been defined</p>
{%endif%}
</body>
</html>
urls.py
• Defines routes to send urls to various views
• Can use regular expressions
• Extract parameters from a url and pass to the view as a named
parameter:
r(‘^question/(?P<question_id>\d+)/$’,’views.question_detail’)
• Extensible – urls.py can include additional url files from apps:
r(‘^question/’,include(question.urls))
Hooking up the Question List
from django.conf.urls import patterns, url, include
urlpatterns = patterns(‘’,
(r’^questions/$’,’views.QuestionList’)
)
OR:
from django.conf.urls import patterns
from views import QuestionListView
urlpatterns = patterns(‘’,
(r’^questions/$’,’views.QuestionList.as_view())
)
Forms in Django
• django.forms provides a class to build HTML forms and validation.
Example:
from django import forms
class EditQuestionForm(forms.Form):
question_text = forms.CharField(max_length = 200)
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
super(MyView,self).dispatch(*args, **kwargs)
Custom Auth Backend for the Bubble
Sending Email
• django.core.mail includes functions and classes for handling email
• Set EMAIL_HOST in settings.py to outgoing mailserver
• Import send_mail for simple mail:
send_mail(subject, message, from, to_emails)
• Use django.template.render_to_string to format a message using a
template
• Use EmailMultiAlternatives to create a text message and attach a
html version as well.