Monday, September 21, 2009

Setting linux time from your Cell Phone

I have a Sony z610 cell hooked up to my wgt634u via usb running openwrt. As this box has no internet connection, on power failure it has the wrong date/time.

As the z610 has it's own battery I wish to set my system time based on the z610 clock.

the z610 has no support for NITZ or getting it's time from the cell provider, but this is ok.

the z610 appears as ttyACM0 and ttyACM1. I use smsd pointing at ttyACM1 , so my clock script is free to use ttyACM0.

As this is openwrt, and mipsel based, perl modules like serialport.pm are broken (termios.h etc.) so instead I use ser2net to share the port via tcp, then use perl sockets to access the serial port. This is actually deceptively easy. In perl I used Net::Telnet instead of sockets as this was even easier ! (I didn't get the expected results with sockets either).

Perl Script is easy
----------
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10);
$t->open(Host => "localhost", Port => 2008);
@lines = $t->print("AT+CCLK?");
$line = $t->getline;
$line = $t->getline;
print $line;
-----------

to set system date with the shell and this script

date -s "20`perl /etc/getcelltime.pl | grep CCLK | cut -b 9-25 |tr "," " " | tr "/" "-"`"

Thursday, September 3, 2009

watchdog script for openwrt

sometimes my 3g based openwrt system gets in a broken state, and as it is going to be remote, I need a watchdog script to reboot it if the internet connection is broken

This is a bash script that runs from boot

---------------
#!/bin/sh

ping -4 -c 3 -I ppp0 www.google.com | grep -v '0 packets received' | grep 'packets received' || (ping -4 -c 3 -I ppp0 www.yahoo.com | grep -v '0 packets received' | grep 'packets received' || reboot)

sleep 60

/bin/sh /etc/config/watchdog.sh >> /dev/null 2>&1 &

---------------

it is called from /etc/rc.d/S95done