DashboardPi


#1

Hi,

Introduction: Python is amazing, thanks to powerful libraries. Sense HAT is one of the best HATs you can get, what happens if we take both of them?. Matplotlib enables us to create plots with any data we want, so we can analyse data obtained from the Sense HAT.

Background: I use Initial State and I love it, so I recommend to everyone. However, the free account is a bit limited and I have no money to spend in a suscription (so much geeky things out there to buy). I started thinking on how to do something close to it but on my own. With a Virtual Host, running on Apache2 and some HTML code I took from out there I created a little online dashboard. Code doesn’t explain this, it only shows how to create graphs but I can help anyone if needed. The code is as follows:

#!/usr/bin/env python
#-*-coding:utf-8-*-
#This program creates graphs with the temperature and data obtained by the SenseHAT.
#Created by Iker García.

import csv
import datetime
import matplotlib
matplotlib.use("Agg") #Added to plot graphs without running X server.
import matplotlib.dates as md
import matplotlib.pyplot as plt
from sense_hat import SenseHat
import time

x = [] #Define list needed for the plot.
y = []

while True:
  try:
    sense = SenseHat()
    temp = sense.get_temperature() #Reads temperature from SenseHAT.
    temp = round(temp, 2)
    now = datetime.datetime.now()
    file = open("/var/www/dashboard/temperature.csv", "a") #Opens file to save data.
    writer = csv.writer(file, delimiter = ",")
    data = [now,temp]
    writer.writerow(data) #Writes data on the csv file.
    x.append(now) #Updates list for the plot.
    y.append(temp)
   
    plt.plot(x,y) #Plots data set.
    plt.gca().xaxis.set_major_formatter(md.DateFormatter("%Y-%m-%d %H:%M:%S")) #Format the date.
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) #Format y axis, in order not to get scientific units.
    plt.xlabel("Date") #x axis label.
    plt.ylabel(u"Temperature (\u00B0C)") #y axis label.
    plt.gcf().autofmt_xdate()#Correct format of the date on the axis.
    plt.savefig("/var/www/dashboard/images/1.png") #Save the plot.
    plt.clf() #Clears the plot, in order to get a tidy plot. 
    time.sleep(3600) #Code is executed each hour.
  except KeyboardInterrupt:
    break

I think everything is explained in the same code, so you can see how it works. Anyway, you can ask whateveryou want. The name this time is DashboardPi.

Here is the repo: https://github.com/IkerGarcia/DashboardPi. Contributions are welcome!

Now, you can find another two examples in the repo and I plan to add new ones in the future. I will probably show this in a future issue of The Mag Pi, once I finish writing this I will start adding new versions to the repo.

Acknowledgments: John D. Hunter created Matplotlib, he passed away on 2012. If any scientific paper is published using Matplotlib, he shoud be cited. Also, donations can be made (hopefully I would do one in a future). In the meantime, I believe that any project made with this library should be done in memory of him.


#2

Hi,

I must apologize for bringing this topic up again but the project is featured in this month’s MagPi issue #46. Just in case you want to have a look at it.

Regards.


#3

Hi,

Excuse me again for posting three times in a row. However, I have just added a new feature: animated plots.

Hope you like it,

Regards.