Search code examples
linuxbashshellserial-portarduino-yun

Linux openWRT shell on Arduino Yun: save input line from Serial Port to file


I'm trying to save the input from the USB port on an Arduino Yun (running openWRT) to a file so it can be ready from a PHP page hosted on the Yun and accessed via a browser.

The idea is the Yun acts as a web interface to the boiler, via the RS485, logging all data, and generating graphs of various temperatures fed from the boiler and saved to file.

The data comes from an RS485 feed (from a boiler), via an FTDI cable.

The data is fed through every second in the form...

DAT,39665,8,0.00,-273.15,-273.15,-273.15,0,0.00,0.60,0.00,0.00,-6.25,0.00,0.00,60.00,60.00,225,-273.15,0.00
STAT,39666,0.00,0,19,924,2,0,0,0,0,2,2,2,0,0

I have had some luck using the following tip... Linux shell: save input line from Serial Port each minute and send to remote server

and... Bash script: save stream from Serial Port (/dev/ttyUSB0) to file until a specific input (e.g. eof) appears

This last article provides the code:

#!/bin/bash
while read line; do
     if [ "$line" != "EOF" ]; then
          echo "$line" >> file.txt
     else
          break
     fi
done < /dev/ttyUSB0

However it raises an error line 8: syntax error: unexpected "done" (expecting then)

but I can't figure why, so have simplified to:

#!/bin/bash
while read line; do
    echo "$line" >> hsffile.txt
done < /dev/ttyUSB0

Actions so far:

  1. Install FTDI driver opkg install kmod-usb-serial-ftdi

  2. Created a script called boiler2text.sh in a folder /bin/ihiu based on simplified version of above.

  3. Given the file permission to run chmod u+x boiler2text.sh

  4. Executed the script from within Putty SSH window. sh /bin/ihiu/boiler2text.sh

So far, it works nice, but I've hit some problems:

  1. I need to have the script run 24/7 without having to SSH into the system. I can execute a system command from within a PHP page, but it just hangs when it runs the script, and nothing appears to be output to file.
  2. As the file starts getting quite large, I need to ideally delete a line from the start of the file each time one is added - once the file reaches a maximum file size. Or have it save to a file name based on the date and hour of the day, so filename changes every hour.

I'm new to Linux, and any pointers would be very much appreciated.


Solution

    1. In order to avoid having to stay SSH'd into the system, when you start up the command, run /bin/ihiu/boiler2.txt &. The ampersand will start the process in the background; you will have to manually kill the process at some point using the kill or killall commands (or you can just leave it running forever). If you don't want to be starting the script up every time you reboot, you can create an entry in /etc/init.d/
    2. In order to, say, prevent your file from exceeding 1000 lines, you could do:

      #!/bin/bash
      SAVE_FILE=hsffile.txt
      while read line; do
          echo "$line" >> $SAVE_FILE
          # Determine the number of lines currently in the file by
          # doing a word count and then getting the first piece
          # of output.
          lines=`wc -l $SAVE_FILE | awk '{print $1;}'`
      
          # Check to see if the line count is greater than 1000.
          if [[ $lines -gt 1000 ]]; then
              # Delete the first line of the file using sed.
              sed -i '1d' $SAVE_FILE
          fi
      done < /dev/ttyUSB0