Scratch_plotly.py

#!/usr/bin/env python
"""
Created on Tue Nov 18 15:41:07 2014

@author: Lennart Kuhlmeier
"""

import scratch
import plotly.plotly as py # plotly library
import json # used to parse config.json
import time
import datetime 
from plotly.graph_objs import *

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"])

data =Data([
    Scatter(x=[],y=[],line=Line(color='rgba(44, 160, 44, 0.5)',shape='spline'),fill='tozeroy',
        stream=Stream(
            token=plotly_user_config['plotly_streaming_tokens'][0],
            maxpoints=50
        )
    )
])

layout = Layout(
    title='Distance to robot car ultrasonic sensor',
    showlegend=True,
    autosize=True,
    width=869,
    height=507,
    xaxis=XAxis(
        title='Time',
        type='date',
        autorange=True,
        showline=True,
        gridcolor='rgb(190, 190, 190)'
    ),
    yaxis=YAxis(
        title='Dsitance [cm]',
        type='linear',
        autorange=True,
        showline=True,
        gridcolor='rgb(190, 190, 190)'
    ),
    plot_bgcolor='rgba(0, 0, 0, 0)',
    dragmode='pan'
)

fig = Figure(data=data,layout=layout)    
url = py.plot(fig, filename='Albert og Fars robotbil operation states')

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

s = scratch.Scratch()
while True:
    s.connect()
    msg = s.receive()
    print msg[1].get('Power15')
    Dist = "%.1f" % msg[1].get('Power15')
    stream.write({'x': datetime.datetime.now(), 'y': Dist})
    time.sleep(1)
    s.disconnect()

Leave a Reply

Your email address will not be published. Required fields are marked *

HTML Snippets Powered By : XYZScripts.com