I decided to make this device because every once in a while when I am out and about with my laptop I need just a bit more processing power than my laptop can offer. To accommodate this I usually end up connecting to my desktop PC via SSH, and for the most part this works great for me. Though this approach does have a few caveats the main issue being that the PC always has to be on and idling not suspended or in sleep mode. In the case that the PC is in sleep mode I can't connect to it over SSH and someone has to press the power button to take it out of sleep mode. Now this isn't the biggest deal in the world, but leaving the PC idle for days or even weeks at a time just for the off chance that I'll need to connect to it while I'm out brings its own set of issues. If I leave the PC idle at all times it is going to consume unnecessary power, introduce unnecessary thermal strain, and accumulate lots of dust that I’ll have to clean later.
How I implemented thisHardwareI decided that the best way to implement something like this would be to just use the PC’s header pins and mimic the action of the power button that was usually connected to those. To do this I connected a transistor to the GPIO pins on my Particle Photon 2 board. To trigger the automation I used a POST request that was sent to the Particle, when it received this POST request it would turn the PC on by pulling the base pin (middle pin) of the transistor to ground resulting in the two outside pins (right and left pins) of the transistor being bridged. The right and left pins of the transitory were each connected to one of the two pins for the power button on the motherboard. Bridging these two header pins is exactly what pressing the power button does, so we are really just mimicking the action of the power button using the transistor.
This is where I ran into the most trouble. First I started with implementing the POST request, I ran into a few hiccups with this though. The first hiccup being that I wanted to send a parameter with this POST request that would tell the Particle how long it should bridge the pins for. I had a bit of trouble figuring out how this was supposed to be formatted. Initially I thought that the parameter was supposed to be included in the URL of the request like so:https://api.particle.io/v1/devices/DeviceID/FunctionName?arg=CommandHowever that's not the case or at least that's not how you do it now, I knew that Particle used to store access tokens in the URL but then switched to including them in the headers of the POST request instead. So I assumed that they probably did something similar with the way that they take parameters from POST requests also and it turns out that they do, parameters are stored in the body of the POST request not in the URL. And that would look something more like this if we were to do if from the command line:
curl -X POST "https://api.particle.io/v1/devices/DEVICE_ID/powerButton" \
-d arg=Command \
-d access_token=YOUR_ACCESS_TOKENSo triggering the POST request with curl is great and all but I don't really want to have to get out my laptop, open up a terminal window, and type all that every time that I want to trigger the Particle. I felt like triggering this from my phone might be a bit easier, so I decided to set this up in iOS's shortcut app.
The next bit of software trouble that I ran into wasn't actually related to the particle or the software on it at all. I use Remote.it to connect to my PC via ssh because I don't want to have a public facing port on my home network, much less one that's set up for ssh. Remote.it works great for me most of the time, it's free, pretty reliable and I really haven't had any issues with it outside of this one: After the PC would wake up from sleep, Remote.it's system services would take about ~8 minutes to start back up. This really stumped me, I had messed around with sleep states in linux a little bit before but I hadn't ever dealt with a problem like this, not to mention the PC was not in sleep mode the problem just happened after exiting sleep mode. After a some googling and a lot of chatGPT I concluded that the best way to fix this was just to have a script that restarted all Remote.it services whenever the computer woke up from sleep. So I a bash script that restarts all Remote.it services on wake in /usr/lib/systemd/system-sleep/ and made it executable with the chmod command. The systemd/system-sleep/ directory is where systemd-sleep.service looks for shell scripts that it's either supposed to run after waking up from sleep or after before going to sleep, our script doing the latter. The script can be seen below:
#!/bin/bash
case $1/$2 in
post/*)
# Wait a moment for network to be ready
sleep 3
# Restart all remote.it services
systemctl restart demuxer.service
systemctl restart schannel.service
systemctl restart 'remoteit@*.service'
# Log the restart
logger "remote.it services restarted after suspend"
;;
EsacThis script solved the 8 minute delay problem that I was having and the connection time was actually cut down to around 20-30 seconds after triggering the automation. After that I was finished with the project, right now everything seems to be working properly and looks stable. You can see it at work below!https://drive.google.com/file/d/1kIYl_CQUWE8eNy2h77ys4n4h9LO_TaJh/view?usp=drivesdk





Comments