0

I am using a OrangePI board to log data from an arduino which gives a line by serial output every 10 secs.

20.0 / 12.0 V

Which is loggig the Temperature measured by a temperature sensor attached to the arduino and the voltage which is for battery saving purposes.

I have managed to build a script from snippets and suggestions collected around the web which does exactly what I need.

#!/bin/bash
# Script will run at reboot.

echo "Beginning Temperature Log!"
NOW=$(date +"%Y-%m-%d")
LOGFILE="log-$NOW"

name=$LOGFILE
if [[ -e $name ]] ; then
    i=2
    while [[ -e $name-$i ]] ; do
        let i++
    done
    name=$name-$i
fi

ts </dev/ttyUSB0>$name

The script runs everytime I boot the machine by setting a cron @reboot, which works pretty well.

Now I plan to extend the arduino code with a "battery guard" which sends a warning to the serial output when the voltage goes below certain level

20.0 / 9.0 V / BAT!

At this point the OrangePI should halt/shutdown and send a specific letter to the arduino when its "safe to shutdown", so that the arduino can deactivate a relay and switch off the whole system. (and itself)

So I assume the OrangePI should always watch the last line in the arduino output for the string "BAT!" and then trigger the shutdown.

I have a concept for the harware part of this project Also a concept for the arduino code

But no idea for how to code this on Linux. Maybe the approach is wrong as it is right now and the whole thing should be coded in python or something?

Thanks for any suggestions.

2
  • This doesn't look like Python to me.. It's in fact a Bash script Commented Jan 12, 2017 at 12:30
  • Yea its not python, the question is, is all this manageable in bash or should I rewrite all this in python. Or C if possible, as I am more familiar with C (because arduino) Commented Jan 12, 2017 at 12:35

1 Answer 1

1

Python would be a really simple and neat way to go. Using pyserial, you could have it read the strings you send across over the serial connection and scan for the word BAT, or whatever it is you want really. So, off the top of my head, you could do something like:

import serial
from sys import argv

port = argv[1]  # just to assign port and baud from command line
baud = argv[2]

COM = serial.Serial(port, baud)  # create serial instance
data_in = COM.readline().strip("\r\n")  #  strip ending
### then some manipulation of data_in ### for example:
if(something you want is in the transmission string):
     # do something
else:
    float_list = [float(x) for x in data_in]
    #  convert all the vals to a floats for logging etc.

Hope this example is helpful and you can do something with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.