Showing posts with label OpenWRT. Show all posts
Showing posts with label OpenWRT. Show all posts

Saturday, 22 September 2012

3D Printer setup

I have upgraded my 3D printer setup.

Now instead of him sitting on a cardboard box he has his own rack and I can have the spools of plastic hanging above him (need to get more plastics to fill the space) 


I have also added one of the routers with openWRT, some LEDs for lighting and a Logitech c270 HD webcam so I can remotely keep an eye on him.  

I am in the process of upgrading his printhead to support better cooling for the extruder and printed part also will have a few built in lights and maybe a small camera.

Once I get my Makibox printer I might have to expand the setup even more.




Tuesday, 21 August 2012

OpenWRT Mjpg_streamer saving to file


By default when you use the file output plugin with mjpg_streamer you can not set the file name just the folder. So what will happen is that folder will quickly fill with time stamped for name picture files. This could be what you want but this is not what I wanted. I wanted a single file in the web server root so I could include it in the web interface.

There are 2 main steps to achieve this. 

  1. Create a shell script that move the snapshot to the web server root(/www/video0.jpg)  
  2. Change the mjpg-streamer service to output snapshot to file and execute shell script to move snapshot


Step 1. The shell script.
This just moves the snapshot to "/www/video0.jpg". The -f is to force overwrite. I called the script "mjpg_save" and saved it to "/usr/bin/


#!/bin/sh
mv -f "$1" '/www/video0.jpg'

don't forget to chmod +x the script



Step 2. Update the service.
You need to edit "/etc/init.d/mjpg-streameradding "\ --output "output_file.so -c /usr/bin/mjpg_save -d 1500" as per below. 
The "-d 1500" is the delay between saving in ms so 1500 = 1.5 seconds. If you have this set too low mjpg-streamer will not get a chance to finish saving this before starting to save the next picture over it leading to corrupt images.  


root@OpenWrt:/etc/init.d# vi mjpg-streamer

#!/bin/sh /etc/rc.common

# Copyright (C) 2009-2011 OpenWrt.org

START=50

SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1

PROG=/usr/bin/mjpg_streamer

error() {
        echo "${initscript}:" "$@" 1>&2
}

section_enabled() {
        config_get_bool enabled "$1" 'enabled' 0
        [ $enabled -gt 0 ]
}

start_instance() {
        local s="$1"

        section_enabled "$s" || return 1

        config_get device "$s" 'device'
        config_get resolution "$s" 'resolution'
        config_get fps "$s" 'fps'
        config_get port "$s" 'port'

        [ -c "$device" ] || {
                error "device '$device' does not exist"
                return 1
        }

        service_start /usr/bin/mjpg_streamer --input "input_uvc.so \
                --device $device --fps $fps --resolution $resolution" \
                --output "output_http.so --port $port" \
                --output "output_file.so -c /usr/bin/mjpg_save -d 1500"
}

stop_instance() {
        local s="$1"

        section_enabled "$s" || return 1

        service_stop /usr/bin/mjpg_streamer
}

start() {
        config_load 'mjpg-streamer'
        config_foreach start_instance 'mjpg-streamer'
}

stop() {
        config_load 'mjpg-streamer'
        config_foreach stop_instance 'mjpg-streamer'
}


If all when well you should be able to access the screenshot from a remote computer using http://routerip/video0.jpg


Monday, 20 August 2012

OpenWRT mjpg streamer with more then one camera


The Documentation for mjpg streamer in OpenWRT is very light for information and finding information for the /etc/config/mjpg-streamer file is almost impossible.

So I decided to add some of my notes.
One of the great things I have found with mjpg_streamer is that it can support many cameras I have tested 3 Logitech C270 and will be testing more when I get a bigger USB hub.
To setup mjpg_streamer for the second camera you need to change the device from "video0" to "video1" then for the third camera same again change "video1" to "video2"

Running multiple cameras is not supported by the default service/config so you ether need to edit /etc/init.d/mjpg-streamer to support the extra settings or run mjpg_streamer from the command line


Below is the command line I am using.
mjpg_streamer --input "input_uvc.so --device /dev/video0 --fps 3 --resolution 720x350 --output "output_http.so --port 8080" 


mjpg_streamer --input "input_uvc.so --device /dev/video1 --fps 3 --resolution 720x350 --output "output_http.so --port 8081" 

mjpg_streamer --input "input_uvc.so --device /dev/video2 --fps 3 --resolution 720x350 --output "output_http.so --port 8082" 

I have found that if you put too much load on the processor you get a lot of garbled frames. So if you are using 1280x720 I recommend you don't have a frame rate of 15fps but something low like 1-5fps. You can even mix them up so on one of the robots I am building he has one camera with a low resolution high frame rate and a second camera with a high resolution low frame rate.

  

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 192.168.1.1
    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 '127.0.0.1' 
         option netmask '255.0.0.0'

     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

    /etc/config/mjpg-streamer

    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 
    http://%routerIP%:8080/?action=stream
    http://%routerIP%:8080/?action=snapshot

Thursday, 19 July 2012

Connecting an Arduino Uno to OpenWRT


IMAG0375.jpg

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 192.168.1.1 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.