Saturday, 12 March 2016

BirdBox2016: what's happening?

There has been plenty of interest shown by a number of local blue tits in both of our Pi powered bird boxes.


I've also added some enhancements to our bird box monitor which sits in a corner of our kitchen and raises the alarm if a box is triggered by a visitor.


It is the 12th March, so we may only be 2 or 3 weeks away from some real nest-building action!

Once again, it looks like the old 2014 plywood bird box, with it's embarrassingly badly made roof, is attracting most of the interest. While the rather better constructed 2015 box does get triggered most days, the potential tenants spend more time actually going in and looking around the older box.

A few days ago (while waiting for the kettle to boil) I looked out of the kitchen window and noticed a great tit at the entrance of the 2014 box, which is attached to our Canadian maple tree. However, this bird was quickly chased away by a smaller, but more aggressive blue tit. And certainly all captured video clips to date only feature blue tits.

Usually the male (I assume) enters first, shortly followed by the female. The male then quickly leaves and takes up guard duty just outside the box.

The bird box monitor


I'm using my TontecPi (the one with the 7" LCD) to monitor the two Pi bird boxes.


It uses a "ping" type program which I wrote in Gambas a few years ago. This "pings" the two Pi bird box systems to see if they are on, and also checks that the Time Server Pi is also on line, as this provides the real time clock necessary to reset the box systems.

The monitor announces "Pergola bird box, on" and/or "Maple tree bird box, on" via eSpeak, and the recent addition of a small audio amplifier. The amp is based around an NJM2073D 8 pin chip. I'll probably write more about this in a subsequent post.

Well, that's embarrassing!


The monitor also displays an html page with the embedded video streams from both boxes. If you use Firefox or its little twin Iceweasel (as I do on this Pi) you have almost certainly seen the message "Well, that's embarrassing" when it tries to start using reloaded tabs/pages.

This is due to Firefox reporting an issue or something it is not happy about concerning the pages it has been asked to re-load. The user is invited to start a new session or just continue.

This happens a lot with the browser pointing to my video feeds, which is a problem because I don't want to leave a keyboard attached to my bird box monitor.

But there is an easy solution. Just go to the Firefox/Iceweasel configuration page by typing into the browser address:-

about:config

...and then go down the page and locate:-

browser.sessionstore.resume_from_crash

...and set to: false

Restarting or shutting down my TontecPi


Another problem with not having an attached keyboard, is that I sometimes want to restart/reboot the Pi. As my TontecPi looks butt-ugly anyway, I'm not bothered about drilling holes in it and attaching extra bits.

So I decided to add a little red push button to allow the Pi to be restarted.  ....and hey! while I'm at it, I may as well allow the switch to optionally shut the Pi right down!

The red push button switch is attached to the Pi gpio physical pins 9 (0V) and 11 (wiringPi pin 0). From pin 11 I have a 10k resistor running to +5V, but this could go to +3.3V if you are bothered by such things.

I started a small Gambas project with a single form. The form is only used as a message box, so I've made it fixed size, without a border.

When the button is pressed, the form is displayed above all other windows, and the user is invited to press again for restart, and yet again for shutdown.

The form includes a timer, three radio buttons and 3 labels.

The Gambas code looks like this:-

Library "/usr/local/lib/libwiringPi"

Public Extern wiringPiSetup() As Integer
Public Extern digitalRead(pin As Integer) As Integer
Public Extern digitalWrite(pin As Integer, value As Integer)
Public Extern pinMode(pin As Integer, pud As Integer)

Public intSeconds As Integer
Const POWER_SWITCH As Integer = 0
Const SWITCH_PRESSED As Integer = 0


Public Sub Form_Open()
  With Me   'centre form on all desktops & send to back
    .Sticky = True
    .Border = False
    .Resizable = False
    .SkipTaskbar = True
    .Left = (0.5 * Screen.Width) - (0.5 * .Width)
    .top = (0.5 * Screen.height) - (0.5 * .height)
    .Window.Stacking = Window.Below
    lblHeader.Width = .Width
    lblStatus.Width = .Width
  End With
  If (wiringPiSetup() = -1)
    lblStatus.Text = "Failed to setup wiringPi. This program must be run as root"
    FMain.Window.Stacking = Window.Above  'a problem, so window on top
  Else
    lblStatus.Text = "WiringPi initialised OK"
    pinMode(POWER_SWITCH, 0)
    lblHeader.Text = "Do you want to restart or shutdown?"
    Timer1.Delay = 400
    Timer1.Start()
 Endif
   
Catch
  lblStatus.Text = Error.Text
End

Public Sub Timer1_Timer()
Dim intSwitch As Integer
 
  intSwitch = digitalRead(POWER_SWITCH)
  If intSwitch = SWITCH_PRESSED Then
    Timer1.Stop()
    rbDoNothing.Value = 1
    FMain.Show()
    FMain.Window.Stacking = Window.Above
    Wait 3   'give the user time to read message & react
    intSwitch = digitalRead(POWER_SWITCH)
    If intSwitch = SWITCH_PRESSED Then
      rbRestart.Value = 1
      Wait 3  
'give the user time to read message & react
      intSwitch = digitalRead(POWER_SWITCH)
      If intSwitch = SWITCH_PRESSED Then
        rbShutdown.Value = 1
        lblMessage.Text = "SHUTTING DOWN NOW!"
        Wait 3     'display message then shutdown
        Exec ["sudo", "shutdown", "now", "-h"]
      Else
        lblMessage.Text = "RESTARTING NOW!"
        Wait 3     'display message then restart
        Exec ["sudo", "shutdown", "now", "-r"]
      Endif
    Endif
    lblMessage.Text = "press & hold button to change selection"
    FMain.Window.Stacking = Window.Below
    FMain.Hide()
    Timer1.Start()   
  Endif
 
End


As you can see, I'm using a timer to check the condition of the switch every 400ms (this is not critical, but I want it to be reasonably responsive without sucking up all the cpu time).



Once the button is pressed, the program just runs through a number of 3 second delays, allowing the user enough time to scan the text and make a decision.

No comments:

Post a Comment