Net access for picaxe with openWrt router

lbenson

Senior Member
This shows a proof-of-concept connection of a picaxe to the inter-/local net using an Asus WL-520gU router running the Linux openWrt operating system. The connection is made with a USB serial dongle plugged into the router. The router’s serial port could be used but it is not brought out of the box. Power (either 5 volt or 3.3 volt) could also be brought out of the router, but for this example 3-AAs are used to power the picaxe.

I chose the WL-520gU because it is an inexpensive router ($44US at Newegg, but with discounts and rebates often bringing it down to $30US), and it is easy to flash with openWrt, and hard to brick. The WL-520gU has a 240mHz MIPS processor, 4 megabytes of flash, 16 megabytes of ram, and a USB port. This router is definitely on the laggard end of the router selection nowadays, but it is very easy to use. I actually have brought both power and 3.3-volt serial out of the router, but this project doesn’t use them.

The hardware: WL-520gU router, USB-RS232 dongle, RadioShack solderboard (276-150), DB9 connector, PICAXE08M.




The USB-RS232 dongle is from ebay, here: http://cgi.ebay.com/USB-to-RS232-Serial-9-Pin-DB9-Adapter-PC-PDA-RS-232_W0QQitemZ280403235075QQcmdZViewItemQQptZLH_DefaultDomain_0?hash=item414955d103

The dongle was $2.38US including shipping, and is a PL2303 serial adaptor (same as my Rev-Ed USB-serial cable--and yes, it does work on my Vista laptop for programming a picaxe). OpenWrt supports this adaptor—many others work, but some may not.

I used the RadioShack board for the circuit because it’s easy to wire for the picaxe, and provides good breadboarding space. The schematic for the picaxe is attached below—it includes download connector and DB9 serial to pins 3 and 4. I soldered the DB9 female connector directly to the solderboard, and used the same resistor setup that the standard download circuit uses. The 20-pin socket on the board allows the use of an 08M, 14M, 18M2 (drool), 20M, or 20X2. The picaxe part is pretty straightforward. Aside from the serial circuitry on pins 3 and 4, no picaxe inputs or outputs were used except for the LED on pin 0. I look forward to playing with a 20x2.

I wrote a very simple program for the 08M which looks for input on pin 3 (a qualifier, “A”, and a 1-byte code), and then uses a select statement to execute code based on the value read. In this case I chose to let “A” mean “turn on LED”, and “B” to mean “turn off LED”. The command set could be about as elaborate as you liked. Data could be sent back to the router to build a web page, send an email, or communicate across the web.

Code:
'08SrIn3B.BAS reads serial in on pin3 & echos to sertxd
#picaxe 08M        ' leg 8: 0V; leg 7: O0, serout, infraout; leg 6: I1, O1, ADC1; leg 

pause 2000
start:
  sertxd ("Hello from 08M",13,10)
'  serout 4,N2400_4,("Hello from wl60",13,10)

main:
  do
    serin 3,N2400_4,("A"),b13
    sertxd(b13)
    serout 4,N2400_4,("Ack ",b13,LF) 'or " "
    select b13
      case "A" ' turn on led
        high 0
      case "B" ' turn off led
        low 0
    endselect
  loop
 
Last edited:

lbenson

Senior Member
The router is running openWrt (http://www.openwrt.org). I built my own image from trunk using the following packages: kmod-usb-core, kmod-usb-serial, kmod-serial-pl2303, picocom (terminal program), ssmtp (email program). One trick is that in the busybox configuration, “stty” must be enabled to allow setting of the serial port parameters. If you were to download a snapshot, this “stty” command would not be available, but a workaround is available if you install the “empty” package ("empty -f picocom -b 2400 -p n -f n -l -r /dev/ttyUSB0" sets the serial parameters in the absence of stty).

If everything is configured properly, when you plug in the USB dongle, openWrt will recognize it and create a name for it, ttyUSB0. This file can be accessed as /dev/ttyUSB0, and the picaxe can be written to with a Linux command line command as simple as “echo AA >/dev/ttyUSB0”. Writing a script to both read and write can be done, but it gets complicated, so I use a different language.

My favorite language for programming on the openWrt platform is Lua. It is a lightweight, fast, interpreted language which comes as a default in openWrt. Lua can be run interactively by typing “lua” and then entering commands, or by creating a file and typing, for instance, “lua test.lua”, or by compiling test.lua and running the compiled program. I’ve never compiled anything—interpreted speed has been fast enough. Here is a very simple Lua program which opens two different files—one for serial in and the other for serial out—writes a command and then reads back the response.

Code:
fileserin = io.open("/dev/ttyUSB0","r")
fileserout = io.open("/dev/ttyUSB0","w")
fileserout:write("AA") – turn on the LED
line = fileserin:read("*l") – read the response (a single line)
print(line) – this should print “ACK A”
The program could create a web page with a command as simple as

os.execute(&#8220;echo '<html><body><p>LED is on</p></body></html>' >/www/test.html&#8221;)

Then, for my ip, it could be accessed from a browser with &#8220;192.168.1.60/test.html&#8221;.

An email is only slightly more complicated (after the ssmtp configuration file is set up):
Code:
os.execute("echo \"From:  yourname@yourfromaddress.com\" > /home/user0/message")
os.execute("echo \"Subject:  wl60 email test\" >> /home/user0/message")
os.execute("echo \"LED on\" >> /home/user0/message")
os.execute("echo \".\" >> /home/user0/message")
os.execute("ssmtp lbenson@somewhere.com < /home/user0/message")
More extended and more elegant solutions of course exist for both emails and web pages. Lua is extensively documented, starting here: http://www.lua.org The answers to many questions which may come up are likely to be answered already and can be found with a search. If not found, responses are usually pretty good on various sites relating to Lua.

The case is different with openWrt. openWrt.org has a lot of information&#8212;unfortunately it is poorly organized and spread across an &#8220;oldwiki&#8221; and a &#8220;newwiki&#8221;, and the information on either may be dated. openWrt is a fast-moving target, and unfortunately the forum is nowhere near as helpful as the picaxe forum. I would estimate that around a quarter of the new questions never get a response&#8212;even if the questioner has done enough homework to ask the question concisely.

This particular setup&#8212;picaxe to openWrt router provides a lot of power and an opportunity to go to the net (were we talking basketball?) at a moderate cost.
 
Last edited:

lbenson

Senior Member
HOWTO: Set up Asus WL-520gU to allow serial connection between picaxe &amp; the internet

HOWTO: Set up Asus WL-520gU to allow serial connection between picaxe and the internet. This assumes a Windows PC--Linux users will probably know enough to be able to translate these instructions.

Hardware: Asus WL-520gU, a router with wifi, wan port, 4 lan ports, a usb port, 4 megabytes of flash memory, and 16 megabytes of ram. See here for openWrt-related information: http://wiki.openwrt.org/toh/asus/wl520gu Note that other devices will work as well, for instance, Asus wl-500gPv2, Buffalo wzr-hp-g300nh, TP-LINK TL-WR1043, Bifferboard, NSLU2 (but with different flashing instructions except for the wl-500gPv2, which will work with these).

Software: OpenWrt, a linux operation system which is modular and can installed on small routers and NASes; supports usb/serial connections to microprocessors such as the picaxe. It is possible to build your own version of openWrt, tailored to the needs of a small router, but one can also use prebuilt "snapshots" and then download the packages needed. For some reason the current snapshot flashes but doesn't boot on the wl-520, so I have made a custom flashable file, wl520.trx, and made it available for downloading.

Download the openWrt basic software:

In a DOS box, cd to your preferred destination folder (for instance, c:\dl\openwrt) and:

wget http://www.lyzby.com/wl520.txt -O wl520.trx

My hosting site only allows downloading of files with certain file types, so I renamed the flash binary as a ".txt" file, which can be retrieved with wget. The "-O" option renames the downloaded file as a "trx" file--the type used for flashing an openWrt image file.

Flash the router:

If you build openWrt on your own, you can set it to have any ip address you like. I have chosen 192.168.1.61. You can change this after the image is flashed. (If you flash a snapshot, it will have an ip address of 192.168.1.1--a very inconvenient address for many purposes, since this is often the default address of your network's gateway router.)

You can flash by plugging the WL-520 into your network. If you have problems, run a wire directly from your PC to a lan (not wan) port on the router. Be sure to set your PC's ip to something in the network range (e.g., 192.168.1.31).

With a paper clip or pen depress the recessed BLACK reset button, plug in power, count one-thousand-one, one-thousand-two, one-thousand-three, one-thousand-four, and release the button. The green power light should be flashing off and on. If it isn't, remove power and repeat, counting faster or slower until it works.

Use the windows tftp program in dos to flash the router (note that for tftp, the router will be addressed as 192.168.1.1).

tftp -i 192.168.1.1 PUT wl520.trx

On my xp machine this takes 5 seconds and nothing is output until the transfer is finished.

Wait 2 minutes while the flashing is completed and then plug the WL-520 into your network router and remove and re-insert the power connecter. Wait a minute or two for the router to reboot.

Modify configuration:

A newly flashed openWrt router can only be connected to using the telnet protocol. Once you connect with telnet and change the password, telnet will be disabled and access will be through the secure ssh method. Any number of programs will handle telnet and ssh access. I use putty, which may be downloaded here:

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Start putty and set up to connect using the telnet protocol to 192.168.1.61. You should get an openWrt greeting. Change the password by typing "passwd" and your new password twice. Make it good--include upper and lower case letters, numbers, and special characters.

While you are in via telnet you might as well modify a configuration file to give the wl-520 a name which suits you, and to set up your time zone. Only the clunky vi editor is available on a machine with 4 megabytes of flash (if there is more, I would install nano). OpenWrt system configuration files are located in the "/etc/config" directory.

Enter "vi /etc/config/system" to modify the system file. (Google "man vi" to get information about using vi--"i" goes into insert mode, where you can type and use Del and Backspace; <Esc>":wq" saves and quits; <Esc>":q!" quits without saving. Sorry--not my fault.) Change the system name from "OpenWrt" to something you prefer, like "wl61". Change the time zone as needed (samples for North America are "AST4ADT"--Atlantic Canada; "EST5EDT"--Eastern time; "CST6CDT"--Central time; "MST7MDT"--Mountain time; "PST8PDT"--Pacific time). Save and quit. Reboot and your putty session will be closed and telnet will be disabled.

To get back in, you re-enter putty with the same ip but this time using the SSH connection. Enter "root" as login ID and the password you set in telnet.

Further configuration--email:

I set up the files I want in "/home/user0". To create this directory, type "mkdir /home<Enter>" and "mkdir /home/user0<Enter>". Change to this directory with "cd /home/user0". Create a lua email program by typing "vi /home/user0/msg.lua". You'll get a blank file. Type "i" to go into insert mode. Copy the following and insert it by right-clicking.

os.execute("echo \"From: yourname@yourfromaddress.com\" > /home/user0/message")
os.execute("echo \"Subject: wl60 email test\" >> /home/user0/message")
os.execute("echo \"LED on\" >> /home/user0/message")
os.execute("echo \".\" >> /home/user0/message")
os.execute("ssmtp lbenson@somewhere.com < /home/user0/message")

Save and exit. (Note that the latest version of ssmtp does some reality checking, and apparently "yourfromaddress.com" above needs to be real.)

In /etc/ssmtp/ssmtp.conf, change the mailhub line to fit your smtp hub, for example, mailhub=smtp.eastlink.ca. Remove the "#" from the line which says "FromLineOverride=YES". You should then be able to do "lua /home/user0/msg.lua", and get an email sent. If the email isn't received, check /home/user0/message for rational values.

Further configuration--serial i/o:

Plug in a usb-to-serial dongle/cable--one which uses the pl2303 driver. Look at the end of the console log to see if it was recognized--type "dmesg | grep tail. You should see that the device /dev/ttyUSB0 was created. If you have that device then the lua program in post #2 will communicate with a picaxe wired up and programmed as in the first post.

Further configuration--web page:

The web server is not enabled by default. You can start it with "httpd -p 80 -h /www". This says that pages in the directory, "/www" will be served on port 80--the default. Create your first web page with something like this:

echo '<html><body><p>The doctor is in</p></body></html>' >/www/test.html

Go to your PC's browser and enter the address, "192.168.1.61/test.html". You should see your web page.

Access from PC:

In addition to access through ssh, the program "openssh-sftp-server" is activated. This means that you can access the device securely, with the same login ID and password, with an SCP program which allows you to view files on your PC in one pane and files on the device in the other, and to move them back and forth. WINSCP is a good windows program for doing this.
 

drone77

New Member
Very nice, it so happens that I am in the process of doing the same, albeit with a WRT54G.
Good to have a reference of someone doing it before. I'll try not to look too much though.

Did you think about adding some shift registers to give yourself more pinouts? One extra should come in useful to play you 'Happy Birthday' via crontab ;)
There's some info on doing so by Dr_Acula over here
 

julianE

Senior Member
Thanks for a great write up. Despite all your good work I still have issues.
the issue is this section,

" One trick is that in the busybox configuration, &#8220;stty&#8221; must be enabled to allow setting of the serial port parameters. If you were to download a snapshot, this &#8220;stty&#8221; command would not be available, but a workaround is available if you install the &#8220;empty&#8221; package ("empty -f picocom -b 2400 -p n -f n -l -r /dev/ttyUSB0" sets the serial parameters in the absence of stty)."

I can't install the "empty" package. I get errors like this,

root@OpenWrt:/# opkg install empty
Collected errors:
* opkg_install_cmd: Cannot install package empty.
root@OpenWrt:/#

BTW, I checked and the USB dongle does load correctly.

This method of interfacing to the web is very difficult but also very cheap, the router was less then $30.

Thanks for all the hard work.
 

lbenson

Senior Member
stty is now packaged as a separate entity: coreutils-stty_8.8-1_brcm47xx.ipk. Try "opkg install coreutils-stty".

I don't have anything set up on which I can currently try it, but the package, "empty_0.6.18b-1_brcm47xx.ipk", is still available. I don't know why it wouldn't install.

If you are building your own openWrt image, you can check the "stty" option under busybox.

This process is difficult, but provides a lot of flexibility.
 

julianE

Senior Member
"empty_0.6.18b-1_brcm47xx.ipk"
i did not have the full name of the "empty" file, googled it and found the file. downloaded and installed and now i can configure the port.

I'm still not getting anything to come out of the dongle. i wanted to verify the operation of the dongle but my WIN7 laptop refuses to install it cause the prolific site says it's not a genuine chip.

i'll keep working on it. thanks for any suggestions.
 

julianE

Senior Member
I did further testing by installing the USB to Serial dongle software on an XP machine, the dongle works well. I also attached an RS232 shifter board,
http://www.sparkfun.com/products/133 to the dongle and the yellow LED flickers when router transmits, so I am getting very close.
 

julianE

Senior Member
I am not having much luck with the serial communication from the usb-serial adapter. I set up a laptop with a null cable to attach to the output from the adapter. there is data when i send "echo AA >/dev/ttyUSB0", the trouble is I get gibberish and after trying numerous variations of baud rates and control setting i still can't get AA to come out. Of course, I did test my set up by connecting 2 PC's and the cables and hyperterminal are all good. The dongle has been tested with a pc connection and it too is good.

Not sure what to try next. Any ideas?
 

julianE

Senior Member
For some reason I came back to the project today and it started to work. I remembered when i plugged the dongle into the PC it installed with a default speed of 9600 baud. So, today i setup everything for 9600 and it began working. there was a very long buffer that emptied and then there was AA at the end. I also verified that it works without doing "empty -f picocom -b 2400 -p n -f n -l -r /dev/ttyUSB0",
i'm guessing it just assumes the default of 9600. trying to change the baud rate with the empty -f picocom was ineffective.

i tried installing the stty package and this was the result,

root@CoolWrt:~# opkg install coreutils-stty_8.8-1_brcm47xx.ipk
Installing coreutils-stty (8.8-1) to root...
Collected errors:
* satisfy_dependencies_for: Cannot satisfy the following dependencies for coreutils-stty:
* coreutils *
* opkg_install_cmd: Cannot install package coreutils-stty.
root@CoolWrt:~#

not sure what to make out of the error.

At this point the project is workable, 9600 baud is usable with the picaxe and a lot of modules use it as a default.
 

julianE

Senior Member
Last piece of the puzzle is in place. I went and installed picocom, i assumed, wrongly, that it was part of the build,

opkg install picocom_1.4-1_brcm47xx.ipk

once installed, I am able to configure the port and all is well, transmitting and receiving data.

thanks again for posting the information, hope others try the project it really is the least expensive solution.
 

MPep

Senior Member
Well done for perservering Julian. Even your 'diary' here is useful for others in the future.
 

hippy

Technical Support
Staff member
I was about to delete some of my 'diary', fearing it might be clogging up the board.
I wouldn't worry about that - It's often very interesting and useful to see what trials and tribulations others faced when trying to do it themselves.
 

lbenson

Senior Member
I'm also glad you got it working. I'm pressed for time right now, but was wanting to dig out an old router to try to find the problem. I'm mostly using Dockstars with debian now, but am packing for a move, and nothing is handy. Once you have the PICAXE talking to a Linux device, there are many things you can do. I'm looking forward to trying the Raspberry Pi (I think hippy is as well).
 

hippy

Technical Support
Staff member
Once you have the PICAXE talking to a Linux device, there are many things you can do. I'm looking forward to trying the Raspberry Pi (I think hippy is as well).
The Raspberry Pi for its low cost and small footprint is really just a small PC so hopefully easier to use than many embedded systems such as repurposed NAS and Routers and with a much larger community giving 'how to' advice.

Most net interfacing problems seem to revolve around (1) what's actually wanted; being very vague in definition such as 'how do I connect a PICAXE to a web site', (2) how to achieve it; often attempting to do it with various utilities and mismatched tools that are not really up to the task and (3) trying to do what's relatively easy on a desktop PC on something that is cheaper and more constrained; often with few people being knowledgeable about what is being used.

Hopefully the Raspberry Pi offers a chance to do it properly with people able and willing to show how to do it properly. I still think the biggest problem will be in defining what people want ( and then others wanting it to do something else or using something else ) and I'm sure we'll still hit the age-old Linux problem of you can do it if you know how to but getting to know how to is often a Catch-22 up-hill struggle on your own - even for skilled people who simply don't have Linux experience.

Community support is the make or break of it and it looks like there will be that support but even so I've already run into a show-stopping dead end on one aspect of things in having to know what to do to be able to do it, no one willing / able to provide the hand-holding necessary. Stalled at the first hurdle so to speak.
 

julianE

Senior Member
the age-old Linux problem of you can do it if you know how to but getting to know how to is often a Catch-22 up-hill struggle on your own - even for skilled people who simply don't have Linux experience.
That is so true about Linux. I reached out to a friend at work that's an expert on Unix. Has written copious amount of code and even has a giant penguin poster in his office. I asked him about the "empty" package and he had never heard of it. i would have never found it if it were not for lbenson guidance.

Community support is the make or break of it and it looks like there will be that support but even so I've already run into a show-stopping dead end on one aspect of things in having to know what to do to be able to do it, no one willing / able to provide the hand-holding necessary. Stalled at the first hurdle so to speak.
That's what makes this forum so outstanding, the support is superior. Hopefully, the raspberry product will be supported well but i do have my doubts, the introduction delay, tho almost normal in this business is worrisome. Their website is not very informative, it just does not look promising to me. The concept is brilliant, hopefully they will be able to pull it off.

My next step is to become familiar with Lua and continue working with the Asus router, i think my final cost for the router was $25.

I also must give kudos to lbenson for his River Cam project, amazing work. Thanks again for all the help and hope the move is uneventful.
 

lbenson

Senior Member
Not specifically picaxe-related, but here is a thread in which I detailed many things which can be done with an openWrt router acting as a little computing device: https://forum.openwrt.org/viewtopic.php?id=23114

This included cgi programming, which could, in theory, take a picaxe input and use it to alter a web page.

You might also be interested in this thread started by steliosm: http://www.picaxeforum.co.uk/showthread.php?15907-OpenBridge-development&highlight=openbridge

It covers his "openbridge" concept, in which a little linux device is used to do web, email, twitter, pachube, etc. from a picaxe.

Note that if you try to do too much, the memory limitations of the 520gU start to bite. I haven't exactly quantified what "too much" is. It is also possible that openWrt's memory handling has improved since I last worked with it.

You might also at some point wish to browse through the (long) list of packages available on openWrt for the brcm47xx platform on which the 520gU is based. The 520gU doesn't have enough resources to run all the packages.

http://downloads.openwrt.org/snapshots/trunk/brcm47xx/packages/
 
Last edited:
Top