0

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.

Community
  • 1
  • 1
heatweb
  • 3
  • 2

1 Answers1

0
  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
    
forkrul
  • 524
  • 1
  • 4
  • 9
  • Thank you kindly for your help. I did get it working by... I had to use following to get serial port at correct speed mgetty -s 19200 /dev/ttyS0 and then ran existing script, both issued as system commands from a PHP page 30 seconds after system reboots and comes online. Although it appears to hang system for 30 seconds, everything starts ticking along, and process carries running (been going straight for over 24 hours now). – heatweb Jul 03 '15 at 11:04
  • I've got the Php to rename the data file every day so file size doesn't get too excessive (and risk sd card write failure), but now I can play around a bit more. Thinking of using php to edit the bash script prior to executing it, so I can have it set file name and have it run a set number of lines as you have shown. Next task is working out how to send a command back out the same port timing it between incoming messages. Current thinking is to use php to kill the script, rewrite it including a send command to immediately follow receive of data, and then reactivate. Cheers. – heatweb Jul 03 '15 at 11:05
  • I realised why I was getting the syntax error. It was because I was editing the file in notepad and it was inserting end of line characters. – heatweb Jul 03 '15 at 20:17