Search code examples
shellcurlserial-portarduinoopenwrt

piping Linux cat command to web in openWRT


I want to run a shell script from openWRT. Basically its need to constantly read arduino serial port and when its reads something its need to be sent to a web based service.

Currently this is my script which only save to text file:

cat /dev/ttyACM0  >> /www/home/log.txt & 

I want to avoid saving to file and send the output string right to a web based service that store the readings in mySQL DB.

All the data saving web service is all set and working something like this:

http://my-service.com/?data=what-ever-the-arduino-spits

Is there a way to do it with wget?

maybe something like this:

cat /dev/ttyACM0 | xargs -n % wget http://ivardi.info?todb=%

keep in mind that the openWRT is on a 32 RAM and 4MB flash storage so this is only possible with shell script and not Phyton/PHP.

Regards


Solution

  • Note that it could be dangerous in some cases to directly read the serial (/dev/ttyACM0) device and pass it direct to wget in case the read blocked for some reason (what happens if the serial port is disconnected and reconnected?)

    It could be safer to route the output to a file; then in a loop read the most recent data and 'pushing' that using wget. Perhaps something like:

    #!/bin/bash
    
    while true; do
        tail -1 /www/home/log/txt | wget <...options...>
        sleep 60
    done
    

    In reality you would probably need to do something a little more advanced so that you don't keep sending duplicate data.

    Of course, in your own situation what you proposed may be sufficient...