Slide 1

Slide 1 text

Keeping cool, using a Raspberry PI to create a networked temperature sensor Keeping cool By Dwight Hubbard [email protected] Using a Raspberry PI to create a networked temperature sensor

Slide 2

Slide 2 text

How this got started We wanted to ensure our cat was safe during our pit stops on road trips.

Slide 3

Slide 3 text

Next Up Choosing the Hardware

Slide 4

Slide 4 text

Raspberry Pi Hardware Connections The Raspberry Pi has a General Purpose Input Output (GPIO) pins that can be used to connect to electronics. These pins allow connecting electronics directly to the system. This is really powerful

Slide 5

Slide 5 text

Raspberry Pi GPIO requirements When choosing electronics to connect to the Raspberry PI GPIO pins there are 2 simple rules: ☐ Component must operate at 3.3 volts ☐ Component must be digital

Slide 6

Slide 6 text

The Temp Sensor Maxim/Dallas DS18B20 Operates at 3.3 Volts ○ Supports 3.0Volts to 5.5Volts Digital ○ Uses a serial (digital) protocol Driver - Built into the Raspbian distribution.

Slide 7

Slide 7 text

Next Up Hooking up the Hardware

Slide 8

Slide 8 text

Other Things Breadboard - A prototyping board Wires - To connect the Raspberry Pi GPIO pins to the Breadboard. 4.7k resistor

Slide 9

Slide 9 text

Wiring the sensor to the Raspberry Pi The sensor has 3 pins: 1. With the flat side of the sensor facing you connect the pin on the right to the 3.3v pin on the raspberry PI. 2. Connect the pin on the left to a GND pin on the Raspberry PI. 3. Connect the middle pin to the Pin labeled #4 4. Use a 4.7k resistor to connect the right and middle pins.

Slide 10

Slide 10 text

Next Up Lessons Learned from writing the first software.

Slide 11

Slide 11 text

First software attempt The first version of the sensor used the sqlite python module for logging the temperature data. SD-Cards like the ones in the Raspberry Pi can only handle a limited number of writes. Left running, and sd-card failed after about a month.

Slide 12

Slide 12 text

Lesson learned Take into account the limited lifespan of the sd-card when writing the software.

Slide 13

Slide 13 text

Switch to Redis for Logging ● Stores data to RAM memory ● Writes to disk/sd-card are configurable ● Can replicate the data to another computer over a network without writing to the sd-card. ● Easy to understand

Slide 14

Slide 14 text

Next Up Setting Up Setting up the Raspberry Pi with our software.

Slide 15

Slide 15 text

Setting up the Software Everything this example uses is either part of the Raspbian OS or installable using the python pip tool. $ pip install redislite redis-collections bottle

Slide 16

Slide 16 text

Next Up Writing the Code Now we have the sensor all wired up now time to write some code to use it.

Slide 17

Slide 17 text

Data Logger from redislite import StrictRedis from redis_collections import List from time import sleep def read_temp_c(): with open('/sys/bus/w1/devices/28-000006b63824/w1_slave') as device: for line in device: value = line.strip().split()[-1] if value.startswith('t='): return float(value[2:])/1000.0 redis_connection = StrictRedis('/var/lib/temp.rdb') temp_readings = List(redis=redis_connection, key='temp_readings') while True: temp_readings.append(read_temp_c()) print('Temp C:', temp_readings[-1]) sleep(1)

Slide 18

Slide 18 text

Web Interface from bottle import route, run from redislite import StrictRedis from redis_collections import List redis_connection = StrictRedis('/var/lib/temp.rdb') temp_readings = List(redis=redis_connection, key='temp_readings') @route('/current_temp') def current_temp(): return str(temp_readings[-1]) @route('/average_temp/') @route('/average_temp/') def average_temp(seconds=3600): seconds=int(seconds) return str(sum(temp_readings[-seconds:])/len(temp_readings[-seconds:])) run(host='0.0.0.0', port=8080, debug=True)

Slide 19

Slide 19 text

Next Up Networking Sensors So lets see how we can run more than one of these

Slide 20

Slide 20 text

Networked Data Logger from __future__ import print_function from redislite import StrictRedis from redis_collections import List from time import sleep def read_temp_c(): with open('/sys/bus/w1/devices/28-000006b63824/w1_slave') as device: for line in device: value = line.strip().split()[-1] if value.startswith('t='): return float(value[2:])/1000.0 redis_connection = StrictRedis('/var/lib/temp.rdb', serverconfig={'port': '8002', 'requirepass': 'secret'}) temp_readings = List(redis=redis_connection, key='temp_readings') while True: temp_readings.append(read_temp_c()) print('Temp C:', temp_readings[-1]) sleep(1)

Slide 21

Slide 21 text

Replicated Web Interface from bottle import route, run from redislite import StrictRedis from redis_collections import List redis_connection = StrictRedis('/tmp/temp.rdb', serverconfig={'slaveof': 'notebook.local 8002', 'masterauth': 'secret'}) temp_readings = List(redis=redis_connection, key='temp_readings') @route('/current_temp') def current_temp(): return str(temp_readings[-1]) @route('/average_temp/') @route('/average_temp/') def average_temp(seconds=3600): seconds=int(seconds) readings=list(temp_readings)[-seconds:] return str(sum(readings)/len(readings)) run(host='0.0.0.0', port=8080, debug=True)

Slide 22

Slide 22 text

Conclusion Internet of things using Python and the Raspberry Pi is actually very easy.

Slide 23

Slide 23 text

Questions