Connect geiger with USB FTDI to an OpenWRT router and send data to radmon.org

More
5 years 8 months ago - 5 years 8 months ago #4331 by pilovis
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:


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

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

reboot the router

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

you should see something like this:
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
#!/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
Last edit: 5 years 8 months ago by pilovis.
The following user(s) said Thank You: mw0uzo

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

More
5 years 8 months ago - 5 years 8 months ago #4332 by pilovis
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:
tail -f /tmp/received.txt

and also:
tail -f /tmp/cpm.txt
Last edit: 5 years 8 months ago by pilovis.

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

More
5 years 8 months ago #4335 by pilovis
Please note this script should also work on different linux devices, like Raspberry (not tested).

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

More
5 years 8 months ago - 5 years 8 months ago #4336 by pilovis
In case you 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 you need:
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)
Last edit: 5 years 8 months ago by pilovis.

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

More
5 years 8 months ago #4339 by Bert490
Thanks, Pivolis
This is a good option for anyone that wants to submit data without needing a running PC or a Raspberry Pi. For those who don't recognize OpenWRT, it is an open-source router project that allows routers to be upgraded to modern capabilities. By over-writing the router's firmware, it allows the router to do things in addition to its original basic function (interfacing between a private LAN and your ISP). If the router processor chip is reasonably modern, it can run other tasks, such as the collection and submission of Geiger data. If your router is no longer being supported (patched) by the vendor or your ISP, consider this option. I have an old WiFi router that was known to be a security risk, and was sitting unused. It had OpenWRT compatibility so I upgraded, and it now works fine as a WiFi extender. The security is better than before, including being resistant to the latest WiFi hacking methods. I intend to also upgrade my main router. There are thousands of add-on applications, and the application written by Pivolis is an ideal add-on. The router is always on, and they are typically very reliable. If you do try this, make sure your router is compatible and that your users can tolerate the outage while you set it up. It may be wise to test on a second router before changing your main router. Many people have their router managed by the ISP, and if you are OK with that, leave well enough alone.
The following user(s) said Thank You: pilovis

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

More
5 years 6 months ago - 5 years 6 months ago #4376 by pilovis
to get uSv/h value from CPM file (last reading data):
sed 's/$/ * .0057/' /tmp/cpm.txt | bc | awk '{printf("%.2f\n", $1)}'
note: 0.0057 is the conversion factor (CPM to uSv/h) for the SBM-20 Geiger tube I'm using, uSv/h value is rounded to the second decimal place.

so, to generate uSv/h value for LCD display printing:
/bin/sed 's/$/ * .0057/' /tmp/cpm.txt | /usr/bin/bc | /usr/bin/awk '{printf("%.2f\n", $1)}' > /tmp/usvh.txt
Last edit: 5 years 6 months ago by pilovis.

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

Moderators: Gamma-Man
Time to create page: 0.196 seconds
Powered by Kunena Forum
Everything's free. Please support us by considering a donation. Log in first!
Solar powered Raspberry Pi 4 server stats: CPU 33% Memory 14% Swap 13% CPU temp=55.5'C Uptime 29 Days