Howto: draw charts from sensor log file with python & matplotlib


#1

@Webb.Peter As requested, here’s a post on making charts. Rather than narrate what I’ve been doing with Go and dynamically generated SVGs, I thought it would be more useful to make an example GitHub repo with a simple Python program that demonstrates how to parse my sensor log and turn it into a matplotlib chart.

How to try the demo

  1. Make sure you have matplotlib. On macOS, it’s already there. On Raspbian, do this:
    sudo apt-get update && sudo apt-get install python-matplotlib

  2. Clone my repo of example code:
    git clone https://github.com/wsnook/farm-chart-demo.git

  3. Read the code and the comments in chart.py. In short, it’s opening a sample sensor log file that I created with my farm controller, looking for records from 23 May 2017, extracting the Si7021 °C and %RH measurements, and turning them into charts with matplotlib.

This is a copy of the sample chart from the example repo:

How to apply this to an MVP

  1. Do something to save sensor data to a log file. You could run my farm controller and my Photon firmware, or you could make up something of your own. You don’t need to use my record file format, but if you use some other format, you’ll need to modify my parsing code.

  2. Modify my script to take a date as a command line argument or maybe have it use datetime.today().

  3. Set up nginx and modify my script’s output file to go in /var/www/html or whatever. You might need to make sure your user is in the right group to have write permissions for the web server’s web root directory.

  4. Make a cron job to run the script at whatever interval you’d like the chart to update.

cc @webbhm, @rbaynes


MVP - Product Design
#2

I updated the demo with additional code to configure the x and y axis limits, labels, and grid spacing. Now it looks like this:

[edit: I also switched my farm controller over to using a fancier version of the matplotlib demo chart maker. The wsnook/farm version of charts.py will show C, %RH, and hPa from a BME280 if one is available. Failing that, it will try to show C and %RH from an Si7021. The charts look like this:

]

[edit # 2: I also fixed a bug in the demo that could cause problems on Raspbian if you don’t have X running.]


#3

We could pretty this up using D3. Make it ready for consumer/education market.