Anonymous Anonymous - 2 years ago 282
Bash Question

Read Arduino data with shell script fast

My Arduino is recording temperature data (datatype float), which I'm trying to log on my Linux machine. The big problem is the speed of this logging. The Arduino uses a DS1822 to read the temperature, after which I use the


command, where temp is the float holding the temperature data. On the Linux machine, I use the following shell script, that collects the data:

while [ 1 ]; do
val=$(cat /dev/ttyUSB0);
echo "$val,$(date "+%Y-%m-%d %H:%M:%S")" >> ~test.txt;
sleep 10;

The main problem with this is that the sleep (delay) has to be quite large; if I use
sleep 1;
, the computer can not receive any data.
When I use
screen /dev/ttyUSB0
, I can see that the data is received quite fast.
With my current script, it seems that the Arduino somehow freezes while sending the data. I also tried to build a simpler script (as the DS1822 data conversion also takes some time) with just a resistance measurement, but this is also taking a lot of time when I use the shell script as described above.

My question is therefore: how can I optimize my Arduino / shell script in order to log the data just as fast as the Arduino can send it?

Answer Source

I found the solution by disabling the autoreset that the Arduino is performing when a new serial connection is established. To do so, I placed a 10 uF capacitor between RST and GND. Please note that this capacitor has to be removed before uploading code to your Arduino!

After making this modification, I was able to read resistance data at 10 Hz from my Arduino. To do so, the only thing I modified was the sleep command on the bash script, which is now

sleep 0.1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download