Use Plotly to graph dynamic local csv file


#1

Hi-

I’m really new to plotly, so I hope you’ll excuse me if this question has been asked before. Let me briefly describe my setup.

I’ve got a raspberry pi connected to the internet. I have a python script that reads three temp sensors, and writes the data to a csv file. I run the python script via cron every 10 minutes. Here is a sample of the csv file.

02-17-2016 16:06,77.45,81.725,87.575

The python script updates the csv file every 10 minutes via the cron job. I can’t seem to figure out how to make a script that would allow plotly to show this data as it is being updated dynamically. I found a tutorial on how to read the output from a temp sensor on a Arduino, but that reads the data directly from the temp sensor. I would like to keep a local copy of the csv file so that I can pull it from time to time to create my own graphs in Excel.

Thanks for any pointers you can provide.


#2

Hi. Were you able to get an answer to this?


#3

You can probably just add a few lines to your python script that will allow you to update a plotly graph hosted on your account.
This post might be relevant to you: http://moderndata.plot.ly/update-plotly-charts-with-cron-jobs-and-python/


#4

electronicsguy/yankev-

Thanks for the replies. I’ll take a look at the link that yakev provided. I was able to get something up and running using thingspeak, but I’ll see if I can get it to work on plotly for a comparison.


#5

@propman07 Hi David. Care to share your thingspeak code?


#6

@electronicsguy: Are you looking for the portion of the code for thingspeak, or the entire code? I can post either…the entire code is written in python. It’s a new language to me, so I pulled a lot of pieces from different sample code that I found on the net. I’m sure that there is a more elegant way to do what I’m doing, but for me, it works. Let me know, and I can post it later.


#7

I suppose the portion of the code to shape the data in the appropriate way and send it to thingspeak would help.


#8

Sorry it took so long for the reply. Work was kicking my @$$ this week…

Here is the code that I’m using. I’ve added some comments so that you can change the parts to fit your needs for your thingspeak account.

#! /usr/bin/python

import os
import glob
import datetime
import time
import urllib
import urllib2
import sys

THINGSPEAKKEY = 'yourthingspeakcodehere'
THINGSPEAKURL = 'https://api.thingspeak.com/update'

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#Change to your device path(s)
lst = []
lst.append('/sys/bus/w1/devices/28-000001/w1_slave')
lst.append('/sys/bus/w1/devices/28-000002/w1_slave')
lst.append('/sys/bus/w1/devices/28-000003f/w1_slave')

#num_sensors = len(lst)
#print "Number of sensors: ", num_sensors

def get_temp(device):
    #To read the sensor data, just open the w1_slave file
    f = open(device, 'r')
    data = f.readlines()
    f.close()
    deg_f = ''
    if data[0].strip()[-3:] == 'YES':
        temp = data[1][data[1].find('t=')+2:]
        #If temp is 0 or not numeric an exception 
        #will occur so lets handle it gracefully
        try:
            if float(temp)==0:
                deg_f = 32
            else:
                deg_f = (float(temp)/1000)*9/5+32
        except:
            print "Error with t=", temp
            pass
    return deg_f
date_log = datetime.datetime.now().strftime("%m-%d-%Y %H:%M")

#device_name = lst[0].split('/')[5]
#When there are multiple devices, a short pause 
#interval between reading sensors seems to work best
intake = str(get_temp(lst[0]))
time.sleep(1)
exhaust = str(get_temp(lst[1]))
time.sleep(1)
pcexhaust = str(get_temp(lst[2]))

def sendData(url,key,field1,field2,field3,temp1,temp2,temp3):
  """
  Send event to internet site
  """

  values = {'key' : key, 'field1' : temp1, 'field2' : temp2, 'field3' : temp3}

  postdata = urllib.urlencode(values)
  req = urllib2.Request(url, postdata)

  log = date_log + ","
  log = log + intake + ","
  log = log + exhaust + ","
  log = log + pcexhaust + ","

  try:
    # Send data to Thingspeak
    response = urllib2.urlopen(req, None, 5)
    html_string = response.read()
    response.close()
    log = log + 'Update ' + html_string

  except urllib2.HTTPError, e:
    log = log + 'Server could not fulfill the request. Error code: ' + e.code
  except urllib2.URLError, e:
    log = log + 'Failed to reach server. Reason: ' + e.reason
  except:
    log = log + 'Unknown error'

  print log

sendData(THINGSPEAKURL,THINGSPEAKKEY,'field1','field2','field3',intake,
         exhaust,pcexhaust)
sys.stdout.flush()

You will have to change the field1, field2, etc. to match the number of fields and their titles to fit your setup. As you can see from the code above, I have three temp sensors, one near the intake for the door, one near the exhaust in the ceiling, and one near the exhaust near the PC. I hope that this helps.


#9

I am looking for the answer for this question using plotly.

I have a csv file that gets updated often and I want to plot the graph on local server and extend the graph when new csv file is updated.

Haven’t found the basic implementation yet. If someone can help would be very helpful.

Thanks