Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dwight Hubbard - Keeping cool, using a Raspberr...

Dwight Hubbard - Keeping cool, using a Raspberry PI to create a networked temperature sensor

Want to keep things cool? Come learn how to build a networked temperature sensor using a Raspberry PI, some simple hardware, and Python.

https://us.pycon.org/2016/schedule/presentation/1986/

PyCon 2016

May 29, 2016
Tweet

More Decks by PyCon 2016

Other Decks in Programming

Transcript

  1. 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
  2. How this got started We wanted to ensure our cat

    was safe during our pit stops on road trips.
  3. 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
  4. 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
  5. 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.
  6. Other Things Breadboard - A prototyping board Wires - To

    connect the Raspberry Pi GPIO pins to the Breadboard. 4.7k resistor
  7. 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.
  8. 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.
  9. 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
  10. 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
  11. Next Up Writing the Code Now we have the sensor

    all wired up now time to write some code to use it.
  12. 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)
  13. 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/<seconds>') 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)
  14. 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)
  15. 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/<seconds>') 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)