Sign In SIGN UP UPGRADE

Raspberry Pi + TMP36 Temperature Sensor

TMP36 Temperature Sensor splash image

Getting started

Hooking up analog sensors to the Raspberry Pi is easy with the MCP3008. Follow along to learn how to hook your Raspi up to a temperature sensor, and start streaming right away! We'll be using the Plotly Python API on the Raspberry Pi to do all the heavy lifting. This tutorial does however, assume that you have an internet connection on your Pi, as well as either SSH access or a keyboard and monitor hooked up.

Materials

TMP36 Temperature Sensor Parts

Hooking it up

TMP36 Temperature Sensor hookup

Software

First, install the required modules and dependencies (you can copy and paste this in your terminal):

sudo apt-get install python-dev
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | sudo python
sudo easy_install -U distribute
sudo apt-get install python-pip
sudo pip install rpi.gpio
sudo pip install plotly

Create a new folder for your project. Create a config.json file in said folder and input your plotly API key, and your plotly streaming tokens.

Example config.json:

{
"plotly_streaming_tokens": ["your_stream_token", "another_stream_token"],
"plotly_api_key": "api_key",
"plotly_username": "username"
}

The script

Grab the Python scripts here!

tmp36.py is where all the fun happens. readadc.py is just a helper script that tmp36.py will use to poll for analog data from the MCP3008. Copy both of these files into the folder you have created above.

Open up tmp36.py, and let's go through it.

First we're just importing all of the necessary libraries. readadc makes it easy to pull analog data from your Rasberry Pi's GPIO pins.

import plotly.plotly as py # plotly library
    import json # used to parse config.json
    import time # timer functions
    import readadc # helper functions to read ADC from the Raspberry Pi
    import datetime # log and plot current time

Now, we're going to pull in our config file, and use them to initialize our Plotly object.

with open('./config.json') as config_file:
    plotly_user_config = json.load(config_file)

    py.sign_in(plotly_user_config["plotly_username"], plotly_user_config["plotly_api_key"])

Now, we create a shell for our graph, and prepare it for streaming. This is where you first include your streaming_token, to tell Plotly's servers to expect a stream from that particular stream_token!

url = py.plot([
    {
        'x': [], 'y': [], 'type': 'scatter',
        'stream': {
            'token': plotly_user_config['plotly_streaming_tokens'][0],
            'maxpoints': 200
        }
    }], filename='Raspberry Pi Streaming Example Values')

print "View your streaming graph here: ", url

The TMP36 is hooked up to PIN0 on the MCP3008. We then initialize our readadc module (preparing to read analog values). We then initialize a plotly stream, indicating which token we will be using for that trace.

sensor_pin = 0
readadc.initialize()

stream = py.Stream(plotly_user_config['plotly_streaming_tokens'][0])
stream.open()

Here we're just creating the main loop that polls for data on sensor_pin, converting that to a temperature reading, and writing that data to our plotly stream with stream.write(data)

#the main sensor reading and plotting loop
while True:
    sensor_data = readadc.readadc(sensor_pin,
                                  readadc.PINS.SPICLK,
                                  readadc.PINS.SPIMOSI,
                                  readadc.PINS.SPIMISO,
                                  readadc.PINS.SPICS)

    millivolts = sensor_data * (3300.0 / 1024.0)
    # 10 mv per degree
    temp_C = ((millivolts - 100.0) / 10.0) - 40.0
    # convert celsius to fahrenheit
    temp_F = (temp_C * 9.0 / 5.0) + 32
    # remove decimal point from millivolts
    millivolts = "%d" % millivolts
    # show only one decimal place for temprature and voltage readings
    temp_C = "%.1f" % temp_C
    temp_F = "%.1f" % temp_F

    # write the data to plotly
    stream.write({'x': datetime.datetime.now(), 'y': temp_C})

    # delay between stream posts
    time.sleep(0.25)

Wrapping it up

You're all ready to go! You've got your Pi hooked up as per the above diagram, you've created a config.json file with your credentials, and in the same folder have you have copied over tmp36.py and readadc.py. From that directory, run:

sudo python tmp36.py

You can watch the console to see the URL returned from Plotly's server. Now go check out your live stream!