Allstar and the GPIO Fan Script for DMK Engineering URI

Fan with 1 channel relay for Arduino

12V Fan with 1 channel relay for Arduino

(Integrated into latest Raspberry Pi2 and BeagleBone Black Allstar image!)

By: Ramon Gonzalez (KP4TR)

This article is a project I decided to embark to understand how the GPIO pin 1,2,3,4 work on the DMK Engineering URI FOB device under Allstar app_rpt. The purpose I wanted to use the GPIO pins was to control a fan relay to cool down my radios. Also in the future make good use of these pins for other projects.

Even though a thermal relay can be glued or bolted to the radio’s heat sink, I just wanted to play with the GPIO pin’s. I already had a fan installed on the radio, so I just wanted to migrate from using the PTT pin turning the fan on and off to using the URI FOB GPIO pins and an external script based on the K0KN tx fan script to control the fan.

 

Hardware

Reading up on the DMK Engineering URI FOB, there are 4 GPIO pins coming from the CM119A sound chip. GPIO pin 3 is the PTT, so only pins 1,2 and 4 are available. But GPIO pin 2 can only be used for input, while pins 1 and 4 can be use for input and output. See URI FOB Schematic as a PDF.

On the URI FOB, the DB25 connector pin 2 is labeled GPIO 1 and pin 4 is labeled GPIO 4. For input only, GPIO pin 2 is DB25 labeled pin 3. See URI FOB data sheet for details. With all this information on hand, the next step was to see how to control GPIO pin 1 from Allstar app_rpt.

Fan Circuit KP4TR for Yaesu FT2600

I use a Yaesu FT2600 radio as a node radio. What I do is use the mobile mounting bracket to hold a fan in place, facing towards the radio’s heat sink. See picture to the right. Also under the bracket I can fit a small circuit board that contains the relay and terminals to wire the fan to it, apply 12V to it, and have it turn the fan on and off. The circuit is an Arduino 1 channel relay, which are very cheap, sturdy, and has 4 mounting hole (4/40 inch). You can find them here.

 

Here is the URI FOB DB25 pin connector. Inside the male connector, you can wire in a 2N2222 or 2N3904 switching transistor. Connect a 4.7K resistor to pin 2, and in turn connect resistor to base of transistor (middle pin). The emitter goes to ground, and collector goes to the relay switch through the other end of the cable. You can also see this diagram for using a NPN transitor and a relay.

This is the picture of the Arduino 1 channel relay up close with wiring description.

I suggest you use a separate 12V supply to fan. I had some buzzing noise in my audio, so I used a separate 12V source for the fan and that fixed it. A 12Volt 1 amp should do like used by routers and CCTV cameras.

App_Rpt Configuration

To have Allstar and app_rpt manage the GPIO pins, I find the GPIO pins are controlled from a “cop” command. The details on this are located in this document: Manipulating GPIO (and Parallel Printer Port) signals within chan_usbradio and chan_simpleusb. Also, I need to use the Allstar “events manager” to keep track of the state of the PTT on the radio. The details are located in the article Event Management Subsystem.

So as to not go into excruciating details, these were my requirements:

  • I wanted to control the fan through a shell script.
  • I wanted the fan script to know the state of the radio PTT (on or off).
  • I needed the script to turn the fan on and off similar to logic per the tx fan script logic ported from IRLP by K0KN.
  • I want to use an Arduino 1 channel relay like this (click here)

Assuming my node number is 1999 (pseudo node as example), the changes needed in rpt.conf and simpleusb.conf (or usbradio.conf) are these:

Add to simpleusb.com designate GPIO pin 1 as “default state off (ground):

; When you configure a GPIO pin, you can either designate it as
; 'in' (input), 'out0' (output with a default state of 'off'),
; or 'out1' (output with a default state of 'on').

gpio1=out0 ; in, out0 or out1

 

Next modify the rpt.conf to add an event entry in the node stanza [1999]:

events=events1999

Next add an events stanza as defined above. It will create 2 events to monitor the radio TX key and create an empty file in the /tmp folder. If the file exists, the radio is transmitting. If it does not exist, it is idle.

[events1999]
touch /tmp/RPT_TXKEYED1999 = s|t|RPT_TXKEYED
rm -f /tmp/RPT_TXKEYED1999 = s|f|RPT_TXKEYED

Next, add 2 functions that will eventually be called to turn on and off GPIO pin 1. Create in [functions1999] stanza corresponding to node 1999

90=cop,62,GPIO1=0 ; Turn off GPIO 1
91=cop,62,GPIO1=1 ; Turn on GPIO 1

Now we have what we need for app_rpt to receive commands to control the fan (GPIO pin 1), and inform us of the state of the radio transmitter key. The only thing we need is a process to manage the fan control process, and we will do this thru a shell script.

 

Testing GPIO Changes

To control the fan, we use an external shell script. We will control the fan by turning it on and off using the app_rpt function configured earlier below to execute a cop command. To test it, use the asterisk CLI command to execute function *91 to run cop command defined above (use your node number instead of 1999)

asterisk -rx "rpt fun 1999 *91"

To turn fan off:

asterisk -rx "rpt fun 1999 *90"

If fan turns on and off, we are ready to use a shell script to control the fan.

We also want to test that we can see the file created in the /tmp directory when the radio PTT is active. When radio is transmitting. you will see a file in the /tmp folder. File disappears when radio goes back to RX:

ls /tmp
-rw-r--r-- 1 root root 0 Aug 24 17:59 RPT_TXKEYED1999

 

Adding Fan Scripts

We will be putting a few scripts in the /etc/asterisk/local folder.

First, add the following line to end of existing /etc/rc.local

/etc/asterisk/local/run_app_rpt_fan.sh

Next, create a script to /etc/asterisk/run_app_rpt_fan.sh and add this, using your own node number:

#!/bin/bash

killall -e app_rpt_fan.sh > /dev/null
/etc/asterisk/local/app_rpt_fan.sh 1999 > /dev/null &

Make it executable with a:

chmod +x /etc/asterisk/local/run_app_rpt_fan.sh

 

Next, copy/download the main fan shell script into /etc/asterisk/local/app_rpt_fan.sh.
http://latinovoip.net/hamradio/allstar/app_rpt_fan.sh

Make it executable with a:

chmod +x /etc/asterisk/local/app_rpt_fan.sh

 

Or copy/paste script below. You can customize how much time fan will turn of while radio is transmitting, and how long to keep fan on after radio stops TX. Enjoy!

#!/bin/bash

#########################################################################################
#
# filename: app_rpt_fan.sh
#
# description: This file controls a PC fan attached to a DMK Engineering URI FOB
# It starts up when PTT is detected,
# and will stay on for the time period specified in
# the TIMEAFTER variable after the PTT drops.
# If the PTT is triggered in the countdown period, the timer resets.
#
# original authors: The majority of this script (the state manager) was taken from
# the ID scripts by KC6HUR and WW4M.
#
# history:
# 2005-02-16 kd6hwc Initial creation (with help from other scripts)
# 2007-07-29 k6jwn Added new variable PTTON, fan won't come on until this timer reaches it's limit.
# 2012-01-28 k0kn Adapted script for app_rpt system
# 2014-08-24 kp4tr Modified script for DMK Engineering URI FOB
#########################################################################################
#
# Modified from http://www.qsl.net/k0kn/txfan script by Kyle K0KN
# To use the GPIO pin on DMK Engineering URI FOB GPIO pin 1
# DB25 pin 2 and the functions+events stanza in rpt.conf
#
# Use diagram from http://www.dmkeng.com/images/GPIO_Relay.pdf
# The GPIO pin requires using a NPN switching transistor like
# 2N2222 or 2N3904 since it only draws 8 ma.
# You can use the Arduino 1 channel 12V relay boards for fan switch
# or build your own. Inside URI FOB DB25 male connector add a 4.7K resistor
# to base and wire inside DB25 male connector like this:
#
# Emitter to ground (DB25 pin 13,19,20 or 25)
# Base to DB25 pin 2
# Collector is your switch that would go to relay ground (-) or
# to arduino 1 channel relay input
#
# This is created in the [events] stanza of the node in rpt.conf
# When PTT is keyed and deleted when unkeyed
# In rpt.conf define in functions and events stanza
# Change 1999 to your rpt.conf configuration node number.
#
# [functions1999]
# 90=cop,62,GPIO1=0 ; Turn off GPIO 1
# 91=cop,62,GPIO1=1 ; Turn on GPIO 1
#
# [events1999]
# touch /tmp/RPT_TXKEYED1999 = s|t|RPT_TXKEYED
# rm -f /tmp/RPT_TXKEYED1999 = s|f|RPT_TXKEYED
#
##################################################################

#set -xv

if [[ $# -eq 0 ]];then
echo "Need a valid local Allstar node number to run fan script"
exit
fi

#this is set from events stanza to create or destroy this file
PTTSTATE="/tmp/RPT_TXKEYED$1"

#Your custom function defined in rpt.conf to turn URI FOB GPIO pin 1 on and off
FANON="/usr/bin/asterisk -rx \"rpt fun $1 *91\""
FANOFF="/usr/bin/asterisk -rx \"rpt fun $1 *90\""

# Define the period of time after the PTT drops to shut off the fan
TIMEAFTER=600

# Define the max PTT time before the fan is activated
PTTON=10

#### No changes needed after this line

# define variables
declare -i IDLETIMER
declare -i PTTKEY

# Start with state set to "Initial"

STATE="Initial"

while [ TRUE ]
do

case "$STATE" in

"Initial") # Wait for first keyup, then proceed to Countdown

echo -en "\r"`date ` "STATE = Initial\n"
sleep 1;
PTTKEY=0

while [ TRUE ]
do
echo -en "\rPTT Timer: $PTTKEY "
sleep 1;

# while PTT is silent (PTT=T)
if [ -f $PTTSTATE ] ; then
PTTKEY=$PTTKEY+1
fi

if ! [ -f $PTTSTATE ] ; then
PTTKEY=0
fi

if [ $PTTKEY -gt $PTTON ] ; then
break
fi
done
echo -en "\nPTT timer expired, activating fan"
eval $FANON
STATE="Countdown"
;;

"Countdown") # Count down period, if PTT is triggered, reset the timer.

echo -en "\n\n"`date ` "STATE = Countdown\n"
sleep 1
IDLETIMER=$TIMEAFTER

while [ TRUE ]
do
echo -en "\rFan powerdown timer: $IDLETIMER "
IDLETIMER=$IDLETIMER-1
sleep 1

if [ -f $PTTSTATE ]
then
IDLETIMER=$TIMEAFTER
fi

if [ $IDLETIMER = 0 ]
then
break
fi
done

echo -en "\nTimer expired, deactivating fan"
eval $FANOFF
STATE=Initial
;;

esac
done

echo -e "\n\nOops! - Not supposed to get to here"