Temps to html

Fri, 20 Sep 2024 16:49:12 GMT

So in an earlier post I talked about setting up a pi zero to do temp and humidity recording

Today we will take this output and convert it to an html file to post on a local webserver.
We will also automate this process and add an archiving operation in case we want to go back and look at past data

First you will need to install a couple of things on the pi.
1. a webserver. I am using nginx as it is light on resources and the pi zero isnt very fast
2. the pandas python library to do the converting so

sudo apt install nginx python3-pandas

Next make a python script. We will call it csvconv.py
nano csvconv.py

## Python program to convert
## CSV to HTML Table

import pandas as pd

## to read csv file named "temps.csv"
a = pd.read_csv("temps.csv")

## to save as html file
## named as "temps.html"
a.to_html("temps.html")

## assign it to a
## variable (string)
html_file = a.to_html()

press ctrl o to save and ctrl x to quit

next make a bash script to do the html conversion and add some header and footers

nano dohtmltemps.sh

#!/bin/bash

cp ftemp.txt temps.csv
rm temps.html
python csvconv.py

cat gheader.html temps.html gfooter.html > index.html
cp index.html /var/www/html/

ctrl o and ctrl x as before to save

(by default the /var/www/html folder is writable only as root you will need to get around this so the script can write the file.)
(you could chmod +w -R the folder but if you will have this webserver connected to the internet that is a security risk.)

the gheader.html and gfooter.html are simple html snippets to make the page more complete.

make yours as fancy as you like.

next I made a script to run weekly to archive the data for posterity.
nano arctmp.sh

#!/bin/bash

echo ftemp.txt > "ftemp-$(date +"%m-%d-%Y").txt"
echo temps.csv > "temps-$(date +"%m-%d-%Y").csv"

sleep 5
cp ftheader.txt ftemp.txt

This simply appends the date to the file name.

Finally you want to setup cron to record the temp hourly, make the html file nightly and archive the temp data weekly.
crontab -e to edit the crontab

Here is what I have in mine

*/60 * * * * /home/pi/temprec.sh

46 23 * * * /home/pi/dohtmltemps.sh

50 23 * * 0 /home/pi/arctmp.sh

enjoy!