Tuesday, 29 November 2016

Pi Light and Movie Quote Alarm Clock

Sorry, No documentation on this one. Rough and ready test program.

Turns a blue LED on in the morning (and evening..don't ask) to wake me up, but also plays a random selection from a bank of quotes.  

from gpiozero import LED
from time import sleep
import datetime
import os
import random

led = LED(4)


print (pmOff)
while True:
        while overRide=='auto':

                soundchoice = str(random.randint(1,5))
                playsound=('aplay '+soundchoice+'.wav')
                if timeNow>pmOn and timeNow<pmOff or timeNow>amOn and timeNow<amOff:
                        print('led on')
                        if soundTrigger==1:
                        print('led off')

Live Energy Consumption in a 'Google Guage'

The author can't find a way to make this visualisation public. Wierd user interface at Thingspeak.

Monday, 28 November 2016

Electracker - Analysis of 2 weeks of data

Earlier this month I got serious about logging my home electrical energy consumption.

The Pi tasked with this job has run continuously for two weeks, happily logging data:


And is still doing so.

I wanted to analyse this data to get an average picture of the daily consumption, by hour:

A typical weekday looks like this:

The code is here:


The only thing I'd do to update this is to plonk the overall average consumption for the time period shown as a data box on the graph, e.g. "Average = 0.871 kW" for the main graph.

Wednesday, 16 November 2016

Circles in Minecraft - Minecraft Pi and Python

Circles in Minecraft?

Using Minecraft Pi, you can write code to place blocks. This means, in theory, you can code objects you could never build accurately - like giant circles.

I had a go at some maths around this in a spreadsheet using the formula for a circle

where x and y are coordinates.

It came out like this:

Which is circular if nothing else.

I don't like the gaps near the axes but with some clever programming (which I'll have to google!) I reckon eventually I could get rid of them.

Trig Method

I realised the numbers above follow 2 overlapping sine waves, so a quick search revealed this:

"A circle can be defined as the locus of all points that satisfy the equations
x = r cos(t)    y = r sin(t)
where x,y are the coordinates of any point on the circle, r is the radius of the circle and 
t is the parameter - the angle subtended by the point at the circle's center."
from  http://www.mathopenref.com/coordparamcircle.html (there's an interactive applet)

So  by carefully choosing the angular interval 't' you should be able to put blocks precisely where you want them in the circle.

Here's my first go in the spreadsheet:

You can see there are still gaps and the numbers in the columns are all over the place. Think the trig functions are working in radians but I entered angles in degrees. 

Using radians instead of degrees:

To plot more points, I'd have to reduce the interval in column 1 to give more points between 0 and 2.

Imagine that in Minecraft! 

I've a Pi 3 that I've just set up to run headless, so I'll move onto that next.....

The Results!

nested loop of expanding rising circles. Feels like an amphitheatre inside.

Simple cylinder - circles on top of circles
This one was weird - a cylinder with different block types in each layer. Unfortunately 2 of them were lava and water.
One massive circle. 

 The code for these:

from mcpi.minecraft import Minecraft
import math

mc = Minecraft.create()
#get players position

#move out of the way, there's something coming
mc.player.setPos (x,y+50,z)

r=15 #radius factor

for q in range (1,4):  #nested loop for when you want multiple circles
    for i in range(0,r*6):  #sets how many blocks comprise the shape
        j = float(i)/(r*3)  #gives max j of 2 (to give  2*pi radians for trig))

        blockXPos = r*math.cos(j*math.pi)  #sets x coordinate of block
        blockZPos = r*math.sin(j*math.pi)  #sets z coordinate of block

        mc.setBlock(blockXPos+x,y+q,blockZPos+z,1) #places a block in the circle

You could use code like this to plot other mathematical functions. Even 3D ones:

Its something called a quadric surface:

Something about that makes me feel like I've earned a glass of wine!

Tuesday, 15 November 2016

Electracker - Domestic Energy Consumption Logger

I've written about this project before but I revived it after a visit to Jersey Tech Fair and meeting the nice chaps at Jersey Electricity. They tried to sell me 'Nest', a home heating automation system (they may have succeeded. Watch this space.) But we also discussed electricity consumption.

I've had a Pi attached to my electricity meter for over a year now, but doing nothing.
Now rekindled, it seems to be happily tracking my energy usage and logging the results at Thingspeak.

I'm going to leave it for a bit and then plan to do a frequency analysis on a few days worth of data.


I also added this widget to my phone:


So now I can see live updates of my energy consumption as long as I have wifi/3G:

The code is here at github:


Things I've learnt:

1) The wifi connection was really flaky - an Edimax attached to a Pi Zero. I've replaced the Edimax with a bulky Netgear with an ariel but this seems quite erratic too. Tried a better power supply.  Seems slightly better now. Made it a real pain to SSH into the Pi and also writing to Thingspeak.

2) You can't have it all ways (yet). I was initially recording every interval between flashes, and getting a really accurate reading, but couldn't write this to Thingspeak as it took too long and caused missed readings and errors due to the delay.
Now I'm counting the number of flashes in a 2 minute period and writing a calculated consumption based on this to Thingspeak. Advantages are that it give a consistent rate of readings, which looks great on Thingspeak, and at high consumption rates it should be very accurate, albeit for an average consumption over 2 minutes. However at low consumption rates the accuracy drops off; the small number of flashes per 2 minute interval kills the resolution on the measurements.


Some kind of hybrid where it records the interval between flashes and averages this out over 2 minutes, and then writes this average data to TS.
Should be possible:

while True:
     Reset total time to 0
     Reset flashes to 0
     Wait for a flash
     Start the timer
     For 2 minutes:
            Wait for a flash
            stop the timer
            record the flash interval
            start the timer
            flashes +=1
            total time = total time + flash interval
            wait 0.07s to check the LED is off again
      average = totaltime/flashes
      Consumption = 1.125/average
      Write Consumption to Thingspeak