Hive SmartPlug and XBee

Since finding out that Hive have restricted their older ‘Smart’ plug from being remotely switched on/off, it become a mini obsession of mine to prove that the plug is still actually capable of being remotely controlled using ZigBee, because I was convinced that is is the same hardware as its predecessor from AlertMe.

Hive only advertised the older SmartPlug as a basic ZigBee booster device to help people improve signal strength in situations where the Hive hub struggles to communicate with its thermostat in larger houses. For this reason they claim they never intend make the SmartPlug remote controllable, because this may encourage people to move the plugs to different locations which may not be optimal to be a signal booster (and, possibly more pertinent, it would discourage people from buying the newer ActivePlug).

In this post I describe my efforts to remotely control a Hive SmartPlug via a XBee and Python code. Much of this post takes inspiration from the Desert Home’s blog in which he has done much of the groundwork controlling the Hives american cousin called ‘Lowe’s Iris’ (also manufactured by AlertMe). I also referred to the Tickett’s blog post for more specific research into the AlertMe ZigBee Profile.

Resetting SmartPlug

In order to pair the SmartPlug with the XBee the first thing we need to do is disassociate it from the Hive hub. Following instructions from the British Gas website:

To reset the SmartPlug, please hold down the button on its top until the light on the front has turned on and then off again (this can take up to 15 seconds). If the SmartPlug is brand new, it might need 30 mins of charge before you can do this. At this point you then need to press the button on the top four times repeatedly, doing this once a second. The logo on the front of the SmartPlug should light up solid for a few seconds, before starting to flash. It is now ready to be added and installed on your system.

The flashing light means it is searching for an open ZigBee network to associate with.

Configuring XBee

Next, we need to configure an XBee to be able to talk with the SmartPlug. Using the XCTU configuration software program a Series 2 XBee with the following settings:

XBee Modem XB24-ZB, ZIGBEE Coordinator API, Version 21A7 (or better)

ZigBee Stack Profile (ZS): 2

Encryption Enable (EE): 1

Encryption Options (EO): 1

Encryption Key (KY): 5a6967426565416c6c69616e63653039

API Enable (AP): 2

API Output Mode (AO): 3

Showing SmartPlug Frames

The above settings place the XBee into a suitable mode to be able to act as a ZigBee HA (Home Automation) co-ordinator. The key, ‘5a6967426565416c6c69616e63653039’, is the standard key for all ZigBee HA profile devices and is hex for ‘ZigBeeAlliance09’.

Following setting the above, I then switched to console mode in XCTU and opened the XBee serial connection to see if I could see any messages, and while pressing the button on the top of the SmartPlug, I could see ZigBee frames in the XCTU console – yay! we have communication…

Python Code

Now that we have proved connectivity between the SmartPlug and the XBee, we next need to write some code in order to send ZigBee messages to the plug. After attempting to read the ZigBee HA specification I got very confused, but after immersing myself in further documentation and YouTube videos I eventually started to understand enough about ZigBee to be able to at least attempt to progress to the next part of this project (this is where Desert Home’s blog provided a huge leap in progress, as had done much of the groundwork already).

At this point I could go off on a tangent to try and explain what I have learnt about ZigBee Profiles, Clusters and End Points but this would be a blog post in itself (which one day I may write), for now I am going to try and keep the reminder of this blog fairly high level. However, one point to note is that AlertMe (Hive) have extended the standard HA profile with their own proprietary set of messages (also known as clusters).

So, armed with the above Python library we can start to exchange messages with the SmartPlug. The following is a summarised version of my code intended to aid discussion and highlight some of the basics. The code below demonstrates simply turning the SmartPlug switch on and off.

The full code for this project can be found on my GitHub site which contains a more featured version of this script which, as well as switch control, also reads power, signal strength values as well as software version information. I would very much welcome anyone who wants to contribute or improve on this code.

The code above will initiate communication with the SmartPlug and then toggle the switch on and off once per second. The code also prints the data content of the received ZigBee packets to the screen. Once again I really must give credit to Desert Home as the origin of much of the above code comes from his blog. The following is a breakdown of the code along with explanations.

Declare a couple of standard ZigBee Profile ID constants. Every data request in ZigBee is sent with an Application Profile ID which are 16-bit numbers and range from 0x0000 to 0x7fff for public profiles and 0xbf00 to 0xffff for manufacturer-specific profiles.

Also declared are the broadcast, switch short and long addresses. Note at this point the switchLongAddr and switchShortAddr are unknown. These are learnt later and populated following receiving a broadcast response.

The following receiveMessage() function is called each time a packet is received by the XBee. It is the job of this function to read, interpret and possibly respond to the packet.

All ZigBee devices have 2 addresses; a long 8-byte MAC address and a short 2-byte address used to reduce transmission time. One of the first tasks of the receiveMessage() function is to take note of the incoming packets long and short addresses and populate the global variables switchLongAddr & switchShortAddr accordingly.

The function then reads the Profile ID, Cluster ID and Cluster Command to determine the type of packet it is.

During initial negotiation, after transmission of certain packets, the SmartPlug expects certain appropriate packets in response. There are a couple of cases of this below, on receipt of a packet with ZDP_PROFILE_ID and clusterId 0x0006 Active Endpoint Request, the function responds back to the switch with a couple of acknowledgment packets.

We also use pp to print the packet contents to the screen for debugging.

Finally, this is where we send a message to the SmartPlug to toggle the switch relay on and off each second. We setup an infinite loop in which we first transmits a broadcast packet and waits for the switchLongAddress to be set. Once the long address is learnt we continue to loop sending a packet to the SmartPlug requesting the switch relay is turned on and off.

We now know the Hive SmartPlug IS capable of being remotely switched on and off as well as power usage values read, however I don’t think Hive will oblige any time soon to enable this, especially now they have launched their newer ActivePlug.

What would be interesting to explore next is whether the same approach could be taken to control other Hive devices such as the thermostat or newer ActivePlug. Another avenue to explore is whether this could be used as a base to write a SmartThings device plugin.

Note: On occasion, usually following having had the SmartPlug associated back with the Hive hub, the SmartPlug would refuse to communicate with the XBee (or at least with the Python code) even after running through the disassociation procedure. Although the RX, TX and RST LEDs would flicker on the XBee no messages could be read. I discovered that I needed to leave both the XBee and SmartPlug on for 24-hours after which they would behave and communicate again. I do not know the reason for this, but assume there is an association time-out which needs to run its course. If anyone knows the reason for this I would love to know!