6th Power Cut In 2 Months Causes Networking Crisis In My House!!

Yesterday morning I woke up and checked my email from my phone (as I normally do)… I’d received no emails overnight which was a bit odd and then noticed that I’d switched off 4G the night before and didn’t have a Wifi connection.

Since I have a Wifi access point in my room, that was a little strange. The phone was stuck in “Obtaining IP Address” mode… Maybe the phone’s having problems with that access point.

I wandered off downstairs to feed the cats and noticed the oven times were blinking 00:00 – great, power cut… 6th in less than 2 months!! The dashboard in the kitchen was showing nothing with all the Splunk panels saying failed to create search… Refreshed the dashboard – still nothing, infact Splunk is down, not even an nginx failure! Uh-Oh…

It turns out that the pfsense box had got stuck during boot and required someone to press f1 to continue booting – not very helpful! Unfortunately I didn’t know this until about 3pm and had hastily ordered a new SSD before popping out to go shopping as I thought perhaps the boxes SSD had failed 🙁

It’s certainly made me wonder if a UPS might be a good idea!

Ubiquiti mFI mPower Project – Part 1

This year I upgraded the home monitoring suite in my house from a Wattson to a Splunk dashboard running on a tablet in the kitchen. The primary reason for doing this was that after getting the Tesla Powerwall, Wattson no longer was able to correctly state how much power was being imported/exported from the home until the battery was completely charged. The downside of getting rid of Wattson was that we relied on the Optiplugs to run the immersion and a radiator when there was excess power being sent to the grid. So I needed another solution to running the immersion on the rare days when the Powerwall will be completely charged and we’re exporting to the grid.

After a fair amount of time searching for a wireless plug with an API, I found the following plug https://wifiplug.co.uk/ that ticked all the boxes, but being the little bit paranoid I am, I didn’t really want a plug that could theorietically be controlled by anyone from the internet. Also, if that company went bust, how would I control the plug without their API gateway?

I then spent another handful of hours searching and came across the following blog https://blog.linitx.com/ubiquiti-mfi-mpower/ which happened to use a wifi plug that doesn’t rely on an internet connection and is from the same company that I use for my WiFi access points! The only problem is that unlike the wifiplug, this one doesn’t have an official API and there isn’t a UK plug version… But what the heck, at least it’d be an interesting project!

So I bought the European single socket version from wifi-stock.co.uk/. The next day I got a call from wifi-stock to check “I hand’t made a mistake and that I did realise it wasn’t a UK power socket” – that was nice and a good idea! 🙂

I also needed a UK to European adaptor and a European to UK adaptor to be able to use it in a UK socket and power a UK device. This makes the plug stick out quite far from the socket, but it is secure and doesn’t drop over time.

A couple of days later my plug arrived and I set about logging into the device as mentioned on the blog using ssh. That was suprisingly easy as it uses the default username and password that’s used on some of the other Ubiquiti devices – username:ubnt, password:ubnt. I spent a couple of hours poking around on the file system, making the lights blink on and off and tested out that it was possible to turn the plug on and off by echoing 1/0 to the relay

echo 1 > /proc/power/relay1
echo 0 > /proc/power/relay1

I also found the code for the web GUI on the plug /usr/www/mfi/power.cgi (the code behind this: http:///mfi/power.cgi). And that got me thinking…

I was originally going to control the plug from my server via the home monitoring application using ssh, but that means the Java app will have to invoke the ssh command and I’d prefer to have an API on the plug that I could call. I could write another cgi file like the power.cgi file, remove the session aspect and control the plug as Ubiquiti have done, but why bother! Ubiquiti has done all the leg work – I can just use their web app! 🙂

So I began playing with the power GUI and found that it was making POST and GET requests from the GUI to http:///sensors/1/ to fetch the current status and turn the plug on and off. Well that’s easy to control! After a bit of experimentation, these are the four curl commands I need.

Login

You will get an AIROS_SESSIONID back which needs to be put in as a cookie header in the following curl requests.

Get plug data

curl -v \
    http://<device ip>/sensors/1/ \
    -H "Cookie: AIROS_SESSIONID=015152b0143a0267f8f0ae30fcb42f6f"
{"sensors":[{"state":{"output":0,"power":0.0,"energy":0.0,"enabled":0,"current":0.0,"voltage":0.0,"powerfactor":0.0,"relay":0,"lock":0}}],"status":"success"}
{"sensors":[{"state":{"output":1,"power":0.0,"energy":0.0,"enabled":0,"current":0.0,"voltage":0.0,"powerfactor":0.0,"relay":1,"lock":0}}],"status":"success"}

Switch the plug off

curl -v \
    -X PUT \
    http://<device ip>/sensors/1/ \
    -H "Cookie: AIROS_SESSIONID=015152b0143a0267f8f0ae30fcb42f6f" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "output=0"
{"status":"success"}

Switch the plug on

curl -v \
    -X PUT \
    http://<device ip>/sensors/1/ \
    -H "Cookie: AIROS_SESSIONID=015152b0143a0267f8f0ae30fcb42f6f" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "output=1"
{"status":"success"}

Output and enabled on

The above is only controlling the output field but on the GUI you will notice there are two fields, output and enabled. Switching enabled on gives stats about the plugs energy usage but it’s not required to control the device.

To switch the plug on with output and enabled on, simply run

curl -v \
    -X PUT \
    http://<device ip>/sensors/1/ \
    -H "Cookie: AIROS_SESSIONID=015152b0143a0267f8f0ae30fcb42f6f" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "output=1&enabled=1"
{"status":"success"}

If you then request the plug data, you would get something similar to the following response:

curl -v \
    http://<device ip>/sensors/1/ \
    -H "Cookie: AIROS_SESSIONID=015152b0143a0267f8f0ae30fcb42f6f"
{"sensors":[{"state":{"output":1,"power":958.341251254,"energy":44.0625,"enabled":1,"current":4.023638963,"voltage":238.905914783,"powerfactor":0.99695206,"relay":1,"lock":0}}],"status":"success"}

 

In the next part I’ll go through how I controled the plug from Java using the APIs above.

Chromecast Ate My Internet Data Allowance!

In January I bought a Chromecast so that friends and family could cast videos onto the main living room TV. It’s a handy little device and when idle it displays lovely background images of nature, space and various other things. I don’t know why, but I never considered where it gets those lovely background screen saver images from…

As I previously have mentioned, I have a capped Internet data allowance of 64GB per month. This month (11th March to 11th April) we ran out of data two days early for the first time this year. It’s incredibly painful having to wait a couple of days until the Internet comes back on 🙁 Yes I know – I need to get out more!

I logged into my home router to see who’d gobbled the most data this month and give them a bit of a shouting at only to discover that the top user for the month was the Chromecast, beating even me to top data user!!

Chromecast was using 700MB+ per day IDLE!

Luckily I managed to find a web page explaining how to tame the Chromecast and restrict it’s data usage by giving it access to a Google photo album with 1px images.

I found that I needed more than two of these images and that each had to be a slightly different colour or Google photos was clever enough to not upload duplicates. The backgrounds can be seen in my shared album here: https://photos.app.goo.gl/GPf1UFMRN5Rw1Xaq2. I also found that you have to make sure that when you disable the other sources of pictures that you untick all the items within the category before marking the category as “off”.

The effect of using the new 1px album images was instant and hopefully I won’t end up running out of data two days early next month 🙂

(Chromecast limited at 9pm Thursday 12th April)