24h timer with web interface

This is the software for the tuxgraphics.org
Tuxgraphics.org ethernet host watchdog. See http://tuxgraphics.org/electronics/

Copyright of most software and all diagrams: Guido Socher

License for timer: GPL V2
See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
License ip-stack and driver:LGPL V2
See http://www.gnu.org/licenses/old-licenses/lgpl-2.0.html

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

 All parts needed to build this AVR ethernet solution can be ordered from 
 http://shop.tuxgraphics.org/                                          
 Note that the magnetics need to fit exactly the requirements of the enc28j60
 chip. You can not just take any transformer.

The relay must be connected via pin PD7.

An optional push button may be connected between PD6 and GND.
You can add an extra 4K7 resistor between PD6 and +3.3V to
make the pin less sensitive to electro magnetic interference. Especially
if there is a longer cable between the button and the board.
That button can be used to switch the relay on/off locally.
The LED on pin PB1 changes its state every other seconds 
(blinking in 0.5Hz intervals).

Compiling the software

!! READ THIS FIRST !! --------------------- ! Edit the file main.c and change the 2 lines: ! static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x29}; ! static uint8_t myip[4] = {10,0,0,29}; For the first device you build you will not need to change the mymac line. But you will probably need to change the IP address (myip). It must be a free address from the address range in your home network. Use an IP address in the same network as you DSL router. Example: you wlan router might have 192.168.1.1, your PC might have 192.168.1.2. This means you could e.g use 192.168.1.10 and leave some room for more PCs. Edit the beginning of the make file. And adjust those lines according to the CPU you use (atmega328p or atmega644): MCU=atmega328p DUDECPUTYPE=m328p or MCU=atmega644 DUDECPUTYPE=m644 or MCU=atmega644 DUDECPUTYPE=m644p Edit the avrdude command definition: # linux (plug in the avrusb500 and type dmesg to see which device it is): LOADCMD=avrdude -P /dev/ttyUSB0 # mac (plug in the programmer and use ls /dev/tty.usbserial* to get the name): LOADCMD=avrdude -P /dev/tty.usbserial-A9006MOb # windows (check which com-port you get when you plug in the avrusb500): LOADCMD=avrdude -P COM4 More information about Makefiles can be found at: http://tuxgraphics.org/electronics/200912/makefiles.shtml To compile the final software use the command: make Note: windows users may use and modify the supplied winmake.bat to setup the environment correctly. The above command will compile the following hex file: main.hex

Loading the software into the board

The makefile included as well a command to load the hex file using avrusb500 in combination with avrdude. You may use your own method to load the file. make load The "make load" command will run something like: avrdude -p m328p -c stk500v2 -e -U flash:w:main.hex Note: your programmer might interfere with the enc28j60! If you use an avrusb500 then you can normally leave the cable on. For all other programmers it is saver to remove the programmer cable and power down/up the ethernet board. This is because the SPI interface is used for loading of the software and communication to the enc28j60. To see a list of all possible build targets run the command: make help

Selecting the right clock source

Tuxgraphics hardware sold as of march 2007 is prepared to use the clock signal from the enc28j60. To use this you need to change the low fuse byte once from 0x62 to 0x60: avrdude -p m328p -c stk500v2 -u -v -U lfuse:w:0x60:m or avrdude -p m644 -c stk500v2 -u -v -U lfuse:w:0x60:m The above command can as well be executed by using the make file. In a Linux shell you can just type: make fuse Note: windows users may use and modify the supplied winsetfuse.bat script. The fuse settings of the atmega328p are after programming should be as below (default factory settings for atmega328p, the efuse has some not used bits and is dependent on the chip revision): lfuse reads as 0x60 hfuse reads as 0xD9 efuse reads as 0x07 Fuses of the atmega644/atmega644a after programming: Device signature = 0x1e9609 lfuse reads as 60 hfuse reads as 99 efuse reads as FF details of the lfuse bits: CKDIV8 = 0 CKOUT = 1 SUT1 = 1 SUT0 = 0 CKSEL3 = 0 CKSEL2 = 0 CKSEL1 = 0 CKSEL0 = 0 If you are unsure what the current fuse setting are on the atmega then you can read the fuse settings with a command like: avrdude -p m328p -c stk500v2 -v -q The below list of fuses shows for reference purpose the settings of a new and never programmed chip. The result should be this (default factory settings for atmega328p): lfuse reads as 62 hfuse reads as D9 efuse reads as 7 The result should be this (default factory settings for atmega644/atmega644a): Device signature = 0x1e9609 lfuse reads as 62 hfuse reads as 99 efuse reads as FF

Calibrating the clock

At the top of the file main.c you will find a statement like this: #define CLOCK_CAL 2 You can change this number to calibrate the clock. Let the clock run at least for 60 days before you decide what value to use. The CLOCK_CAL is the amount of seconds to add or subtract every 10days.

Compilation on non-Unix systems

Please use the provided Makefile !!! It will also work under Windows. Take a look at the supplied winmake.bat and winload.bat file. The bat script might be needed to set the environment correctly. Take a look at the supplied winmake.bat and winload.bat file. The bat script might be needed to set the environment correctly in order to use the Makefile under windows. Edit the Makefile to make the needed adjustments at the top of the file. More information about Makefiles can be found at: http://tuxgraphics.org/electronics/200912/makefiles.shtml

Using the Timer

Point your javascript capable web browser to http://10.0.0.29 (or what ever IP address you have given your hardware) You should see a page like this: [set] [switch] [refresh] 24h Timer Current time:15:01 Click table fields to change. +-------------------+ +-------------------+ | Time | green=on | | Time | green=on | | | white=off | | | white=off | |-------+-----------| |-------+-----------| | 0:00 | | | 12:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 1:00 | | | 13:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 2:00 | | | 14:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 3:00 | | | 15:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 4:00 | | | 16:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 5:00 | | | 17:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 6:00 | | | 18:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 7:00 | | | 19:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 8:00 | | | 20:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 9:00 | | | 21:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 10:00 | | | 22:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | 11:00 | | | 23:00 | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | |-------+-----------| |-------+-----------| | | | | | | +-------------------+ +-------------------+ One row=15min passw: ___________ [ save ] ---------------------------------------------------------- (c)tuxgraphics

Revision history

2011-02-13: version 1.0 -- First version 2011-02-19: version 1.1 -- correct error in clock calibration algorithm 2011-05-13: version 1.2 -- add gratuitous ARP to work around broken wifi AP clients -- add viewport tag for mobile devices -- increase push button debounce time -- go to main page after change of switch 2011-12-22: version 1.3 -- cleanup after code review 2012-12-01: version 1.4 -- some people seem to have a week power supply and then it can be a problem to do packet handling and relay switching at the same time. We can reduce power consumption by making sure that the enc is idle when switching the relay. 2014-04-09: version 1.5 -- integrate new tcp/ip stack # vim:sw=8:ts=8:si:et