lbenson
Senior Member
While we're waiting for our Raspberry Pi, here's another option for going to the internet from the picaxe via usb serial with a small device running the linux openWrt operating system--the TP-Link WR703N. This tiny computer has usb, an ethernet socket, and wireless, for $24.20US shipped with openWrt pre-installed.
http://www.volumerates.com/product/tp-link-tl-wr703n-openwrt-compatible-pocket-11n-150m-3g-mobile-wireless-broadband-router--blue-ship-with-openwrt-pre-installed-upon-customers-request-103048
Here is my little setup:
I use a usb-to-ttl-serial dongle which also provides 5 volts (or 3.3) to the picaxe. This is available here from ebay for $2.88US shipped: http://www.ebay.com/itm/USB-2-0-UART-TTL-6PIN-Connector-Module-Serial-Converter-CP2102-New-/350499583299?pt=LH_DefaultDomain_0&hash=item519b673543
This particular picaxe board reads a single DS18B20 temperature sensor, but my standard programming allows for 3 temperature sensors per board, so I send changed values as a 1-byte code and three temperatures separated by spaces. In this case, I just repeat the one temperature 3 times, so a reading might be "A 23 23 23". This means that sensor "A" returns a temperature of 23C.
I send a newline charactor (0xa) at the end of each line. This allows for a simple script to read each line, break it into 4 values, and append a time-stamp before appending it to a log file. I have another program running on the TP device every 5 minutes, which reads the log file, updates a web page, and sends an email if the temperature gets out of range high or low. Here is the script for reading the serial input:
For each line input, the output of this script will be something like "A 23 23 23 120127142342". The "stty" command sets the serial parameters--2400 bps, etc.
There is a drawback to openWrt on the WR703N--it doesn't come with the proper serial driver for the usb dongle I use, and it lacks the "stty" command. For most openWrt devices, these commands could be downloaded, but not for the WR703N. I intend to post to the developers to ask that they be included, but in the meantime, it is necessary to build your own version of openWrt--something I have done dozens of times for a half-dozen different devices, but not necessarily trivial for first-time users. If there is interest, I might be able to host an image somewhere which could be flashed.
The picaxe code is not necessarily of much interest--anything which sends serial could be used. My program is a mangled mess which has grown and shrunk over the course of 3 years or so. My latest addition was to try to get rid of dithering around a degree change by doing readtemp12, and making sure that there has been a change of at least a whole degree before sending an update. The program has the remnants of code which was intended to minimize power usage for running on batteries--not an issue now with power from the WR703N's usb port.
The WR703N is connected wirelessly to my network.
This device would also be perfect for implementing the "openbridge" concept of steliosm, as shown here: http://www.picaxeforum.co.uk/showthread.php?15907
http://www.volumerates.com/product/tp-link-tl-wr703n-openwrt-compatible-pocket-11n-150m-3g-mobile-wireless-broadband-router--blue-ship-with-openwrt-pre-installed-upon-customers-request-103048
Here is my little setup:
I use a usb-to-ttl-serial dongle which also provides 5 volts (or 3.3) to the picaxe. This is available here from ebay for $2.88US shipped: http://www.ebay.com/itm/USB-2-0-UART-TTL-6PIN-Connector-Module-Serial-Converter-CP2102-New-/350499583299?pt=LH_DefaultDomain_0&hash=item519b673543
This particular picaxe board reads a single DS18B20 temperature sensor, but my standard programming allows for 3 temperature sensors per board, so I send changed values as a 1-byte code and three temperatures separated by spaces. In this case, I just repeat the one temperature 3 times, so a reading might be "A 23 23 23". This means that sensor "A" returns a temperature of 23C.
I send a newline charactor (0xa) at the end of each line. This allows for a simple script to read each line, break it into 4 values, and append a time-stamp before appending it to a log file. I have another program running on the TP device every 5 minutes, which reads the log file, updates a web page, and sends an email if the temperature gets out of range high or low. Here is the script for reading the serial input:
Code:
#!/bin/sh
stty -F /dev/ttyUSB0 2400 clocal cread cs8 -cstopb -parenb -crtscts
cat /dev/ttyUSB0 2>/dev/null | while read v1 v2 v3 v4; do
echo "$v1 $v2 $v3 $v4 $(date +%y%m%d%H%M%S)" >> /home/user0/Rx01.txt
done
There is a drawback to openWrt on the WR703N--it doesn't come with the proper serial driver for the usb dongle I use, and it lacks the "stty" command. For most openWrt devices, these commands could be downloaded, but not for the WR703N. I intend to post to the developers to ask that they be included, but in the meantime, it is necessary to build your own version of openWrt--something I have done dozens of times for a half-dozen different devices, but not necessarily trivial for first-time users. If there is interest, I might be able to host an image somewhere which could be flashed.
The picaxe code is not necessarily of much interest--anything which sends serial could be used. My program is a mangled mess which has grown and shrunk over the course of 3 years or so. My latest addition was to try to get rid of dithering around a degree change by doing readtemp12, and making sure that there has been a change of at least a whole degree before sending an update. The program has the remnants of code which was intended to minimize power usage for running on batteries--not an issue now with power from the WR703N's usb port.
Code:
' 08TxM2D3f: DS18B20 to dock81 hardwired with cp210x 120123
' C send if change in readtemp12 > 0xF
' or if from/to 0, if >= +1 or <= -1
' inTemp1Pin on 1, out with sertxd on pin 0, 4800
' for wl60, Milton 2nd floor monitor
#picaxe 08M
' task IDs--different for each sensor detector module
symbol MyMsgNo1 = "F" ' DR temp & upstairs temp
symbol MyMsgNo2 = "P" ' Battery level
symbol TxMode = 1 ' 1=3 retries, no Rx; 2=Rx
symbol baudrate = T2400_4
symbol bNegativeFlag = bit0
symbol bLastNegativeFlag = bit1
symbol bSendMsgFlag = bit2
symbol bChangeFlag = bit3
symbol Flags = b0 ' reserved for bit values
symbol MsgNo= b1
symbol inTempPin = b1
symbol passCount = b2
symbol Tmpvar = b3
symbol Temp12 = w2 ' b4 & b5
symbol lastTemp12 = w3 ' b6 & b7
symbol Data1= b8 ' temperature data
symbol Data2= b9 ' temperature data
symbol Data3= b10 ' used for temperature
symbol timePeriod = b11 ' major loop counter of 6-minute periods--240 per day
symbol TempDiff = w6 ' b12 & b13
'output pins 0-5
symbol onLED = 0 ' output
symbol Tx433 = 0 ' output
symbol TxRS232 = 4 ' output
symbol inTemp1Pin = 1 ' ds18b20
'symbol inTemp2Pin = 4 ' ds18b20
'symbol inTemp3Pin = 2 ' ds18b20
symbol ADCON0 = $1f ' ADC control SFR register to turn off ADC module
symbol OSCCON = $8f ' Oscillator control SFR register for speed up/down
' register memory 0x50 - 0x7e (peek/poke--prefix 'r')
symbol rlasttemp1 = 0x50 ' & 0x51
symbol rlasttemp2 = 0x52 ' & 0x53
symbol rlasttemp3 = 0x54 ' & 0x55
start:
'pause 1000
'disablebod
'disconnect
initiate: ' or roll over to new day by rough counting
' sertxd("Module ",MyMsgNo1,CR,LF)
timePeriod = 239 ' set to roll to effective end of 24 hours
' high onLED
pause 4000
' low onLED
sertxd("08TxM2D3",MyMsgNo1,lf)
newday: ' once a day, resend all
lastTemp12 = 0xffff
poke rlasttemp1, word lastTemp12
poke rlasttemp2, word lastTemp12
' poke rlasttemp3, word lastTemp12
' lastTemp12_1 = 254
' lastTemp12_2 = 254
' lastTemp12_3 = 254
main:
do ' this loop completes about once every 6 minutes
' for b2 = 0 to 4
' scratchb13 = rlasttemp1 + b2
' peek scratchb13, lasttemp
bSendMsgFlag = 0
inTempPin = inTemp1Pin
peek rlasttemp1, word lastTemp12
gosub checkTempChange
if bchangeFlag = 1 then
poke rlasttemp1, word lastTemp12
Data1 = Temp12 / 16
bSendMsgFlag = 1
endif
' inTempPin = inTemp1Pin
' peek rlasttemp2, word lastTemp12
' gosub checkTempChange
' if bchangeFlag = 1 then
' poke rlasttemp2, word lastTemp12
' Data2 = Temp12 / 16
' bSendMsgFlag = 1
' endif
if bSendMsgFlag = 1 then
; readtemp inTemp1Pin,Data1
' readtemp inTemp2Pin,Data2
' readtemp inTemp3Pin,Data3
gosub TxData
endif
'sleep 130 ' 5 minutes
'sleep 26 ' 1 minute
pause 3000
'pause 60000 ' one minute
'pause 60000 ' one minute
'pause 60000 ' one minute
'pause 60000 ' one minute
'pause 60000 ' one minute
' next b2
' goto initiate ' for testing
#rem
inc timePeriod
if timePeriod = 240 then ' approximately 24 hours
timePeriod = 0 ' reset
b0 = 0 ' reset all flags for day
goto newday
endif
#endrem
loop
TxData:
' MsgNo = MyMsgNo1
serout TxRS232,baudrate,(MyMsgNo1," ",#Data1," ",#Data2," ",#Data1,lf)
sertxd(MyMsgNo1," ",#Data1," ",#Data2," ",#Data1,lf)
return
checkTempChange:
bChangeFlag = 0
readtemp12 inTempPin,Temp12
' 0 and 85 are common erroneous values
if Temp12 = 0 then ' check again
pause 500
readtemp12 inTempPin,Temp12
if Temp12 = 0 then
pause 500
readtemp12 inTempPin,Temp12
endif
endif
bNegativeFlag = Temp12 & $f800 / 2048 ' 1 if negative
bLastNegativeFlag = lastTemp12 & $f800 / 2048 ' 1 if negative
if bNegativeFlag = bLastNegativeFlag then ' look for dif >0xf
if Temp12 > lastTemp12 then
TempDiff = Temp12 - lastTemp12
else
TempDiff = lastTemp12 - Temp12
endif
if TempDiff > 15 then
bchangeFlag = 1
'sertxd("1 ",#Temp12," ",#lastTemp12," ",#TempDiff," ",#b0)
endif
else ' change of sign: look for new or old > 0
lastTemp12 = lastTemp12 & 0x07ff / 16
if lastTemp12 > 0 then
bchangeFlag = 1
else
lastTemp12 = Temp12 & 0x07ff / 16
if lastTemp12 > 0 then
bchangeFlag = 1
endif
endif
'sertxd("2 ",#Temp12," ",#lastTemp12," ",#TempDiff," ",#b0)
endif
lastTemp12 = Temp12
return
This device would also be perfect for implementing the "openbridge" concept of steliosm, as shown here: http://www.picaxeforum.co.uk/showthread.php?15907