building the internet of things with raspberry pi
DESCRIPTION
With the advent of the low cost Raspberry Pi computer, anyone with a soldering iron and some basic Python skills can take everyday objects and transform them into fully networked, smart devices. In this talk, I will show you how I hacked a Raspberry Pi into my home alarm system, turning my network of IP cameras into motion triggered sensors. I will show you how to build basic input and output circuits and introduce you to the RPi.GPIO Python module. We’ll talk about how you can build a RESTful server on your Raspberry Pi to enable remote access. And finally, more ideas for hacking everyday objects around the home! No prior electronics knowledge required.TRANSCRIPT
![Page 1: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/1.jpg)
Building the Internet of Things with Raspberry Pi
![Page 2: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/2.jpg)
Who am I?
I am a Software Developer and Architect at Energy Partners
Caution – I am not an Electronic Engineer!
Building the Internet of Thingswith Raspberry Pi
![Page 3: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/3.jpg)
Building the Internet of Thingswith Raspberry Pi
![Page 4: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/4.jpg)
Building the Internet of Thingswith Raspberry Pi
![Page 5: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/5.jpg)
Building the Internet of Thingswith Raspberry Pi
Model A Model B Model B+
$25 $35 $35
1 USB port 2 USB ports 4 USB ports
No Ethernet 10/100 Ethernet 10/100 Ethernet
SD card slot SD card slot Micro SD card slot
![Page 6: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/6.jpg)
General-purpose input/output pins (GPIO)
Building the Internet of Thingswith Raspberry Pi
• GPIO pins can be configured to be input or output
• Input values are readable - HIGH or LOW (3v3 or 0v)
• Output values are writable/readable
![Page 7: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/7.jpg)
The Hardware
Building the Internet of Thingswith Raspberry Pi
![Page 8: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/8.jpg)
Building an Input Circuit
Opto-coupler / Opto-isolator
Transfers electrical signals between two isolated circuits by using light
Building the Internet of Thingswith Raspberry Pi
![Page 9: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/9.jpg)
Building an Input Circuit
Building the Internet of Thingswith Raspberry Pi
Resistor
Implements electrical resistanceReduce current flow
![Page 10: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/10.jpg)
Pull-up / pull-down resistors
• Pull-up resistors are connected to the high voltage (3.3V)
• Pull-down resistors are connected to ground
• Initialize an input pin to a known state when nothing is connected to it
Building the Internet of Thingswith Raspberry Pi
Pull-up resistor
![Page 11: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/11.jpg)
Building an Input Circuit - The diagram
Building the Internet of Thingswith Raspberry Pi
![Page 12: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/12.jpg)
Building an Output Circuit - The components
Building the Internet of Thingswith Raspberry Pi
Transistor
• Used to amplify and switch electronic signals and electrical power
• A voltage or current applied to one pair of the transistor's terminals changes the current through another pair of terminals.
![Page 13: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/13.jpg)
Building an Output Circuit - The components
Relay
• Electrically operated switch• Many relays use an electromagnet to
mechanically operate a switch• To control a circuit by a low-power
signal
Building the Internet of Thingswith Raspberry Pi
![Page 14: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/14.jpg)
Building an Output Circuit
Building the Internet of Thingswith Raspberry Pi
![Page 15: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/15.jpg)
Veroboard / Stripboard
Building the Internet of Thingswith Raspberry Pi
![Page 16: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/16.jpg)
26pin IDC ribbon cable + crimp connector
Building the Internet of Thingswith Raspberry Pi
![Page 17: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/17.jpg)
Shortcuts!
Building the Internet of Thingswith Raspberry Pi
![Page 18: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/18.jpg)
Accessories
![Page 19: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/19.jpg)
The Software
Building the Internet of Thingswith Raspberry Pi
![Page 20: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/20.jpg)
Building the Internet of Thingswith Raspberry Pi
![Page 21: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/21.jpg)
Finding your Pi's hardware revision
cat /proc/cpuinfo
Code(s) Model and revision2 Model B Revision 1.03 Model B Revision 1.0 + ECN0001 4, 5, 6 Model B Revision 2.0
Building the Internet of Thingswith Raspberry Pi
![Page 22: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/22.jpg)
Software considerations
• The software is the easy part!
• Possible to damage the Pi• Triple check your pin
configuration
Building the Internet of Thingswith Raspberry Pi
![Page 23: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/23.jpg)
Introducing RPi.GPIO
• Depending on your distro, probably already installed
• If not, use pip: sudo pip install rpi.gpio
Building the Internet of Thingswith Raspberry Pi
![Page 24: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/24.jpg)
Basic usage
#To import the RPi.GPIO module: import RPi.GPIO as GPIO
#Select the pin numbering scheme: GPIO.setmode(GPIO.BOARD) # or GPIO.setmode(GPIO.BCM)
Building the Internet of Thingswith Raspberry Pi
![Page 25: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/25.jpg)
Exit Cleanly
• Inputs are safer than outputs• Shorting outputs to ground can damage the PI• Any ports not reset before script exit will
remain as is• Always perform cleanup before script exits
Building the Internet of Thingswith Raspberry Pi
![Page 26: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/26.jpg)
Exit Cleanly
import RPi.GPIO as GPIO
# the rest of your code goes here
GPIO.cleanup() # remember, a program doesn't necessarily # exit at the last line!
Building the Internet of Thingswith Raspberry Pi
![Page 27: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/27.jpg)
Inputs - Setupimport RPi.GPIO as GPIO
#GPIO numbering GPIO.setmode(GPIO.BCM)
channel = 7 GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP) # or GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
Building the Internet of Thingswith Raspberry Pi
![Page 28: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/28.jpg)
Inputs - Pollingif GPIO.input(channel): print('Input was HIGH') else: print('Input was LOW')
# Or, in a loop while GPIO.input(channel) == GPIO.LOW: # wait 10 ms time.sleep(0.01)
Building the Internet of Thingswith Raspberry Pi
![Page 29: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/29.jpg)
Inputs - wait_for_edge()
• blocks execution until an edge is detected• GPIO.RISING, GPIO.FALLING or GPIO.BOTH
GPIO.wait_for_edge(channel, GPIO.RISING)
Building the Internet of Thingswith Raspberry Pi
![Page 30: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/30.jpg)
Inputs - event_detected()
• Designed to be used in a loop with other things
# add rising edge detection on a channel GPIO.add_event_detect(channel, GPIO.RISING) do_something() if GPIO.event_detected(channel): print('Button pressed')
Building the Internet of Thingswith Raspberry Pi
![Page 31: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/31.jpg)
Inputs - Threaded callbacks
• RPi.GPIO runs a second thread for callback functions
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)
Building the Internet of Thingswith Raspberry Pi
![Page 32: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/32.jpg)
Outputs - Setup
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
Building the Internet of Thingswith Raspberry Pi
![Page 33: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/33.jpg)
Outputs
#Set output to HIGH GPIO.output(12, GPIO.HIGH)
#Set output to LOW GPIO.output(12, GPIO.LOW)
#Read the state of the output GPIO.input(12)
Building the Internet of Thingswith Raspberry Pi
![Page 34: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/34.jpg)
Example - Bell
Building the Internet of Thingswith Raspberry Pi
![Page 35: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/35.jpg)
The Raspberry Pi Alarm
Building the Internet of Thingswith Raspberry Pi
![Page 36: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/36.jpg)
On Github
Building the Internet of Thingswith Raspberry Pi
github.com/nbroers/hal
![Page 37: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/37.jpg)
What’s next?
Building the Internet of Thingswith Raspberry Pi
![Page 38: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/38.jpg)
Questions?
Building the Internet of Thingswith Raspberry Pi
![Page 39: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/39.jpg)
Parts list12V input circuit:
• Optocoupler: LTV-847 - http://www.mantech.co.za/ProductInfo.aspx?Item=340M0324• Resistor: 1K Ohm - http://www.mantech.co.za/ProductInfo.aspx?Item=64M0009
Output circuit:
• Resistor: 100k ohm• Resistor: 10k ohm• Transistor (NPN Transistor): P2N2222AG - http://www.mantech.co.za/ProductInfo.aspx?Item=72M3626• Relay: EDR101A1200Z - http://www.mantech.co.za/ProductInfo.aspx?Item=35M1045• Diode: 1N4007: http://www.mantech.co.za/ProductInfo.aspx?Item=64M0027
Extras:
• 26pin IDC ribbon cable crimp connector - http://www.mantech.co.za/ProductInfo.aspx?Item=14M8762• Ribbon cable - http://www.mantech.co.za/ProductInfo.aspx?Item=45M0001• Single core cable• Veroboard - http://www.mantech.co.za/ProductInfo.aspx?Item=64M0010
Building the Internet of Thingswith Raspberry Pi
![Page 40: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/40.jpg)
ReferencesInput Circuit design:http://raspberrypihobbyist.blogspot.com/2012/09/gpio-input-circuit_19.html
Output Circuit design:http://raspberrypihobbyist.blogspot.com/2012/10/revision-to-relay-circuit.html
RPi.GPIO wikihttp://sourceforge.net/p/raspberry-gpio-python/wiki/Home/
Raspberry Pi Technical Infohttp://elinux.org/RPi_Low-level_peripherals
GPIO quick referencehttp://raspi.tv/2014/rpi-gpio-quick-reference-updated-for-raspberry-pi-b
Exit Cleanlyhttp://raspi.tv/2013/rpi-gpio-basics-3-how-to-exit-gpio-programs-cleanly-avoid-warnings-and-protect-your-pi
Building the Internet of Thingswith Raspberry Pi
![Page 41: Building the Internet of Things with Raspberry Pi](https://reader036.vdocument.in/reader036/viewer/2022062307/557c2341d8b42a22218b513e/html5/thumbnails/41.jpg)
Neil Broers
@nbroers
[email protected] github.com/nbroers
Building the Internet of Thingswith Raspberry Pi