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)

Replacement to HP microserver

I’ve had my HP microserver for four years this summer and it’s been a great little machine and very reliable, but the time has come to upgrade the machine as it doesn’t run very quitely and could be more power efficient. I’m currently using Ubuntu 12.04 and am a little bit nervous about upgrading to 16.04 – so a new machine allows me to transfer over in a more leisurely fashion.

The replacement is going to be using an ASRock low powered CPU motherboard machine to minimise power consumption and noise. I’ve used an ASRock motherboard in my router and it runs virtually silent as I didn’t need any fans running in the case.

My first choice was the ASRock J3710-ITX, but its currently retailing at £40 more than the ASRock N3700-ITX, so I’ve gone with the cheaper one. The main difference between the boards is the CPU (see 1 and 2 at the end), but since this is primarily a file server, I’m not concerned about the N3700 being lower frequency.

The rest of the parts are on a wishlist until the motherboard has been shipped, but the plan is:

  • 8GB Kingston RAM
  • OCZ Trion 100 120GB SSD
  • StarTech 5.25in Trayless Hot Swap Mobile Rack
  • WD Red 6TB NAS HDD
  • WD Purple 6TB Surveillance HDD
  • Antec P50 mATX Case
  • Antec Earth Watts 380W 80+ Bronze Power Supply

(the list can be seen here: http://www.ebuyer.com/lists/471177ACE13237EA7FE0CFEC53968C65)

The file server will be in a RAID 1 configuration using the Red and Purple WD drives, the SSD being the O/S drive.  I’ve deliberately not selected two reds or two purples so that the drives should be made at different times and shouldn’t both be from the same batch.  Most likely I’ll repurpose the existing 2TB drives to I have for the offsite backup to save money!

The 5.25in rack allows me to plug in one of the offsite HDDs without opening up the case. The case will be used to home my router machine, allowing me to put the file server into the Lian Li media center case.

 

1) http://www.cpu-world.com/CPUs/Pentium/Intel-Pentium%20N3700%20Mobile%20processor.html

2) http://www.cpu-world.com/CPUs/Pentium/Intel-Pentium%20J3710%20-%20FH8066501715931.html