Posts Tagged ‘Python coding’
Python script shutting down the station
Next up is my Python script for shutting down my contest station. That probably makes it sound like I’m running some massive, high-end operation—but in reality, I just really like things to work smoothly. I find it incredibly handy to simply double-click a Python shutdown icon on my desktop and have everything taken care of automatically. Some might call that lazy, overly dependent on a PC, or a bit geeky. I prefer the last option, and I’m fairly certain my dear wife would agree.
The reason I use a Python script for shutting the station down is the same reason I use one for startup: everything shuts down in the proper order. The shutdown process is essentially the reverse of the startup sequence, with a few hiccups that required some creative workarounds.
One issue was that certain applications—Win4Icom in particular—take noticeably longer to shut down than they do to start up, so I had to extend some of the timing in the script to allow for a clean exit. Another problem was that Win4Icom was not shutting down my Icom 7610 as it’s supposed to. As a result, the radio was staying powered on until the Wi-Fi plug turned off, cutting power to the supply and abruptly killing the radio. That obviously doesn’t allow for a proper shutdown sequence.
In the video, you’ll see that the Icom 7610 now shuts down cleanly at the very end of the shutdown process. I accomplished this by adding Python code that sends the appropriate Icom HEX command to power down the radio correctly.
Python script energizes my contesting setup.
I have been working on some Python scripts to automate tasks that I would rather not do manually. The video above shows my contest station powering up from start to finish.
Why do this, you ask? The part of the amateur radio hobby that really interests me is CW contesting. Now that I am retired, I take part in four weekly one-hour “mini” contests and, in an average month, I spend two or three weekends CW contesting as well.
The programs I use need to be started in a specific order. For example, I must start my virtual port program (VSPE) first, and then start the radio and contest programs in a certain sequence. If this order is not followed, I get flooded with error messages.
Some say I over complicate things, but I am somewhat of a perfectionist and like everything set up just so. Python lets me achieve that with a single mouse click. There has been some fine-tuning of the Python code along the way. For example, my radio control program Win4Icom is delivered as a complete new program with each update, rather than as an add-on to the original, as the N1MM+ contesting program does. Because of this, I wrote the script for Win4Icom so that it always checks for and runs the highest revision number of the program; otherwise, it would just continue to load the same old version.
I also had to introduce delays between the startup of some programs. After VSPE starts, I delay Win4Icom by five seconds, because VSPE is still initializing in the background and Win4Icom would otherwise throw errors. I also added a step to minimize VSPE to the taskbar once it starts, so it does not sit on the screen. As Win4Icom starts, I added a 10-second delay while it powers up my Icom 7610 and sets up the COM port configurations for N1MM+. I found N1MM+ needs to wait for those steps to complete before it begins its own startup. Finally Reverse Beacon Network web page is setup to open with my call sign showing spots where it is heard.
Here's the sequence shown in the YouTube video:
WiFi smart plug powers on.
Astron power supply powers on seen in the lower left corner .
Raspberry Pi 4B boots (red power LED visible on desk), launching HamClock.
VSPE starts and minimizes to taskbar.
Win4Icom launches, powering on the Icom 7610.
Reverse Beacon Network loads/refreshes as HamClock continues to load.
N1MM+ Logger starts.
Next up: Python shutdown scripting (with its own challenges). I'm also 3/4 through a script to generate ADIF file of contest contacts that exports and auto-upload to N3FJP, Club Log, LoTW, CWops, and QRZ.com. Stay tuned!
Step one in my Python coding adventure
In my first post regarding my Python adventure, I shared how I wanted
to use python coding in the shack. As my readers know I am an avid CW
contester and before a contest begins I have 5 programs that need to be
launched. The issue is if I launch them out of order or if one program has an issue things get all
screwed up, and I have to start all over again. In doing so, most of the
time I need to go into some programs and reconnect some com ports and
clear lots of error messages. My first Python project will be one
double-click on a desktop icon that will get all my contest stuff up and
running smoothly in the right order.
The approach I am using is
to write python code to start each individual program. Then save those in a file. This will get my whistle wet with python coding, both
with success and some head scratching. At this point I have coded out
most of my programs so they start. Once that is fully completed, it will
be grouping them all together into one process.
In a nutshell, here is the plan:
– Turn on a Wi-Fi plug which powers on my power supply and Pi4B power supply
– Start my VSPE virtual com port program and minimize it.
– Start my Wn4icom program which also starts my Icom 7610 radio.
– Start my N1MM contest software.
–
Then finally Firefox will start, open Reverse Beacon Network, log me
in, set up search for my call and set it to refresh my call sign spots
every 10 minutes.
Lets take a fast look at the python coding for the WiFi plug.
import asyncio
from kasa import SmartPlug
async def main():
plug = SmartPlug(“10.0.0.71”) # Replace with your plug’s IP address
await plug.update()
await plug.turn_on()
print(“Plug turned on”
asyncio.run(main())
For the Kasa smart WiFi plug by TP link to work I had to first download into python the kasa library. I opened up python and entered the code below…well actually cut and paste.
pip install python-kasa
Now below in a nut shell is what the code is all about for the Kasa WiFi plug to turn on.
1. Python loads the needed modules. (asyncio and SmartPlug)
2. Program defines async functions or in English connecting to the wifi plug could take time and this allows things not to freeze if the process takes time.
So now modules are loaded and it knows some actions could take time.
3. Now asyncio.run(main()) runs and this is what happens.
Smart plug is created for IP address 10.0.071
The plugs state is defined (on or off)
The plug is turned on via network command
A message printed in python code window “plug turned on”
Program closes.
In closing I am not by any means a pythonista regarding code and I am sure many who are can poke holes in the coding or what I left out regarding what to explain. This is my first attempt at this game and I was actually shocked that it worked. BUT your input will and always is welcomed.
Next post is about the learning curve, hiccups and added lines of code for smoothness and reliable start up.












