rigposter/rigctld3.php
2023-01-16 19:27:05 +01:00

109 lines
4.0 KiB
PHP

<?php
$mycall='XXXXXX'; # my call sign
$apikey='XXXXXXXXXXXXXXXX'; # my qrzcq api key
$rigctl='/usr/bin/rigctl'; # where rigctl lives
$rmodel='363'; # rig model for hamlib
$device='/dev/ttyUSB1'; # serial device connected to rig
$dspeed='19200'; # serial speed set in rig
$ssb_wid='2400'; # Icom IC-7600 -> Fil-2
$cw_wid='500';
###############################################################
print "==> RIG CONNECT... ";
$rigcmd=$rigctl.' -m '.$rmodel.' -r '.$device.' -s '.$dspeed;
$socket=popen($rigcmd,'r');
print "OK (".$device.")\n";
print "CMD[".$rigcmd."]\n";
$last_read=time();
while ( TRUE ) {
# GET
// get beg
$data = new stdClass();
$data->auth = new stdClass();
$data->auth->call=$mycall;
$data->auth->key=$apikey;
$get=qrzcq_api_rig('get',$data);
// get end
if ( $get->status == 'OK' ) {
if ( isset($get->rig) ) {
if ( $get->rig->set->freq > 0 && $get->rig->set->freq <= 54000000 && strlen($get->rig->set->mode) > 0 ) {
$freq=$get->rig->set->freq;
$mode=$get->rig->set->mode;
################################################################
## DO WHATEVER YOU NEED TO SET YOUR RIG TO THIS FREQ AND MODE ##
################################################################
// here for Icom IC-7600
if ( $mode == 'CW' ) { $wid=$cw_wid; } else { $wid=$ssb_wid; };
$amode=array(); exec($rigcmd.' M '.$mode.' '.$wid,$amode,$mret);
$afreq=array(); exec($rigcmd.' F '.$freq,$afreq,$fret);
print "==> RIG SET TO ".number_format($freq,0,'','.')." ".$mode." ".$wid."\n";
################################################################
################################################################
}
}
usleep(1000);
} else {
print "ERROR: QRZCQ GET FAILED\n";
print "SLEEP(10)\n";
sleep(10);
}
unset($data);
# PUT
if ( time() != $last_read ) {
#############################################################
## DO WHATEVER YOU NEED TO READ THE FREQ AND MODE FROM RIG ##
#############################################################
// here for Icom IC-7600
$afreq=array(); $get_freq=$rigcmd.' f'; exec($get_freq,$afreq,$fret); $freq=trim($afreq[0]);
$amode=array(); $get_mode=$rigcmd.' m'; exec($get_mode,$amode,$mret); $mode=trim($amode[0]);
$asmet=array(); $get_smet=$rigcmd.' l STRENGTH'; exec($get_smet,$asmet,$sret); $smet=trim($asmet[0]);
if ( count($amode) > 1 ) { $wid=trim($amode[1]); };
#############################################################
#############################################################
if ( $freq*1 > 0 && $mode == strtoupper($mode) ) {
#print "RIG READ ".$freq." ".$mode." ".$wid."\n";
$smet=$smet+54; # dB S9 -> dB S0
// put beg
$data = new stdClass();
$data->auth = new stdClass();
$data->auth->call=$mycall;
$data->auth->key=$apikey;
$data->request = new stdClass();
$data->request->mode=$mode;
$data->request->freq=$freq;
$data->request->smeter=$smet;
$put=qrzcq_api_rig('put',$data);
// put end
if ( $put->status != 'OK' ) { print "\nERROR: QRZCQ PUT FAILED\n"; };
}
$last_read=time();
if ( preg_match("/0$/",time()) && $freq*1 > 0 && $mode == strtoupper($mode) ) { print "ALIVE[_".number_format($freq,0,'','.')."_/".$mode."/".$wid."/".$smet."]\n"; };
usleep(1000);
}
# SLEEP
usleep(200*1000);
# STAMP
if ( mt_rand(1,1000) == 1000 ) {
print "STAMP[".gmdate("Y-m-d H:i:s")."]\n";
}
}
function qrzcq_api_rig($type,$data) {
if ( ( $type == 'get' || $type == 'put' ) && is_object($data) ) {
#print_r($data);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'https://ssl.qrzcq.com/api/rig/'.$type);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_POST, 1);
$result=curl_exec($ch);
if ( $result !== FALSE ) {
$ret=json_decode($result);
#print_r($ret);
return $ret;
}
}
return FALSE;
}