The Free Site   |  vBuddy - make friends, share photos, blogs, have fun   |  Cheap Web Hosting - starting at $5

Postbox code

One problem with home automation is that there are lots of sensors detecting all sorts of things. If a PC checks the sensors too often it ties up the PC as serial port communications hang a lot of other systems.The solution is to have a postbox picaxe that collects data from all sensors. The PC periodically asks if this postbox picaxe has any new data, and if it does, then the new data is uploaded.

The system uses a single wire-or common data bus. Any sensor system can put data onto this bus using a 914 diode. Sometimes data will clash with other sensors, and if a sensor really has important data it just sends it a few times.

In addition, the PC also puts data onto the common bus again using a 914 diode. The PC RS232 data goes through the picaxe 22k/10k circuit then two HC04 inverters in series, then the 914 diode.

Picaxes are either 'doing' picaxes or 'sensing' picaxes. Doing picaxes listen on the bus for their address, which generally comes from the PC but could theoretically come from any other picaxe on the bus. Sensing picaxes detect things and put their data on the bus from time to time.

Schematic = PC =>22k/10k download circuit => 2 HC04 inverters => 914 diode => Bus

And Bus => 08M pin1 => 2 HC04 inverters => PC

Bus is grounded with 10k resistor
' postbox 08m
' protocol for common bus - 3 commands - DataOut DataIn and Dump
' DataOut is Data/T, DataIn is Data/I and Dump is Data/D
' PC sends DataOut to data output devices on the bus
' Devices that read data put DataIn data on the bus
' The PC sends a Dump request to the bus which the postbox dumps all its data to
' Each device on the bus has a unique number 0-31
' each DataIn is a max of 4 bytes
' if need more than 4 bytes use two addresses eg 17 and 18 and send 4 bytes for each - ie create a psuedo device
' DataOut is also max 4 bytes
' All data packets must be the same length eg Data+1+1+4=9 bytes
' Data/I/0-31/b3,b4,b5,b6,b12,b13 where I=b1 and 0-31=b2 eg devices that read data send serout 1,N1200,("I",b2,b3,b4,b5,b6,+checksum)
' Data/T/0-31/b3,b4,b5,b6,b12,b13 sent by PC
' Data/D/0-31/b3,b4,b5,b6,b12,b13 - dummy values in b1-b6 but put there because all the same length sent by PC
' The postbox responds to a dump with D/0 or D/255/then 128 bytes (ie only send data if data has changed)
' keep the message short back to the PC if no change, so can go back to listening to the bus (so doesn't miss anything)
' run data at 1200 baud - not sure if can go several hundred metres at faster rates
start: serout 0, N1200, ("Startup")


main: serin 1, N1200, ("Data"), b1, b2, b3, b4, b5, b6, b10, b11 ' b1="D" "I" or T, b2=device, b3-6=data, b7,8=checksum LSB 1st
If b1 = "D" Then ProcessDumpPeek
If b1 = "I" Then ProcessInputPoke
GoTo main



NoChange: Let b1 = 0
pause 200
serout 0, N1200, ("N")
GoTo main


ProcessInputPoke: ' rewritten as there are a limited number of writes before the emprom dies
' has less registers =80 to 127, the old code had 127 , new dump code has dummy bytes
Let w6 = b3
Let w6 = w6 + b4
Let w6 = w6 + b5
Let w6 = w6 + b6
If w5 <> w6 Then main ' checksum is invalid
Let b2 = b2 * 4 ' sets address
If b2 < 45 Then ' make sure 0-47 only otherwise wierd bugs if write to other registers
b7 = 80 ' hex $50 = start of registers
b2 = b2 + b7
poke b2, b3
b2 = b2 + 1
poke b2, b4
b2 = b2 + 1
poke b2, b5
b2 = b2 + 1
poke b2, b6
End If
Let b9 = 1
GoTo main

ProcessDumpPeek: ' replaces code above
If b9 = 0 Then NoChange ' if no inputs received then send this back
Let b1 = 255
serout 0, N1200, ("Y")
' short pause for VB to catch up
Let w5 = 0 ' checksum
pause 200
For b1 = 80 To 127
peek b1, b2
serout 0, N1200, (b2)
w5 = w5 + b2
Next b1
' sent 48 bytes, send dummy ones now
b2 = 0
For b1 = 0 To 79
serout 0, N1200, (b2)
Next
serout 0,N1200,(b10,b11) ' checksum out
Let b9 = 0 ' reset change pointer
GoTo main


Home