Connect geiger counter FTDI and send data to radmon.org

sezione dedicata ai sistemi operativi alternativi Open Source

Connect geiger counter FTDI and send data to radmon.org

Messaggio da leggereda root » 02/02/2019, 9:36

this is a quick How-To about how to connect a geiger counter with serial FTDI USB connection to an OpenWRT router, read CPM values and send data to radmon.org.

As usual all code is in BASH, all commented for better understandig :)

Geiger counter I used:

Immagine

https://rhelectronics.net/store/mygeiger-3-pro-diy-dosimeter-ratemeter-radiometer-kit-with-lcd-usb.html

OpenWRT prerequisites:
Codice: Seleziona tutto
opkg update
opkg install kmod-usb-serial-ftdi
opkg install minicom coreutils-stty


reboot the router

test the serial connection:
Codice: Seleziona tutto
stty -F /dev/ttyUSB0 speed 9600  cs8 -parenb -cstopb
echo | (stty raw; cat) < /dev/ttyUSB0


you should see something like this:
Codice: Seleziona tutto
root@OpenWrt:~# echo | (stty raw; cat) < /dev/ttyUSB0
22
20
20
18
19
19
19


this is the script to read CPM values for one minute, calculate mean value and send data to radmon.org:

read-ftdi.sh
Codice: Seleziona tutto
#!/bin/bash
## by pilovis 2109 Italy - www.49v.com
# main loop (infinite)
while true
do
# CPM reading loop (60 seconds)
while true
do
  # populate a tmp file with CPM data received from USB serial connection, one read per line
  cat /dev/ttyUSB0 > /tmp/received.txt
# send reading loop to background and go further
done &
# count 60 seconds and then stop the CPM reading loop and the serial data reading / file writing
pid=$!
/bin/sleep 60 && kill -TERM $pid; killall cat
/bin/sleep 1
# calculate the mean value of the readings and insert the integer value rounded to the CPM variable
cpm=$(/usr/bin/awk '{ total += $1; count++ } END { print total/count }' /tmp/received.txt | awk '{printf("%.f\n",$0)}')
# OPTIONAL: insert CPM value in a tmp file for other uses (it changes every minute)
/bin/echo $cpm > /tmp/cpm.txt
# send CPM data to radmon.org
/usr/bin/wget -O /dev/null "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
/bin/sleep 5
# OPTIONAL: notify alarm if CPM > 50
alarm=50
if [ $cpm -gt $alarm ];
then
   /bin/echo "Alarm CPM = "$cpm > /dev/null 2>&1;
  # put your alarm on script here
  # example:
  #/bin/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
  # example:
  #/bin/echo 0 > /sys/class/gpio/gpio1/value  # switch off GPIO1
fi;
# wait 10 seconds and then restart main loop
/bin/sleep 10
done

notes:
measure time = 1 minute
change myuser & mypass in the above script with your radmon login data
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Connect geiger counter FTDI and send data to radmon.org

Messaggio da leggereda root » 02/02/2019, 10:25

if you want to see what the script is writing into the /tmp/received file (CPM data), open another shell console and give the following command:
Codice: Seleziona tutto
tail -f /tmp/received.txt


and also:
Codice: Seleziona tutto
tail -f /tmp/cpm.txt
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Connect geiger counter FTDI and send data to radmon.org

Messaggio da leggereda root » 02/02/2019, 13:55

Please note this script should also work on different linux devices, like Raspberry (not tested).
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: Connect geiger counter FTDI and send data to radmon.org

Messaggio da leggereda root » 02/02/2019, 14:45

In case we want to read the current CPM value stored in the /tmp/cpm.txt file (see above) and import the value in a python scrip, this is the python code:

Codice: Seleziona tutto
in_file = open("/tmp/cpm.txt", "rt") # open file cpm.txt for reading text data
contents = in_file.read()            # read the entire file into a string variable
in_file.close()                      # close the file
print(contents)
radiation = ("CPM: " + contents.rstrip("\n"))
print(radiation)
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin


Torna a OpenWRT

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron