Tag-Archive for » wrt «

Samedi, septembre 27th, 2008 | Author: admin

Sometimes, it could be handy if your router (in this case, a WRT54G) could restart the device that connects it to the internet. For instance, if your ADSL modem needs some reboot or your cable modem needs a kick, having the WRT to do this would be nice.

This could be especially useful if the modem+router are deployed as an access point somewhere in the wild.

This usage is not limitating : we’re gonna build a WRT managed relay. What you switch on/off with this relay is your problem. A modem seems a reasonable idea, but you could switch a coffee pot (in this case, you should go with HTCPCP [http://www.ietf.org/rfc/rfc2324.txt]), lights, etc…

All this is possible with little modification. The behaviour of the system here is to switch the relay for approx. 14 seconds, but this time is also easy to change. You can even use a trim pot instead to make it fully tunable.

Schematics

Relay board schematic

Relay board schematic

The schematic is rather simple. The board is connected to the internal WRT54G serial port (TX pin). This pin uses LV-TLL signals (0, +3.3V), and logic high is 0V. In other words, when nothing is send out to the serial port by the WRT, the level is low and the TX pin show 3.3V. What we want is to activate the relay when voltage on this pins drop. The trick is to feed the TX pin to the Threshold pin of a 555 timer (see those excellent 555 links in Electronics Club and Mecanno Electronique in french). And this is what happens when we the WRT sends data to the serial port : square wave comes out of second serial port TX pin and triggers the 555. For this to work, the 555 is powered at 6V, and thus triggered when TX voltage drops below 2V (approx 1/3 of VCC). The 6V feed is very primitive and comes from WRT power (12V) sliced in half by a voltage divider. It would be wiser to use a voltage regulator (7805 or al.), but we’re going the easy way here.

The trigger from the 555 is fed at the base of the 2N2222 transistor which in turns feed the coil enough current to switch.

Primitive, as I said.

Assembling it

First, be aware that doing the bad things below will void your WRT warranty, your modem warranty. You also can burn things or yourself in the process, etc… So do at your own risk. This being said, that stuff has been my first electronics project so it is really easy. “my first electronics project” should also be taken as a warning.

  • Open your WRT54G (or GS, or GL) by pulling the blue cover out
  • Take the WRT PCB out.
  • Solder a wire in the TX pin (pin 3) of the serial port

Soldering this port is really easy, even I did it without breaking anything or melting the router to a shapeless plastic blob. You’ll find more info and pictures on the LinkSys WRT5G/GS Dual Port Mod Page. You don’t need IDC headers or high end stuff like this. Just solder in the hole. It will be ok as long as you don’t pull too much on the wire.

The blue front cover has holes inside. These holes can’t be seen from the outside because of the stickers. This is a good place to make a complete hole with a hot wire if you plan to put your PCB relay outside the WRT. For recent models, like the GL, the PCB can probably fit inside.

  • Pull the TX cable through the blue cover and assemble the WRT back
  • Solder the wires with the DC power jacks on the board
  • Cut power cables from modem and WRT. Be sure to cut it at the right place ! You need enough cable to plug the wall wart and enough cable to plug the modem and WRT.
  • Remove some insulating material from the power wires and insert them in the screm clamps. Don’t mess with polarity or which wire go where. You would burn something for sure.
  • Now we need software to get this going. The basic idea is to ping some IP address, and when consecutive failures are detected, the relay is activated and the stuff behind it reboots. You can push the shell script to the WRT running Sveasoft firmware under Administration/Diagnostocs/Run, then paster script and click “Save Startup”. You can do the same with OpenWRT and probably with other firmwares.

wrt.sh


#!/bin/sh

########### Tunable Variables #############
# which host to check
checkhost="212.27.48.10"

# how often to check
checkinterval=20

# how many failures to activate relay
activateafter=3

# where is the wrt_relay_mod
# serial=/tmp/test
serial=/dev/tts/1

# how long should we rest after switching
rest=120

# how many log/debug lines do we keep
keeplines=20
########### End Variables #################

#
pingfail=0
logfile=/tmp/wrm.log
debuglogfile=/tmp/wrm_debug.log
version=1
#

log() {
echo -n `date` >> $logfile
echo " : "${1} >> $logfile

# trim log file
head -n 8 $logfile > $logfile.tmp
tail -n $keeplines $logfile >> $logfile.tmp
mv $logfile.tmp $logfile
}

debug() {
echo -n `date` >> $debuglogfile
echo " : "${1} >> $debuglogfile

# trim debug file
tail -n $keeplines $debuglogfile > $debuglogfile.tmp
mv $debuglogfile.tmp $debuglogfile
}

check_loop() {
# we sleep $checkinterval seconds
sleep $checkinterval

if ping -qn -c 1 $checkhost > /dev/null ; then
# network is ok
pingfail=0
debug "/-\ => ping ok"
sleep $checkinterval
else
# network seems down
pingfail=`expr $pingfail + 1`
debug "/?\ => $pingfail ping failures"

if [ $pingfail -ge $activateafter ]; then
log “relay switched”
debug “/!\ => switching relay…”
echo ACTIVATE > $serial
debug “/-\ => resting for $rest seconds”
pingfail=0
sleep `expr $rest - $checkinterval`
fi
fi
}

# log few information at boot
log “######################################”
log “# wrm daemon version $version starting”
log “# checkhost=$checkhost”
log “# checkinterval=$checkinterval”
log “# activateafter=$activateafter”
log “# serial=$serial”
log “# rest=$rest”
log “######################################”

# initial rest
sleep $rest

# control loop
while true; do check_loop; done

Tune variables at your liking. You can see how the relay is software activated : it just writes ‘ACTIVATE’ to the device driving the second serial port. What you write is not important. ’saddam’ and ‘george’ work equally well for instance. You should also restrain yourself from logging too much. The idel situation for deployment is to disable debug and logging. Space is scarce on the WRT.

Here is the final thing assembler, with the driver modem (a Terayon TJ720). The system works fine but isAssembled setup quite static sensitive. So discharge yourself somewhere before touching the running circuit, or the relay might activate. Also, proper casing should be done. Putting the circuit inside the WRT is possible with latter models (WRT54GS and WRT54GL). This hasn’t been tester though, and might put some noise inside.

Bill of materials

Building this stuff shouldn’t take more than 2 hours and if you wish to buy parts, it will cost less than 4€ (3.0€ each by 20 at GoTronic in France).

Here is the list of components required :

Component Quantity
MINIATURE RELAY 6-12V/12V-2A 1
TWO WAY TERMINAL BLOCK 2
1K RESIST. CARBON 1/4-5% 2
47K RESIST. CARBON 1/4-5% 1
33K RESIST. CARBON 1/4-5% 1
TRANSISTOR NPN 50V-0.2A 1
CAPACITOR 10n 1
CAPACITOR 220u 1
CAPACITOR 100u 1
DIODE 1A 1N4007 or eq. 1
DIODE 1N4148 or eq. 1
NE555 TIMER 1
LED 5MM RED (not in schematics) 1
LEDS 5MM GREEN (not in schematics) 1
PROTO BOARD (approx 20 holes x 20 holes) or PCB to etch 1

Besides the board and may be the 555, the rest is easily scavengeable.

Category: electronics, projects  | Tags: ,  | Leave a Comment