#!/usr/bin/python
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here(you need to sign up first): https://plot.ly/settings/api
# a few of the pages I read to get things going
# https://plot.ly/python/axes/
# https://plot.ly/python/line-and-scatter-plots-tutorial/
# this page really got me started with the multiple lines in one plot:
# https://plot.ly/python/line-charts/
# I put the script here on my pi:
# /home/pi/brewpi-tools/brewpi_plot_ly.py
# here is the crontab entry I used:
# 0,30 * * * * sudo /home/pi/brewpi-tools/brewpi_plot_ly.py >/home/pi/logs/brewpi_plot_ly.log 2>&1
# the script looks in your /var/www/data/ directory
# it will look for csv files of the same name of the directory and name your graph the same name as your directory
# It skips the "Sample Data", ".gitignore" ,"profiles" and "My First BrewPi Run" directories
# it will print out the name of the graph/directory and the URL when it is done
import os, sys
import os.path, time
import string
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
def read_and_write_graph(whole_path, graph_name):
graph_name = string.replace(graph_name, '%20', ' ')
time_stamp = []
bt = []
ft = []
bs = []
fs = []
with open(whole_path) as f:
content = f.readlines()
for beer_string in content:
beer_data = beer_string.split(';', 9)
beer_date = beer_data[0]
beer_temp = beer_data[1]
beer_setting = beer_data[2]
ignore_1 = beer_data[3]
fridge_temp = beer_data[4]
fridge_setting = beer_data[5]
ignore_2 = beer_data[6]
ignore_3 = beer_data[7]
ignore_4 = beer_data[8]
time_stamp.append(beer_date)
bt.append(beer_temp)
ft.append(fridge_temp)
bs.append(beer_setting)
fs.append(fridge_setting)
trace1 = Scatter(
x=time_stamp,
y=bt,
mode='lines+markers',
name="beer temp",
line=Line(
shape='linear',
width=0.4
),
marker=Marker(
size=2,
)
)
trace2 = Scatter(
x=time_stamp,
y=ft,
mode='lines+markers',
name="fridge temp",
line=Line(
shape='linear',
width=0.4
),
marker=Marker(
size=2,
)
)
trace3 = Scatter(
x=time_stamp,
y=bs,
mode='lines+markers',
name="beer setting",
line=Line(
shape='linear',
width=0.4
),
marker=Marker(
size=2,
)
)
trace4 = Scatter(
x=time_stamp,
y=fs,
mode='lines+markers',
name="fridge setting",
line=Line(
shape='linear',
width=0.4
),
marker=Marker(
size=2,
)
)
data = Data([trace1, trace2, trace3, trace4])
layout = Layout(
title=graph_name,
legend=Legend(
y=0.5,
traceorder='reversed',
font=Font(
size=16
),
),
xaxis=XAxis(
tickmode='auto',
nticks=6
)
)
fig = Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename=graph_name)
print graph_name + " is at this URL: " + plot_url
tls.set_credentials_file(username='YOUR_USER_ID', api_key='YOUR_KEY')
# Open the directory
path = "/home/brewpi/data/"
dirs = os.listdir( path )
file_list = []
# This would print all the files and directories
for file in dirs:
#print file
if file != "Sample Data" and file != ".gitignore" and file != "profiles" and file != "My First BrewPi Run":
#print "adding ", file
whole_path = path + file + "/" + file + ".csv"
#print "adding whole path", whole_path
#print "last modified: %s" % time.ctime(os.path.getmtime(whole_path))
#print "created: %s" % time.ctime(os.path.getctime(whole_path))
read_and_write_graph(whole_path,file)