Automation of the remote radio on/off control

Today’s offering is a serious head melt so I apologise in advance. That said it makes the remote management of the radio or any device attached to a PC and controlled via a relay board much more elegant.

The driving force for this was the fact that on several occasions I’ve forgotten to switch the radio off before disconnecting from a VNC session or even when I’m sat at the machine. This is despite having written nice little command line scripts that give you a nice coloured flag to confirm the action when you double click on the desktop icon!

The logic followed that when the PC was switched on, the radio would always need to be powered up and ready to go. The objective was to get the PC to power the radio on at start up and power down at switch off.

I researched various methods of achieving this and the method provided below works 100%. There are other options available on the internet but after much head scratching it just wasn’t going to happen using those methods.

Firstly, you need to create two batch files containing the line of java code which switches the radio either on or off.

Using your favourite text editor (I favour Vim http://www.vim.org/) simply recreate the script in a file called radio on.bat and radio off.bat, for the relevant function.

Screenshot 2014-12-07 10.50.30

Make a folder in the root of drive C on the machine called radio scripts. Copy your two batch files to there.

The next trick is to get Windows to do something useful with these files. Task Scheduler overcomes a lot of problems in automating processes and I would suggest this rather than using gpedit.msc and trying to link the execution of the batch files to Local Computer Policy script execution at startup and shutdown.

Trying to keep this simple, within Task Scheduler create a new Basic Task. Follow the wizard through and populate the fields with the information below. You only need the pertinent bits, the rest can be left at default settings.

Screenshot 2014-12-07 11.00.59

 

Screenshot 2014-12-07 11.01.12

Screenshot 2014-12-07 11.01.20

 

In short we’re creating a task called Radio ON, which is triggered at an event “sytem startup” with a desired action of executing a program “radio on.bat”

By rebooting the machine, the proof that this works is born out. The machine executes the bat file perfectly and the radio is powered on ready for use.

Getting things to switch off automatically are a bit more complicated. There needs to be a bit of tweaking of Windows to set various things in motion before this works and without it, you can just pack up and go home.

Firstly, you need to have gpedit.msc installed on your copy of Windows. It’s supposedly already there in Windows 7 Professional and Enterprise but don’t believe everything you read.

If you haven’t got it (type gpedit.msc in your ‘search programs and files’ bar after hitting the Start button to confirm) simply follow this guide http://www.askvg.com/how-to-enable-group-policy-editor-gpedit-msc-in-windows-7-home-premium-home-basic-and-starter-editions/

gpedit allows you to alter various group policies set on your machine. Just work with that and you’ll be fine.

The next trick is to create your Task to switch the radio off at system shutdown. Again, use Task Scheduler to create a Basic Task, but our trigger will be different this time.

Screenshot 2014-12-07 09.47.31

Screenshot 2014-12-07 09.48.46

Screenshot 2014-12-07 09.49.01

Screenshot 2014-12-07 09.49.23

 

Screenshot 2014-12-07 12.06.04

** Make sure that you select “Run whether user is logged on or not” and tick “Run with the highest privileges”.

After much playing with this I can’t offer a straight answer as to why these two are needed but guess it may be down to how Windows is processing the tasks at shutdown but without them selected the task reports that it has run but you see no activity on the relay board.

This time our trigger for executing our batch file is when an Event ID is triggered. The short version is when anything within Windows happens various flags are set to allow the system to know what’s going on. Event ID 1074, to quote Bill Gates’s boys “This event is written when an application causes the system to restart, or when the user initiates a restart or shutdown by clicking Start or pressing CTRL+ALT+DELETE, and then clicking Shut Down.”

So what we’re going to do is use this Event ID to drive the execution of Radio OFF.bat

The problem comes in that not all Event ID’s are recorded by the system by default. As such we need to enable it. This is where gpedit.msc comes in.

By running gpedit.msc you can edit the various settings at a very low level within the operating system. No fear, as all we’re interested in is the Shutdown Event Tracker group policy setting.

Screenshot 2014-12-07 10.39.29

You need to wade through the list until you find it and the default is Not Configured. Right click to bring up the edit menu and select the radio button for “enabled”. You don’t need to touch anything else. Select OK and close gpedit.msc

And that’s it. To prove that everything is working simply reboot the machine. On power up the relay board will switch in and power up the radio. On shutdown the relay board will switch the relay to off, powering down the radio.