Wednesday, January 30, 2013

Watchdog timer for Raspberry Pi

Just a quick note: I set up the watchdog timer facility on the Raspberry Pi this evening following the directions at http://raspberrypi.stackexchange.com/questions/1401/how-do-i-hard-reset-a-raspberry-pi pretty much verbatim. Here's an excerpt from the blog, signature at end:



You can use the BCM2708's hardware watchdog.
To use it begin by loading the module:
sudo modprobe bcm2708_wdog
Then edit the /etc/modules file:
sudo nano /etc/modules
and add the following line:
bcm2708_wdog
Next you will need to setup the watchdog daemon.
Install and confiigure it to start on bootup:
sudo apt-get install watchdog chkconfig
chkconfig watchdog on
sudo /etc/init.d/watchdog start
Next configure watchdog:
sudo nano /etc/watchdog.conf
# Uncomment the line watchdog-device = /dev/watchdog
The watchdog daemon will send /dev/watchdog a heartbeat every 10 seconds. If /dev/watchdog does not receive this signal it will restart your Raspberry Pi.



The daemon monitors the state of various things defined in the file /etc/watchdog.conf, including the existence of several files,  successful pings, etc. and stops signaling the watchdog timer if they don't exist, which resets the Raspberry Pi.

Thursday, January 24, 2013

New Raspbian “wheezy” installed on 8 GB SanDisk SDHC card

I obtained an 8 GB SD card and downloaded and installed using windows 7 a later drop of wheezy (2012-12-16-wheezy-raspbian.zip) on it using Win32DiskImager.

After booting I had to download and install avahi-daemon ('sudo apt-get install avahi-daemon') as before, but unlike with the previous version first I had update using 'apt-get update' due to some daemon libraries being missing.

Remote logins to the pi account were blocked due to a host key problem in the Pi. Here's the error message:


john@john-ThinkPad-T42:~$ ssh pi@raspberrypi.local
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
d4:21:93:57:3e:18:79:9f:d3:f4:99:00:9a:c1:fe:59.
Please contact your system administrator.
Add correct host key in /home/john/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/john/.ssh/known_hosts:12
  remove with: ssh-keygen -f "/home/john/.ssh/known_hosts" -R raspberrypi.local
ECDSA host key for raspberrypi.local has changed and you have requested strict checking.
Host key verification failed.
john@john-ThinkPad-T42:~$ ^C



The new version has no root account, so privileged operations require sudo, which the 'pi' user has the ability to use.

I used raspi-config to change the keyboard to the 104 key pc style and expand the disk partition to the full 8 GB available on the SD card. 

The new version also shows the full 512 MB of ram, unlike the previous version.

Tuesday, January 22, 2013

Raspberry Pi Received

I just got a Raspberry Pi, model B, and am trying some stuff. The idea is to use it instead of the Beaglebone in a less expensive home monitor.

I received from MCM Electronics Inc a "Raspberry Pi cased product", which is a Pi in a clear plastic case, a 4 GB SD card with Debian 6 for Linux already installed, and a 5V 1A power adapter with a micro USB connector. Total including shipping was $76.80.

I had an HP LCD monitor with an HDMI cable, a USB mouse and a USB keyboard.

I simply connected these to the Pi, powered it on, and it booted to a text-based shell. Running startx brought up the windowing system.

I had a wireless adapter with an ethernet cable, and after connecting this to to Pi ethernet jack I had to do an ifup command to eth0 to bring up the network (only the first time, however.)

So, the board had access to the network, but addressing it (for ssh use) required knowing its IP address. the ifconfig command provided that, and I could ssh to it.

Zero config allows addressing it using local names like raspberrypi.local, and it seems that avahi-daemon was not installed. So I did apt-get install avahi-daemon to get it, and that seemed to install and run the daemon:

$ sudo apt-get install avahi-daemon

The network name is raspberrypi.local. From another computer, such as my laptop running Ubuntu, you can log on to the board thusly:

$ ssh pi@raspberrypi.local

The pi password is raspberry.

The next thing was to get the host-based javascript engine node.js running, since I'd like to program in javascript. (My monitor software is currently written in tcl, which comes preinstalled on the Pi, but I plan to transition to javascript.) There is a package for node:

$ sudo apt-get install nodejs

After installing node, I wrote the standard web server using the http module, and ran it:

$ nodejs webserver.js

Server running at http://raspberrypi.local:1337/

webserver.js:

var http = require('http');
var counter = 0;
http.createServer(function (req, res) {
  counter = counter + 1;
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('From Raspberry pi for the ' + counter + ' time: Hello World\n');
}).listen(1337);
console.log('Server running at http://raspberrypi.local:1337/');

The next job will be to install the usb-modeswitch software so I can run the USB GSM modem as a serial device (HSO.) I don't have a modem here, since I am in New Hampshire and the modem is doing its job back in Maryland, so I'll delay installing that software until I get a modem and sim card to play with.


Wednesday, November 21, 2012

Watchdog timer again

The last post outlined a code change that pinged a watchdog timer periodically so that when the program hung the system would reboot. That has been working well so far.

However, it also allowed the system to be reset/rebooted via a command, which I was unable to do via any previously known method. Simply put, to reset the board, while the /dev/watchdog file is still open, simply delay for more than the timer timeout period (about 60 seconds.) The timer will go off and cause a board reset.

I added a command, sent via a text message, that causes just this timeout and subsequent reset/reboot. So, now I can reset/reboot the board by simply sending that command.

Monday, November 5, 2012

Further Bullet Proofing: watchdog timer

Having implemented the previously described circuit and associated code to reset the GSM modem via a USB power cycle when communications with it shut down, there were still occasions when the beaglebone board would lock up, and cease further communications with either the network or the USB-based terminal gadget.

So, I decided that a watchdog timer would provide a last line defense against this sort of "crash." The beaglebone has a special watchdog timer that can be used for just this purpose. When the file /dev/watchdog is open, absence of a write to it for more then about one minute will cause the entire board to reset and reboot.

I added code to beagelgsm.tcl that opened the file just after startup, and then wrote to it once each main loop, which seemed from inspection to provide much more frequent writes than required to keep the watchdog timer from timing out and resetting the board. I added code to measure loop time and log any times greater than about 30 seconds, to get a sense of what the loop times were.

The system is now up and running, as of about 6 PM November 5, 2012.

Tuesday, August 14, 2012

Beaglebone Analog Input read() results in 4 bytes

After writing some C code to read the analog inputs, it became clear that reading analog inputs via the file system (.../ain1, etc.), the system returns 4 bytes for each read. The bytes are the ASCII characters for the digits 0-9 (values got from 0-4095), and if the reading is less than 4 digits, the byte = \x00 is placed in trailing locations. The \x00 byte is the string terminator for C, and is not present for readings that are 4 digits long. This could explain why channels shift when reading the analog values via Tcl reads: the area the 4-byte value is read into might not have a string terminator.

So, the technique is to read into a buffer of 5 or more bytes, then make sure the 5th byte is a \x00 by either setting the whole area to \x00 first or storing \x00 there after the read, then the C string will be properly terminated.

Friday, August 10, 2012

Recoding Analog and GSM Modem drivers in C

I have referred to problems with the analog inputs shifting channels and GSM modem hangups often in the blog. Since I've been coding in Tcl, I thought that it might be hiding some behavior or data behind its own layer of software. To investigate this I recoded the analog input and GSM modem interface in C language. Since I've done so I've not seen any problems, but then I've only written test programs. I still need to rewrite the bulk of the monitor in C.

I've been off doing other stuff here in New Hampshire and will not be back to this for a week or so. More then hopefully.