Final bash script to generate CPM and uSv/h mean values and send data to Radmom

More
4 months 2 weeks ago - 4 months 2 weeks ago #4377 by pilovis
This is the final script (fully commented) to generate once per minute the CPM and uSv/h mean values from geiger data received over a serial port,
to send CPM data to you Radmon account,
and to generate an alarm (launch a script or a command) when the alarm level is overcomed.


#!/bin/bash
####################################################################################
##serial port setup
#stty -F /dev/ttyUSB0 speed 9600  cs8 -parenb -cstopb
## continuously print values in a file
#(stty raw; cat > /tmp/received.txt) < /dev/ttyUSB0
## continuously print values in console
#echo | (stty raw; cat) < /dev/ttyUSB0
## print one value in console
#head -n1 /dev/ttyUSB0
###################################################################################à
## by pilovis 2109 Italy - parknat12@yahoo.com
####################################################################################
# main loop (infinite)
while true
do
# CPM data reading loop (60 seconds)
while true
do
# populate a tmp file with CPM data received from USB serial connection, one reading per line
cat /dev/ttyUSB0 > /tmp/received.txt 
# change the above /dev/ttyUSB0 according to your serial port device
#
# send reading loop to background and go further
done &
# count 60 seconds and then stop the CPM reading loop and the serial data reading / tmp file writing
pid=$!
sleep 60 && kill -TERM $pid; killall cat
sleep 1
# calculate the mean value of the readings and insert the integer value rounded to the CPM variable
cpm=$(awk '{ total += $1; count++ } END { print total/count }' /tmp/received.txt | awk '{printf("%.f\n",$0)}')
### OPTIONAL PART 1: you may want to use  CPM and uSv/h values, so let's create two tmp files
# insert CPM value in a tmp file, updated every minute
echo $cpm > /tmp/cpm.txt
# calculate uSv/h value from CPM and put it in a tmp file, updated every minute
sed 's/$/ * .0057/' /tmp/cpm.txt | bc | awk '{printf("%.2f\n", $1)}' > /tmp/usvh.txt
# note: .0057 is the conversion factor (CPM to uSv/h) for the SBM-20 Geiger tube, change it according to your tube conversion factor
### OPTIONAL PART 1 END
# send CPM data to radmon.org
curl -s "http://radmon.org/radmon.php?function=submit&user=myuser&password=mypass&value=$cpm&unit=CPM"
# change myuser & mypass in the above line with your radmon login data
sleep 7
### OPTIONAL PART 2: notify alarm if CPM goes above the alarm level
# set the alarm level
alarm=50
#
if [ $cpm -gt $alarm ];
then
  echo "Alarm CPM = "$cpm > /dev/null 2>&1;
  # put your alarm on script here
  echo 1 > /sys/class/gpio/gpio1/value # switch on GPIO1 
else
   echo "everything is normal" > /dev/null 2>&1;
   # put your alarm off script here
   echo 0 > /sys/class/gpio/gpio1/value  # switch off GPIO1
fi;
### OPTIONAL PART 2 END
# wait 10 seconds and then restart main loop
sleep 10
done
Last edit: 4 months 2 weeks ago by pilovis.
The following user(s) said Thank You: mw0uzo

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago - 4 months 2 weeks ago #4378 by pilovis
Important note:

if you want to use Raspberry or any other *nix device that doesn't have a real Hard Disk, but just a SD card / USB memory stick, I would suggest you to mount the /tmp directory in RAM, to avoid the wear leveling (reduced lifespan) of the storage device.

To do so, just put the following line in the "/etc/fstab" file:
tmpfs    /tmp       tmpfs    defaults    0 0

If you plan to use OpenWRT/LEDE, you don't need to do this, since OpenWRT has the /tmp directory already mounted on RAM by default.
Last edit: 4 months 2 weeks ago by pilovis.
The following user(s) said Thank You: mw0uzo

Please Log in or Create an account to join the conversation.

Time to create page: 0.049 seconds
Powered by Kunena Forum