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
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:
Install FTDI driver opkg install kmod-usb-serial-ftdi
Created a script called boiler2text.sh in a folder /bin/ihiu based on simplified version of above.
Given the file permission to run chmod u+x boiler2text.sh
Executed the script from within Putty SSH window. sh /bin/ihiu/boiler2text.sh
So far, it works nice, but I've hit some problems:
I'm new to Linux, and any pointers would be very much appreciated.
/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/
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