Tag Archives: Raspberry Pi

Arduino ethernet hardware watchdog

So I have a few raspberry pis that (3 in fact!), and sometimes have them set up for remote access such as a raspberry pi webcam using the raspberry pi camera, web servers, remote power socket control, weather monitoring, energy usage – the usual geeky stuff.

Various raspberry pi projects of mine!

Anyway, sometimes the thing runs smoothly and reliably for years on end without problems (usually the less you tinker with it!), surviving power cuts and what have you. My first Model B Rev 1 Pi (thanks Dad!) is an example of these. It ran for years monitoring temperature/humidity and air pressure, before finally being shutdown due to a house move (someday i’ll have to resurrect it!).

Sometimes however, things don’t go to plan and Linux will crash or lock up, or the network interface will go down on power cuts. For instance I once wrote a script to restore the wifi connection of a pi in my GF’s locked and empty house using a lovely blackberry smartphone while sitting in a buddy’s car eating kfc chicken outside. But that’s another story! (Thanks Andy!). It usually happens right when you’re away the Pi and trying to login remotely!

I tried using the raspberry pi watchdog timer (see here: Raspberry pi Watchdog timer), but this didn’t seem to fix the problem

So I built an Arduino hardware watchdog that keeps an eye on the ping result of a host on the network, and reboots the device if it fails tor respond. IT has a 16×02 lcd to display current action/state although someday i’d like to have leds for each state instead. with a button to manually reboot.

The project utilized a remote control socket (from Maplin, RIP), an arduino uno with an ethernet shield and a 16×02 lcd shield (with pin 10 bent out to prevent issues between the ethernet shield and the lcd shield backlight) plus a 433mhz tx module and the rcswitch library.

Maplin/Wilko remote sockets, available here 

code below

/*
Ping Example

This example sends an ICMP pings every 500 milliseconds, sends the human-readable
result over the serial port.

Circuit:
Ethernet shield attached to pins 10, 11, 12, 13
433mhz tx module on pin 2
LCD on pins 8, 9, 4, 5, 6, 7

Pin 10 of the LCD bent out of socket to prevent issues caused by the backlight

created 30 Sep 2010
by Blake Foster

Modified by Garreth Tinsley

*/
const int txDataPin = 2;

#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
#include <RCSwitch.h> // for 433 tx module/remote switch control
#include <LiquidCrystal.h> // For LCD
long retrytimeout = 15L * 1000L; //recheck every 15 seconds
long cycletimeout = 120000;
long starttime;
long currtime;

bool looptwice = false;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
//byte ip[] = {192,168,2,177}; // ip address for ethernet shield
IPAddress pingAddr(192, 168, 1, 2); // ip address to ping

SOCKET pingSocket = 0;

char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
RCSwitch mySwitch = RCSwitch();
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // initialize the LCD library with the numbers of the interface pins

void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(“Rebooter 0.2”);
//0.1 it works!
//0.2 it works an is neater (lcd printing countdown, ip addr print not . after last octet, padding seconds)
lcd.setCursor(0, 1);
lcd.print(“starting…”);
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“IP DHCP CONFIG”);
lcd.setCursor(0, 1);
lcd.print(“Failed! Halting.”);
// no point in carrying on, so do nothing forevermore:
while (true) {
Serial.println(“no dhcp, halted”);
delay(1000);
};
}

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“IP DHCP CONFIG”);
lcd.setCursor(0, 1);
printIPAddress();
delay(2000);

//start the rc tx module
mySwitch.enableTransmit(txDataPin);
}

void loop()
{
lcd.clear();
ICMPEchoReply echoReply = ping(pingAddr, 4);
if (echoReply.status == SUCCESS)
{
lcd.setCursor(0, 0);
lcd.print(“Ping succeeded.”);
sprintf(buffer,
“Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d”,
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() – echoReply.data.time,
echoReply.ttl);

starttime = millis();
currtime = starttime;
long prevtime = starttime;

while (currtime <= starttime + cycletimeout) {
if (currtime >= prevtime + 1000) {
//update the lcd every second
lcd.setCursor(0, 1);
lcd.print(“retest in: “);
lcdPrintSecondsQuad(((starttime + cycletimeout) – currtime) / 1000L);

//lcd.print(“s”);
prevtime = currtime;
}
delay(100);
currtime = millis();
}

}
else
{
sprintf(buffer, “Echo request failed; %d”, echoReply.status);
lcd.setCursor(0, 0);
lcd.print(“Ping failed”);

//ensure ping has failed twice consecutively
if (looptwice == true) {

lcd.setCursor(0, 1);
lcd.print(“Switching off…”);
/* See Example: TypeB two rotary */
mySwitch.switchOff(4, 4);

delay(3000);

lcd.setCursor(0, 1);
lcd.print(“Switching on…”);
mySwitch.switchOn(4, 4);

lcd.clear();
lcd.print(“Waiting 1 minute”);
lcd.setCursor(0, 1);
lcd.print(“please wait…”);
looptwice = false;
//delay(cycletimeout);

starttime = millis();
currtime = starttime;
long prevtime = starttime;

while (currtime <= starttime + cycletimeout) {
if (currtime >= prevtime + 1000) {
//update the lcd ever second
lcd.setCursor(0, 1);
lcd.print(“reteest in: “);
lcd.print(((starttime + cycletimeout) – currtime) / 1000L);
lcd.print(“s”);
prevtime = currtime;
}
delay(100);
currtime = millis();
}

}
else {
lcd.setCursor(0, 1);
lcd.print(“retest in 15s”);

looptwice = true;

starttime = millis();
currtime = starttime;
long prevtime = starttime;

while (currtime <= starttime + retrytimeout) {
if (currtime >= prevtime + 1000) {
//update the lcd ever second
lcd.setCursor(0, 1);
lcd.print(“retest in: “);
lcdPrintSecondsQuad(((starttime + cycletimeout) – currtime) / 1000L);
prevtime = currtime;
}
delay(100);
currtime = millis();
}

delay(retrytimeout);
}
}
Serial.println(buffer);
}

void printIPAddress()
{
Serial.print(“My IP address: “);
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
lcd.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(Ethernet.localIP()[thisByte], DEC);
if (thisByte < 3) { //dont dot if last octet
lcd.print(“.”);
Serial.print(“.”);
}
}

Serial.println();
}

void lcdPrintSecondsQuad(int digit)
/* A ten percent reduction in input voltage
will cause the lower bound to drop by ten.
This sketch is calibrated for ~4.9-5.1v
using the linksprite LCD shield. */
{
if (digit < 10)
{ // 9s
lcd.print(” “);
lcd.print(digit, DEC);
lcd.print(“s”);
}
else if (digit < 100)
{ // 99s // 10s
lcd.print(” “);
lcd.print(digit, DEC);
lcd.print(“s”);
}
else if (digit < 1000)
{ //999s //100s
lcd.print(digit, DEC);
lcd.print(“s”);
}
else if (digit > 999)
{ //1000s
lcd.print(digit, DEC);
}
}

Links to guides for some of the projects i’ve tried:

  • Raspberry ambilight clone for Raspbmc/OSMC (Kodi on Linux) – Hyperion Project:

    WS2812b leds providing ambilight like backwash

  • Raspberry pi hardware watchdog timer (reboot on crash using kernel module, only requires raspberry pi, no external hardware) – Raspberry pi Watchdog timer

Ps sorry this post is long and a bit rambly, it was written across a few years!

PPS if anyone knows how to get wordpress to display C++/Arduino sketches nicely with syntax highlighting etc, please let me know in the comments!

[UPDATED] G Projects

My friends Pi, after repairs to the sd card holder.

[purple means updated on Sunday Feb 2nd 2014]

My pi in its latest incarnation:
image

The other day a good friend and fellow geek of mine acquaintance was reminding me that it’s not possible to do everything we want to do, and actually harmful to try and shoehorn everything in. Someone told him this, and he passed it along.

It got me thinking (and googling – what doesn’t these days?)
Are you a Perfectionist? Do you find yourself telling yourself this: ‘I can’t relax till i get it all done’?

If so we are kindred spirits!

Anyway, here’s all the projects that I’d like to complete:

Raspberry Pi

WebUI

V2 of the User interface.

  • Prettify
    • Layout does not work in IE9. – Who cares?! No one likes IE9 anyway!
    • Embedd/add link to xively
      • Can we get image of all feeds on one graph?
      • Change layout to 4 column 3 row to add this?
        • enough space?
    • apply css to sockets page
    • get that annoying favicon to work! (maybe it doesn’t like the filetype?
      • save as gif/make it smaller and reupp/change mimetype
  • Test – new squeezeplayer page needs testing
  • Develop ?python? code to allow setting of Home Automation parameters
    • E.g. on/off times per DOW

Pressure sensor:

  • Write Exponential moving average code to smooth the values. I found the code somewhere…refind and figure out
    how to store the data to be ema’d
    variable – can bash hold arrays?

    rrdtool – effort bruv
    use xively api to read old datapoints – ditto
  • Descriptive weather prediction – e.g. ‘rain coming’ [Just today it rained, so I have worked out the min time resolution needed to predict/try and deduce if it is raining]
  • Absolute Humidity calculations – output moisture density of air to see how much has been added(RH% is not a good measure of the amount of water bc it’s relative to temperature, and I want to find how wet the air is!)
  • Solder to Slice of Pi board

i2c LCD Screen:

  • Connect to Pi on fly lead (attach to pibox?)
  • Output status – e.g. current sensor data
  • Output interpreted status – e.g. ‘it’s getting hot in here’, Nice weather coming etc
  • Implement a simple UI with pushbuttons ->gpio
    I actually designed the UI while in a meeting!
    functions like shutdown, remote power control, network configuration/join wifi,slimclient/server control?

RFM12b 433mhz wireless transceiver module:

  • Connect to pi (on fly lead? bad reception has been problematic on the transceiver according to susa.net
  • Implement data decoding from CurrentCost transmitter
  • Control remote sockets ala home automation

Streaming Music Server – Plug and play, wireless capable – Using Squeezebox/LogitechMediaServer

Remit: To be able to rock up somewhere, plug in power, and then play music wirelessly from my collection
(Extra: stream from the internet if connection available)

  • Stream music from Pi
  • Control Music from Nook using Squeezebox Android app
  • Stream music to my Hauppage MediaMVP (wirelessly [using WEP])

Progress: 

  • Got MediaMVP to boot from Pi when pi was using wired network
    • tftpd-hpa (modify to port 16869)
    • compiled mvprelay.c 
    • wrote init.d script for mvprelay to startup and point to ip address of pi
      • Need to find a way of dynamically setting ip
      • (altho when setup on wifi, up of pi will be statically fixed)
    • downloaded mvpmc image to boot on the MVP
    • wrote a dongle.bin.config file for the MVP to load it’s config at boot and start the squeezebox ‘mclient’
      • this should enable headless playback, so won’t need a tv screen for Video
    • made a dongle.bin.ver file using dd
    • booted it

Issues/todo: 

It wouldn’t boot over the network bridge. It talked to mvprelay using UDP; tftpd-hpa gave ‘ACK connection refused, could not read’ weird errors when it tried to download the files.
My laptop however could suck the files up no problem. Weirdness!

Todo: investigate settings on Bridge AP looking at what happens to BC packets. And also check DHCP relay settings.
I think they might be flooding the network somewhat.

  1. Setup Pi hostapd: Configure the dhcpd config file
  2. Repair the wifi dongle: The usb socket is no longer solidly connected!! Need to dremmel the plastic off and resolder, then melt and reattach the plastic, (it’s meltable) but a lot tighter this time!
  3. Get the donle working ok in debian/raspbian. (haha, good luck)

 

I can’t do it all………………….

at least not all today!

Actually, part of me likes it going slowly (certainly a surprise to the rest of me!). Time to mull things over, make decisions, come up with sketched designs and sometimes even pseudocode/real code.

Seems to have it’s advantages not trying to do everything right now at least haha.

On that note, bed time! [ 2:03 am this time]