Raspberry Pi + TI Sensortag + Plotly

In the last post (Raspberry Pi 2 TI Sensortag) I recapped the possibility to use Raspberry Pi to connect with BLE to a TI Sensortag using the bluepy-library.

A logical next step is to push the data to the „cloud“.

First register under https://plot.ly/ go to settings and note your API Key and your tokens (create two).

To use it with the Raspberry Pi ssh into it (or use a terminal) and install the prerequistes:
sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install rpi.gpio
sudo pip install plotly

The next step is to create a script which uploads the streaming data plot.ly, it is heaviliy based on this intructable from plotlygraphs:

import plotly.plotly as py
from plotly.graph_objs import Scatter, Layout, Figure, Data
import time
import datetime
import sensortag

username = 'your_username'
api_key = 'your_api_key'
stream_token1 = 'one_unique_token'
stream_token2 = 'another_unique_token'

py.sign_in(username, api_key)

trace1 = Scatter(
    x=[],
    y=[],
    name='Ambient Temp.',
    stream=dict(
        token=stream_token1,
        maxpoints=5000
    )
)
trace2 = Scatter(
    x=[],
    y=[],
    name='IR Temp.',
    stream=dict(
        token=stream_token2,
        maxpoints=5000
    )
)

layout = Layout(
    title='Raspberry Pi Streaming Sensor Data',
    showlegend=True,
    autosize=True
)

data = Data([trace1,trace2])
fig = Figure(data=data, layout=layout)

print(py.plot(fig, filename='Raspberry Pi Streaming Example Values'))

# activate sensortag
intervall = 20 # seconds
print "Push button on sensortag"
time.sleep(2.0)
tag = sensortag.SensorTag('BC:6A:29:AC:53:D1')

time.sleep(1.0)
tag.IRtemperature.enable()
print("Connected")

i = 0
stream1 = py.Stream(stream_token1)
stream1.open()

stream2 = py.Stream(stream_token2)
stream2.open()


#the main sensor reading loop
while True:
    sensor_data = tag.IRtemperature.read()
    now = datetime.datetime.now()
    stream1.write({'x': now, 'y': sensor_data[0]})
    stream2.write({'x': now, 'y': sensor_data[1]})
    i += 1
        # wait for new measurement
    tag.waitForNotifications(intervall)
    if i > 100:
        break

stream1.close()
stream2.close()
tag.disconnect()
print("Finished!")

The result looks somthing like this.