Monday, 27 July 2015

Tide Indicator Pi Project #1 - Scraping the tidal data for St Helier - Update

A quick update to post 1 on this project.

This is the data from the Jerseymet site and it looks like it is indexed well and the required data sits in nice blocks.


Sunday, 26 July 2015

Tide Indicator Pi Project #1 - Scraping the tidal data for St Helier

I have a plan to build a live tide indicator. This will lift tide data from the web, interpret and extrapolate it to work out the current tide height, and then output this data live to the web and a physical indicator. 

This is the data on the UK Hydrographic Office site:




(http://www.ukho.gov.uk/easytide/EasyTide/ShowPrediction.aspx?PortID=1605&PredictionLength=7)





and here's the html to scrape:


 

Tidal info is also avalable here: http://www.portofjersey.je/Pages/tides.aspx


and here*: http://www.gov.je/Weather/Pages/Tides.aspx

*Of these it actually looks like this last one has the neatest, most 'scrapable' formatting and the current days tides will always be at the same position on the page.


To scrape the data I initially tried this: 

#python program to import tide data from a website
import urllib2

#open site
rawhtml = urllib2.urlopen("http://www.ukho.gov.uk/easytide/EasyTide/ShowPrediction.aspx?PortID=1605&PredictionLength=7").read(20000)

print (rawhtml)

Which collected the text from the site, but it appeared tricky to extract the meaningful data.

I googled and found this: http://docs.python-guide.org/en/latest/scenarios/scrape/

But it wasn't to easy to install the lxml module using pip but this worked:

sudo apt-get install python-lxml

Next job is to learn how to extract particular bits of data.

Sunday, 14 June 2015

Being a 21st Century Teacher

I LOVE being a 21st century teacher! 

By most measures, teaching in general, and teaching computing in particular, is easier and more fun than ever before. A bold statement perhaps?  For me, the way technology facilitates community is key.

For starters, social media allows me to join a great club of fellow professionals. Entry criteria: a twitter account and a willingness to learn and share.

With the advent of resource-sharing sites like TES and primaryresources.co.uk, not to mention our own site, there really is no excuse for poorly planned, boring lessons.

If I get stuck and need to learn something new myself, well, just like Neo in the Matrix you can get on Youtube and learn just about anything (it can take a bit longer than it did for Keanu). For example I learned lots of Kodu tricks from @GeekyNicki and returned the favour with some Kodu and Flowol tutorials of my own.

In a typical day I can wake up to inspirational tweets from the likes of @urban_teacher@markbarnes19 and @ICTEvangelist. I can teach lessons from plans I've found and adapted from CAS, TES or computing super heroes like Simon Haughton, and in the evening I can study Computer Science at MIT.

I can't imagine, now, not being able to connect quickly and easily with the best educators in my chosen field.

Teaching from just your own experience, behind a closed door, from a book? 

That's so 20th century, darling!


Thursday, 16 April 2015

Pi-based Domestic Electricity Monitor - Part 7 - Project stalled indefinitely.

For now this project has been halted while I work on other things to develop my skills further, especially with databases and web publishing.

Eventually I hope to buy a cheap model A Pi and get this project up and running again.


Thermobot Update

Video of Bot in operation.



The thermobot also stores and plots 24 hours of data. It's finished its job now, but was logging live every minute to a webpage. You can see its results from my conservatory for 16th April 2015 here:

http://jcwyatt.ddns.net/thermobot.html

Initially, pyplot was producing a wierd colouring-in effect under the graph, but adding   

plt.clf()
 
to reset pyplot after each plot fixed the problem. I discovered this accidentally.

the updated code to include the data logging and plotting is here:


import RPi.GPIO as GPIO
import time
import matplotlib
matplotlib.use('agg')
import os
import matplotlib.pyplot as plt


#pseudo code:
#   fill a list with 60 zeros
# set the t1_temp variable to 18 deg c
# every minute:
# get the t2_temperature from the chip
# ext:write the temperature to a list or database
# calc the temperatures difference from previous value
# move the bot an amount relative to the temp difference

#initialise bot settings (stepper motor))
GPIO.setmode(GPIO.BCM)
 
enable_pin = 18
coil_A_1_pin = 7
coil_A_2_pin = 8
coil_B_1_pin = 23
coil_B_2_pin = 24
 
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
 
GPIO.output(enable_pin, 1)
 
delay = 2

def forward(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(1, 0, 0, 1)
  time.sleep(delay)
 
def backwards(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(1, 0, 1, 0)
  time.sleep(delay)
 
def setStep(w1, w2, w3, w4):
 GPIO.output(coil_A_1_pin, w1)
 GPIO.output(coil_A_2_pin, w2)
 GPIO.output(coil_B_1_pin, w3)
 GPIO.output(coil_B_2_pin, w4)

#from http://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi:
#load drivers for temperature probe
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-0414607111ff/w1_slave'

#Get the raw data from the sensor:
def temp_raw():

 f = open(temp_sensor, 'r')
 lines = f.readlines()
 f.close()
 return lines

def read_temp():

 lines = temp_raw()
 while lines[0].strip()[-3:] != 'YES':
  time.sleep(0.2)
  lines = temp_raw()
        
 temp_output = lines[1].find('t=')

 if temp_output != -1:
  temp_string = lines[1].strip()[temp_output+2:]
  temp_c = float(temp_string) / 1000.0
  return temp_c

#initialise current temperature
currentTemp = 14.00

#make an empty list for 24 hours worth of data
tempLog = [0] * (60*24)

#run the bot
while True:
  oldTemp = currentTemp
  currentTemp = read_temp()
  tempDiff = currentTemp - oldTemp
  if tempDiff >= 0:
   steps = tempDiff * 256
   forward(int(delay) / 1000.0, int(steps)) 
  else:
   steps = tempDiff * -256
   backwards(int(delay) / 1000.0, int(steps))
  setStep(0,0,0,0)

#now do the graph stuff:
#add the new reading to the end of the list 

  tempLog.append(currentTemp)

#delete the first item in the list

  del tempLog[0]

#plot the graph as an image    
  
  plt.plot(tempLog)
  plt.ylabel ('Bedroom Temperature (deg C)')
  plt.xlabel ('Time (minutes) (Full width = 24hrs)')
  plt.savefig('/var/www/images/tempLog.png')
  plt.clf()

  hrlog=(tempLog[-60:]) 
  plt.plot(hrlog)
  plt.ylabel ('Temp (deg C)')
  plt.xlabel ('Time (minutes) last hour')
  plt.savefig('/var/www/images/tempLog60.png')
  plt.clf()

  time.sleep(60)
 

Pi Science Project: Tank Power - Part 1 - Outline of project

New Project: Tank Power

Aim:

To establish whether, in the summer, I should heat my hot water tank using electricity via the immersion heater or use gas via the central heating boiler. 

Hypothesis: Electricity is a more efficient way to heat the water in the tank because when using gas, there is heat loss via the pipes transporting the heat from the boiler to the tank and also heat lost from the boiler flue pipe.

 

Proposed Method:

Use Raspberry Pi and temperature sensor to log the temperature of the tank by fitting the sensor to the outside of the tank. Record the value every 2 minutes. By looking for when the temperature is rising, the length of time for which energy is being supplied to the tank each day can be measured. 

For gas heating, if the quantity of gas used during the same 24hr period is known, the energy consumption can be calculated. 

For electrical heating, if the power consumption of the the heater is known then the energy consumption can be calculated.

Other factors should be revealed, for example the rate of heating for gas compared to electricity and how well the thermostats work at regulating the temperature.

Step 1: Rig up the pi logging the temperature of the tank with the sensor taped to the outside to gather preliminary data. 

draft pseudo code:

#import sqlite to write the temperature and time data to a file
#import matplotlib to write the data  to graphs
#import OS  to access the temperature sensor

#if it doesn't exist create the database
#open the database

#read the current temperature and time

#write time and temp to the end of db

#get the data from the db (how much?)

#1) last 24 hours
#2) last 6 hours
#3) last 3 hours

#plot each graph to a .png for showing on a database

#data analysis extension:
#import last 24 hours data (midnight to midnight)
#read temperature values
#if temperature is rising start the timer
#if temperature is falling stop the timer
#might need to average these values out to allow for small fluctuations
#add the timer value to total 'on' time
#return a value for total time of rising temperature in 24 hrs
#calculate the theoretical value of energy used vs actual?
#for electrical this is easier done by the program if the power rating of the heater is known
#for gas would need to input the meter reading for the 24hour period and do some calcs.







 


Monday, 30 March 2015

Thermo-Bot Project




In this post, I describe how I took a stepper motor, some Lego(R) and a temperature probe to make a temperature sensitive robot. Basically it's a Pi-on-wheels that moves to the correct position on a temperature scale to indicate the current room temperature. Don't ask why I thought this was a good idea, but I'm quite proud that it works!



Video of it working here (time lapse of 50 minutes) https://youtu.be/bbkwAPPF_vY


I bought a stepper motor with a view to making some kind of robot.

This tutorial from Simon Monk on the Adafruit site is all you need for Pi-controlled stepper motors:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-10-stepper-motors

I bought my stepper motor from ebay with the controller chip included on a board with some LEDs, which were really helpful in debugging, since the wires in the Adafruit diagram don't 'line up' obviously with the Pi GPIO pins. My motor would only go forwards until I followed the diagram MUCH more carefully. (The fault was with me not Simon's diagram.)

I also did some work with the single wire temperature sensor, again using the Adafruit tutorial from Simon Monk:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing

Although some of the Python code came from here:

http://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi:


However, this time it wasn't my fault when it didn't work. With model B+ Pis you have to add this to the /boot/config.txt file:

dtoverlay=w1-gpio,gpiopin=4

Once this was in place the sensor worked fine.

The robot is cobbled together really quickly (badly) using lego. Initial belt-drive version had problems with the elastic-band drive belt jumping off the drive wheel, so direct drive was used instead.

Then it was time to combine the two tutorials and with a few extra lines of code I have a robot that moves up and down a temperature scale according to the current room temperature.


Here's the code I used. You'll notice most of it is pretty much left as per the Adafruit example and the modmypi link above, so credit to Simon Monk and the ModMyPi guru.



import RPi.GPIO as GPIO
import time
import os

#initialise bot settings (stepper motor))
GPIO.setmode(GPIO.BCM)
 
enable_pin = 18
coil_A_1_pin = 7
coil_A_2_pin = 8
coil_B_1_pin = 23
coil_B_2_pin = 24
 
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
 
GPIO.output(enable_pin, 1)
 
delay = 2

def forward(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(1, 0, 0, 1)
  time.sleep(delay)
 
def backwards(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(1, 0, 1, 0)
  time.sleep(delay)
 
def setStep(w1, w2, w3, w4):
 GPIO.output(coil_A_1_pin, w1)
 GPIO.output(coil_A_2_pin, w2)
 GPIO.output(coil_B_1_pin, w3)
 GPIO.output(coil_B_2_pin, w4)

#from http://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi:
#load drivers for temperature probe
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-0414607111ff/w1_slave'

#Get the raw data from the sensor:

def temp_raw():

 f = open(temp_sensor, 'r')
 lines = f.readlines()
 f.close()
 return lines

def read_temp():

 lines = temp_raw()
 while lines[0].strip()[-3:] != 'YES':
  time.sleep(0.2)
  lines = temp_raw()
        
 temp_output = lines[1].find('t=')

 if temp_output != -1:
  temp_string = lines[1].strip()[temp_output+2:]
  temp_c = float(temp_string) / 1000.0
  return temp_c

#initialise current temperature start value (and place bot on 14 deg C mark before running)
currentTemp = 14.00


#run the bot
while True:
  oldTemp = currentTemp
  currentTemp = read_temp()
  tempDiff = currentTemp - oldTemp
  print (oldTemp, currentTemp, tempDiff,)
  if tempDiff >= 0:
   steps = tempDiff * 256
   forward(int(delay) / 1000.0, int(steps)) 
  else:
   steps = tempDiff * -256
   backwards(int(delay) / 1000.0, int(steps))
  setStep(0,0,0,0)
  time.sleep(60)
  
 
It works!