Tuesday, 24 July 2012

Connecting an Arduino Uno to OpenWRT Part2

More OpenWRT progress and a few more issues resolved.

One of the issues I run into was the microSD cards becoming write protected. Even after removing the cards they were write protected on every other device I tried to connect then too. With full size SD cards there was a small switch to enable write protect but not with the microSD cards. I did read a few tricks like using a microSD to SD card adapter then disabling the write protect and formatting the cards but this did not work for me. Not wanting to run into this issue again I have just replaced the microSD cards with USB memory sticks.

Installing OpenWRT to a MR3020n.
Ok I have reinstalled openWRT a few times now and almost have it down to a fine art. This is not a full walk through but the key steps to get openWRT installed and working with a USB memory device. I did connect a TTL serial cable to the router so I did not have to play around with the networking side of things too much.
  1. Format the USB drive with the EXT2 file system, under Windows you can use Free EaseUS® Partition Master 9.1.1 Home Edition
  2. Flash the base image to the router. You can use the web interface to upload the firmware update of if you have a console and the router connected to the web you can use these two commands 
    wget http://downloads.openwrt.org/snapshots/trunk/ar71xx/openwrt-ar71xx-generic-tl-mr3020-v1-squashfs-factory.bin

    mtd -r write *mr3020*.bin firmware
  3. Now after the router reboots it will have the IP of
    before able to SSH to the router you will need to Telnet to it and using the passwd command set a password.
  4. Configure the network settings. You will need to edit /etc/config/network using vi to match the settings of your network. For me that is just enable DHCP so the settings will look this 

     config interface 'loopback'
         option ifname 'lo' 
         option proto 'static'
         option ipaddr '' 
         option netmask ''

     config interface 'lan'
         option ifname 'eth0'
         option type 'bridge'
         option proto 'dhcp'
  5. Now you need to reboot and reconnect using the new settings. If using the DHCP and you are not sure what IP the router has you can connect to your DHCP server for the lease details. I cheated and just used the serial console.
  6. Now we need to start installing support for the USB drive and filesystem. We want to install as little as possible until we have the USB driver mounted.

    opkg update

    This will download a list installable packages and needs to be done after every reboot.

    opkg install kmod-usb-storage block-mount kmod-fs-ext4

    This will download a list install the minimum kernel drivers and dependencies.

  7. Now to setup the overlay we need to copy the existing overlay files across. So we need to use the following commands

    mkdir /mnt/usb

    So we have somewhere to mount the drive

    mount -t ext2 /dev/sda1 /mnt/usb -o rw,sync

    To mount the drive

    tar -C /overlay -cvf - . | tar -C /mnt/usb -xf -

    To copy the data across

    Now to update fstab to use the sda1 as the overlay 
    vi /etc/config/fstab

    and should look like this
    config mount
            option target        /overlay
            option device        /dev/sda1
            option fstype        ext2
            option options       rw,sync
            option enabled       1
            option enabled_fsck  0

    Now reboot
  8. If all when well if you df -h you should see that you have gone from just a few KB of free space to a few GB of space. Now we can install anything we want and not have to worry about space. So for most people the first thing to do is install and setup the web interface. Just type the following commands

    opkg updateopkg install luci/etc/init.d/uhttpd enable/etc/init.d/uhttpd start
    Now you should be able to open the web interface of the router.
  9. To install a webcam (Logitech c270) use the following command

    opkg install kmod-video-core kmod-video-uvc mjpg-streamer
    This will install the uvc camera driver which is supported by many different webcams and it will install mjpg-streamer which will allow you to stream the webcam.
    To get mjpg-streamer start automatically you need to enable the service ether in the web interface or by running

    /etc/init.d/mjpg-streamer enable

    You may also want to edit


    I found that some resolutions like the default 640x480@5fps showed a lot of JPG corruption so I tried a few other resolutions and found 1280x720 worked nice but ended up settling on 720x350@10fps

    After editing the settings you will need to stop/start/restart the service for the new settings to take effect
    /etc/init.d/mjpg-streamer stop
    /etc/init.d/mjpg-streamer start

    You should be able to view the webcam via 

Thursday, 19 July 2012

Connecting an Arduino Uno to OpenWRT


Connection an Arduino Uno to TPlink mr3020n running OpenWRT.

After about a days of playing around with OpenWRT I have finally have it communicating with an Arduino Uno. 
The process is too complex to put into a single post so I will have to break the process up into small steps/posts. 
The process requires you to know your way around vi/vim as the base openWRT images do not have a webserver installed. I high recommend you use a usb-ttl serial adapter to connect to the serial console of the router during the process. 

There are also a few things to watch out for,

  1. The wireless and wireless nic has the same MAC address. Don't use both at the same time to connect to the same access point. Your DHCP server does not like this.
  2. After flashing the openWRT image the default IP address is with not gateway set and until there is a root password set there is no SSH. So you need to telnet into the router and set a password and using vi change the network settings in /etc/config/network      
  3. Only 4MB flash. The router is very tight on space. After flashing the openWRT you have only 1.2MB of space free and this disappears quickly. To get more space you need to add a something like a USB flash drive. But by the time you have installed the USB drivers, Filesystem drivers and web interface you are down to less the 150k. I recommend you format the drive as EXT2 and set it up as extroot as the first thing you do otherwise you will be out of space before you have even finished installing your basic drivers. Note that if you get down to less then 100k programs like vi will not let you save changes so try and keep as much flash free. After setting up the USB storage you can then go on installing things like Python and anything else until your heart is content (or until you run out of space on your USB drive)  
  4. Arduino Uno uses usb-cdc so install kmod-usb-acm. The Uno will be detected and mounted as /dev/ttyACM0
    The older Arduino boards with FTDI chips require kmod-usb-serial-ftdi and will be detected and mounted as /dev/ttyUSB0 
    I was having a lot of issues using /dev/ttyACM0 like a serial port so piping data to it does not appear to work too well but if you install socat you can bind a TCP port to it using the following command
    socat tcp-l:1234,reuseaddr,fork file:/dev/ttyAMC0,nonblock,raw,echo=0,waitlock=/var/run/tty,b9600
    After binding a TCP port you can use a loopback connect or a remote telnet connection to port 1234 to communicate to your Arduino.  

Friday, 6 July 2012

RFnodes v1.0 + Bluetooth + Solarbattery

RFnode with Bluetooth and solar battery attached. 
Just a quick post to show a Bluetooth module connected and powered by a solar battery.
What you can't see is that it is connected to my tablet via Bluetooth (only a serial console over Bluetooth)

RFnode 1.2b PCB update

RFnode PCB version 1.2b almost ready for printing and have made many changes with this version (and a few more to come).

Major changes so far:

  • Added SDcard slot for storage on the under side. This will allow the RFnode to be used as a data logger. The problem with SDcard slot is that takes up a large chunk of PCB space and the SDcard libraries take up a lot of programming space so if using a Atmega168 you need to keep a close eye on your program size.
  • Replace Red&Green LEDs with a RGB LED. This opens up the full spectrum of colors. This also means I loose 3 analog I/Os but gain 2 digital I/Os. This leaves me with -3 Analog I/Os (includes 2 for i2c bus)
    -6 Digital I/Os (Includes 2 for serial and 1 for SDcard if used)
  • Breadboard friendly pin header. Now all of the I/Os are on a single 11pin breadboard friendly header. The boards were going to be just for another project I am working but have decided to open it up a little. So that means making the board more general purpose and Arduino hacker friendly.  
  • Better power consumption. I ended up caving in and adding a switch to select power source. It seemed to be the most simplest and cost effective solution to the power consumption issue I was having with the power regulator.
  • Optional pull up resistor pads added for D6 & D8. Makes it easy to add 1wire digital sensors like the DHT11 & DHT22 temperature and humidity sensors.    

Future Changes:
The board still has room to grow so there are some more changes to come.

  • Size. The board has grown a little in this version while I sort out and move features around but is still only approximately 45mm X 35mm. Most of that space is due to connectors and a few parts.
  • Lithium ion support. Looking to add a charge and protection circuitry for Lithium ion battery packs. Looks like a MCP73831T could handle the charging of the battery but now need to source the cut off protection side of things to protect the battery voltage from dropping too low. The problem with this I am running into is cost. It is cheaper to but a phone battery that has this built in then it is to buy the circuit by it's self. Currently I do lithium ion battery protection via software were the software will check the battery voltage then if it is too low will go to sleep (but still consumes a little less then 0.1mAh) 
  • More I/Os. The Radio uses 6 digital I/Os and the RGB LED uses 3 analog I/Os. Then if Using SDcard, serial and i2c bus you only get left with about 4 I/Os so I want to add more I/Os. I know there are chips that allow using the i2c bus to give you an extra few I/Os but I am thinking about something a little more powerful then one of these guys. I am thinking about adding a whole second processor. If I add a second Atmega168/328 I could off load all of the sensor work to the second processor leaving the first processor to handle the communications (USB, Radio, SDcard, RGB LED) then the second processor to handle everything else with almost all of his I/Os (13 digital and 4 Analog).
  • More memory. One of the problems I ran into with creating a meshing protocol with the RFnode beside battery life was lack of memory, routing tables take a lot of space. I did come up with a few memory tricks like creating a u_int8[256] array using the index as the remote RFnode ID then the value being where to address the packet so the next node can follow the same process and pass it on. This allowed to have a routing table of less then 256 nodes in 256bytes of memory. With a Atmega168 this is still about 25% of ram of 50% of EEPROM space and does not leave much space for routing data like number of hops and route health but still possible.
    So to fix the memory issue I am thinking about adding a i2c based ram/flash chip. This will also allow data logging without using the SDcard. 

RFnodes v1.1

RFnode PCB v1.1a
This version of the PCB I move some of the pins around to makes the unit more compact. As the boards normally take 3-4 weeks from time of ordering to arriving I ordered these boards before I had received and tested the Version 1.0 PCBs so had many of the same issues  

Assembled board with DHT11 temperature/humidity sensor attached

AMS1117 power consumption work around.
As a workaround to the AMS1117 power consumption issue I tried adding a diode that helped but only helped by dropped by about 70% and introduced a new issue of voltage drop from the voltage regulator dropping the voltage from 3.3 volts to 2.6 volts. USB still works and the board operates fine but not a long term fix.

I am trying to keep the board as user simple as possible so did not want to add switches. I also want to keep the board cost low so didn't want start adding item that would raise the cost too much (p-channel mosfets

RFnodes v1.0

RFnode v1.0
Time for a second post. This time with pictures.

These little guys are the Version 1.0 RFnodes I was working on, These were the first version I designed so expected a few issues. It is all part of my self inflicted crash coarse into PCB design and SMD (surface mount). This board is a few weeks old now and I have moved on to better designs since. 

The good
  • They are Arduino based (Atmega328/168) 
  • V-USB bootloader (same as USnooBie) http://www.frank-zhao.com/usnoobie/index.php
  • Runs at 12Mhz @ 3.3volts with onboard voltage regulator (AMS1117 3.3)
  • USB or 3 volt coin cell powered (CR2032) 
  • The PCB is designed by me from the ground up (printed in china)
  • Compatible with the USnooBie (as I own a few of them too and over time have found them easier to work with then many Arduinos)
  • Supports V-USB http://www.obdev.at/products/vusb/index.html
  • Uses nRF24L01+ 2.4Ghz radio 
  • Mostly SMD parts (and all soldered by hand) 
  • Some parts not SMD so I could change the sides if I wanted
  • 4 digital IOs (2 used for serial)
  • 6 Analog IOs (2 used for I2C)
  • 2 LEDS (Red/Green) 
  • ISP Header (only used for loading bootloader or adding another SPI device)
  • Can consume <0.1mAh in sleepmode and about 0.5mAh idle (read below)
 The Bad
  • The Radio overhang. This was done on purpose for this version of the board so the antenna is free from interference from the main board. After a bit of testing I decided I did not need to position the radio in such a way. 
  • SMD pad sizes. As I am a little new to SMD parts and their required pad sizes on the PCB some of my pads where too small for the part. Well it was only one part, The main power capacitor which I ended up just soldering to the voltage regulator pins.
  • Silk screen. Not all of the silk screen was printed or was too small or mostly off the edge of the board after cutting.   
  • Power consumption. Board was consuming about 17.5mAh so a CR2032 would only last about 6 hours which is less then the maths that said I should be able to get up to about 5 months. Issue turned out to being the AMS1117 power regulator would still consume about 4mAh if power was connected to it's output. I thought it was only meant to consume power if connected to input only. I also found that the radio was consuming about 13.5mAh when idle in RX mode. So after disconnecting the voltage regulator and powering down the radio when not in use I can the power consumption to less then 0.3mAh and less then 0.1mAh when in sleep mode.     
My finger compared to it's size, all soldered by hand (mostly my off hand)

Thursday, 5 July 2012

Time for a new site?

Time for a new site so that means time to start looking for somewhere to host it.
One of the problems I have with my current site is that is so single sided (I post, you read). This might suit some people fine but I like feedback and sometime find comments can contain a wealth of information (and amusement).

So the purpose of this post is just to test out Blogger.com and if it's feature set suits my needs. If it does then many more posts may follow this one. 

sooooo to get started, Testing123
is still online and may start moving content across slowly.