Blog
2013-07-11
On 19 June, the USB temperature sensor I ordered from Amazon arrived. This sensor is now hooked up to my Raspberry Pi, which is taking the temperature every 10 minutes, drawing graphs, then uploading them here. Here is a brief outline of how I set this up:
Reading the temperature
I found this code and adapted it to write the date, time and temperature to a text file. I then set cron to run this every 10 minutes. It writes the data to a text file (/var/www/temperature2) in this format:
2013 06 20 03 50,16.445019
2013 06 20 04 00,16.187843
2013 06 20 04 10,16.187843
2013 06 20 04 20,16.187843
2013 06 20 04 00,16.187843
2013 06 20 04 10,16.187843
2013 06 20 04 20,16.187843
Plotting the graphs
I found a guide somewhere on the internet about how to draw graphs with Python using Pylab/Matplotlib. If you have any idea where this could be, comment below and I'll put a link here.
In the end my code looked like this:
python
import timeimport matplotlib as mpl
mpl.use("Agg")
import matplotlib.pylab as plt
import matplotlib.dates as mdates
ts = time.time()
import datetime
now = datetime.datetime.fromtimestamp(ts)
st = mdates.date2num(datetime.datetime(int(float(now.strftime("%Y"))),
int(float(now.strftime("%m"))),
int(float(now.strftime("%d"))),
0, 0, 0))
weekst = st - int(float(datetime.datetime.fromtimestamp(ts).strftime("%w")))
f = file("/var/www/temperature2","r")
t = []
s = []
tt = []
ss = []
u = []
v = []
g = []
h = []
i = []
weekt = []
weektt = []
weeks = []
weekss = []
mini = 1000
maxi = 0
cur = -1
datC = 0
for line in f:
fL = line.split(",")
fL[0] = fL[0].split(" ")
if cur == -1:
cur = mdates.date2num(datetime.datetime(int(float(fL[0][0])),
int(float(fL[0][1])),
int(float(fL[0][2])),
0,0,0))
datC = mdates.date2num(datetime.datetime(int(float(fL[0][0])),
int(float(fL[0][1])),
int(float(fL[0][2])),
int(float(fL[0][3])),
int(float(fL[0][4])),
0))
u.append(datC)
v.append(fL[1])
if datC >= st and datC <= st + 1:
t.append(datC)
s.append(fL[1])
if datC >= st - 1 and datC <= st:
tt.append(datC + 1)
ss.append(fL[1])
if datC >= weekst and datC <= weekst + 7:
weekt.append(datC)
weeks.append(fL[1])
if datC >= weekst - 7 and datC <= weekst:
weektt.append(datC + 7)
weekss.append(fL[1])
if datC > cur + 1:
g.append(cur)
h.append(mini)
i.append(maxi)
mini = 1000
maxi = 0
cur = mdates.date2num(datetime.datetime(int(float(fL[0][0])),
int(float(fL[0][1])),
int(float(fL[0][2])),
0,0,0))
mini = min(float(fL[1]),mini)
maxi = max(float(fL[1]),maxi)
g.append(cur)
h.append(mini)
i.append(maxi)
plt.plot_date(x=t,y=s,fmt="r-")
plt.plot_date(x=tt,y=ss,fmt="g-")
plt.xlabel("Time")
plt.ylabel("Temperature (#^\circ#C)")
plt.title("Daily")
plt.legend(["today","yesterday"], loc="upper left",prop={"size":8})
plt.grid(True)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.gcf().subplots_adjust(bottom=0.15,right=0.99)
labels = plt.gca().get_xticklabels()
plt.setp(labels,rotation=90,fontsize=10)
plt.xlim(st,st + 1)
plt.savefig("/var/www/tempr/tg1p.png")
plt.clf()
plt.plot_date(x=weekt,y=weeks,fmt="r-")
plt.plot_date(x=weektt,y=weekss,fmt="g-")
plt.xlabel("Day")
plt.ylabel("Temperature (#^\circ#C)")
plt.title("Weekly")
plt.legend(["this week","last week"], loc="upper left",prop={"size":8})
plt.grid(True)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter(" %A"))
plt.gcf().subplots_adjust(bottom=0.15,right=0.99)
labels = plt.gca().get_xticklabels()
plt.setp(labels,rotation=0,fontsize=10)
plt.xlim(weekst,weekst + 7)
plt.savefig("/var/www/tempr/tg2p.png")
plt.clf()
plt.plot_date(x=u,y=v,fmt="r-")
plt.xlabel("Date & Time")
plt.ylabel("Temperature (#^\circ#C)")
plt.title("Forever")
plt.grid(True)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%d/%m/%y %H:%M"))
plt.gcf().subplots_adjust(bottom=0.25,right=0.99)
labels = plt.gca().get_xticklabels()
plt.setp(labels,rotation=90,fontsize=8)
plt.savefig("/var/www/tempr/tg4p.png")
plt.clf()
plt.plot_date(x=g,y=h,fmt="b-")
plt.plot_date(x=g,y=i,fmt="r-")
plt.xlabel("Date")
plt.ylabel("Temperature (#^\circ#C)")
plt.title("Forever")
plt.legend(["minimum","maximum"], loc="upper left",prop={"size":8})
plt.grid(True)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%d %b"))
plt.gcf().subplots_adjust(bottom=0.15,right=0.99)
labels = plt.gca().get_xticklabels()
plt.setp(labels,rotation=90,fontsize=8)
plt.savefig("/var/www/tempr/tg3p.png")
If there's anything in there you don't understand, comment below and I'll try to fill in the gaps.
Uploading the graphs
Finally, I upload the graphs to mscroggs.co.uk/weather.
To do this, I set up pre-shared keys on the Raspberry Pi and this server and added
the following as a cron job:
bash
0 * * * * scp /var/www/tempr/tg*p.png username@mscroggs.co.uk:/path/to/folder
I hope this was vaguely interesting/useful. I'll try to add more details and updates over time. If you are building something similar, please let me know in the comments; I'd love to see what everyone else is up to.
Edit: Updated to reflect graphs now appearing on mscroggs.co.uk not catsindrag.co.uk.
(Click on one of these icons to react to this blog post)
You might also enjoy...
Comments
Comments in green were written by me. Comments in blue were not written by me.
Add a Comment
2012-11-02
This is the second post in a series of posts about tube map folding.
Following my previous post, I did a little more folding.
The post was linked to on Going Underground's Blog where it received this comment:
In response to which I made this from 48 tube maps:
Also since the last post, I left 49 tetrahedrons at tube stations in a period of just over two weeks. Here's a pie chart showing which stations I left them at:
Of these 49, only three were still there the next time I passed through the station:
Due to the very low recapture rate, little more analysis can be done. Although I do wonder where they all ended up. Do you work at one of those stations and threw some away? Or did you pass through a station and pick one up? Or was it aliens and ghosts?
For my next trick, I want to gather a team of people, pick a day, and leave one at every station that day. If you want to join me, comment on this post, tweet me or comment on reddit and we can formulate a plan. Including your nearest station(s) in your message will help us sort out who takes which stations...
Previous post in series
This is the second post in a series of posts about tube map folding.
Next post in series
(Click on one of these icons to react to this blog post)
You might also enjoy...
Comments
Comments in green were written by me. Comments in blue were not written by me.
Add a Comment
2012-10-06
This is the first post in a series of posts about tube map folding.
This week, after re-reading chapter two of Alex's Adventures in Numberland (where Alex learns to fold business cards into tetrahedrons, cubes and octahedrons) on the tube, I folded two tube maps into a tetrahedron:
Following this, I folded a cube, an octahedron and an icosahedron:
The tetrahedron, icosahedron and octahedron were all made in the same way, as seen in Numberland: folding the map in two, so that a pair of opposite corners meet, then folding the sides over to make a triangle:
In order to get an equilateral triangle at this point, paper with sides in a ratio of 1:√3 is required. Although it is not exact, the proportions of a tube map are close enough to this to get an almost equilateral triangle. Putting one of these pieces together with a mirror image piece (one where the other two corners were folded together at the start) gives a tetrahedron. The larger solids are obtained by using a larger number of maps.
The cube—also found in Numberland—can me made by placing two tube maps on each other at right angles and folding over the extra length:
Six of these pieces combine to give a cube.
Finally this morning, with a little help from the internet, I folded a dodecahedron, thus completing all the Platonic solids:
To spread the joy of folding tube maps, each time I take the tube, I am going to fold a tetrahedron from two maps and leave it on the maps when I leave the tube. I started this yesterday, leaving a tetrahedron on the maps at South Harrow. In the evening, it was still there:
Do you think it will still be there on Monday morning? How often do you think I will return to find a tetrahedron still there? I will be keeping a tetrahedron diary so we can find out the answers to these most important questions...
This is the first post in a series of posts about tube map folding.
Next post in series
(Click on one of these icons to react to this blog post)
You might also enjoy...
Comments
Comments in green were written by me. Comments in blue were not written by me.
Add a Comment
























