NETIO Professional smart power socket for companies and geeks

Created on

NETIO Professional smart power socket for companies and geeks

NETIO professional smart sockets for businesses and geeks alike

 

  • Wi-Fi sockets do not all come from China
  • Appealing to developers, thanks to a simple JSON API (and the other APIs)
  • The manufacturer is working on a mobile app and cloud-based control of the outputs

 

 

 

We have recently tested in practice what a pass-through Wi-Fi socket from the Chinese Aliexpress can do – naturally bearing the risk that it is rather difficult to complain about a burned-down house to a Chinese e-shop.

 

That was actually the most frequent argument against buying this kind of cheap toys. They work, can be easily connected to Google and Amazon voice assistants, enthusiasts can completely reprogram them with just a little effort, but it is still cheap Chinese production with all the associated drawbacks.

Czech professional Wi-Fi sockets by NETIO

Today we will have a look at the opposite end of the spectrum where we find, for example, the professional Wi-Fi sockets by NETIO, made in the Czech Republic, an EU member state. These sockets conform to all relevant safety standards. With their API (Application Programming Interface), they are intended to be integrated with an existing system, such as a smart home or server room equipment.

 

In the top category, NETIO offers a complete PDU (Power Distribution Unit) that can automatically switch on and off up to four 230V appliances and communicate (depending on the model) over Wi-Fi, Ethernet or serial line using protocols such as Modbus, SNMP, MQTT and more. Some models can also very accurately measure electrical parameters.

PowerCable family

The NETIO portfolio also includes less expensive PowerCable units that look like a short power extension cable and can switch one 230V/16A appliance. They are controlled either over Wi-Fi (802.11n at 2.4GHz) or the Czech-made 868MHz IQRF protocol.



 

The end-client price of such sockets ranges around 99 EUR + VAT. This means we are leaving the world of Asian toys completely, including price-wise, and we will have a closer look at two devices: PowerCable REST and PowerCable MQTT. Today we will show you the PowerCable REST, what you get for your money’s worth and what makes the product unique.

Power socket with a consumption meter

After connecting to a local network and acquiring an IP address, the product provides a simple web interface where the socket can be switched on and off, and at the same time the immediate energy consumption is shown.

 

 

In real time, you can see the immediate values of the electrical voltage (V), current (mA), power input of the connected appliance (W), total energy consumed (Wh) as well as the grid frequency (Hz) and the True Power Factor.

 

In addition to the main administrator, the web interface allows creating new users and assigning them different rights. For example, they can have access to measurements only, or they can also control the switch.

 

 

 

 

Higher models can also schedule on/off switching in time and can be controlled with a mobile app. The PowerCable family does not have an app yet; however, it is already being developed and should be ready in a few months.

Sockets with API for developers

One could argue that the basic PowerCable model cannot actually do very much; however, this is in line with its intended purpose. Rather than ensuring complex automation, such as in your home, it offers the communication API. Therefore, it is really designed to be connected to an existing smart home system – it is not a smart home system just by itself.

 

It is also a good toy for hardware enthusiasts, who might hesitate to introduce an untrusted Asian device in their home network but don’t want to rely on a proprietary manufacturer’s application when their smart home is built e.g. on Fibaro, the community-based Domoticz, or other systems that are open to third-party devices.

 

However, the main target group consists of larger operations that want to tackle automation on their own with a custom solution.

Controlling the socket using a web address

As its name suggests, PowerCable REST offers a simple HTTP REST API. The socket can be switched on by accessing its special web address. In a similar way, consumption data can be obtained in the popular JSON format, or in the somewhat aging XML.

 

 

 

First, let us try the simplest possible API and switch the socket by accessing its URL. To do that, it is necessary to enable the URL API and set a password in the device settings.

 

Be careful, the password is passed among other parameters in the URL and the socket device runs a basic unencrypted HTTP server, so the password travels through the network unencrypted. It is just a basic protection, for example to prevent someone from switching the socket by mistake.

My smart socket device has a URL of pc-rest.netio-products.com and listens on port 22888 and my password is “mops”. Now I can switch the socket by accessing this address:

http://pc-rest.netio-products.com:22888/netio.cgi?pass=mops&output1=1

 

 

As we can see, besides the password there is a second parameter – output1 – that indicates the output to switch. Since the PowerCable only has one output, its number is 1. The parameter has the value of 1, which means to switch the socket on.

 

To switch the socket off, I can simply access:

http://pc-rest.netio-products.com:22888/netio.cgi?pass=mops&output1=0

 

The basic API also supports a command to toggle the current state:

http://pc-rest.netio-products.com:22888/netio.cgi?pass=mops&output1=4

 

Finally, there are two special commands that are hard to find anywhere else. The socket can switch itself on or off for a short time, and then return to the previous state. In this way, it creates a power pulse with a configurable length in milliseconds.

 

 

 

What is this good for? A short switch-off can be used to cold-reboot a computer that has stopped responding. It is as simple as accessing:

http://pc-rest.netio-products.com:22888/netio.cgi?pass=mops&output1=2

 

In the opposite case – the socket is switched off but you need to switch it on for a short time and then switch it back off, all of that in a single step – the URL to access is:

http://pc-rest.netio-products.com:22888/netio.cgi?pass=mops&output1=3

 

This is useful for example to sound a warning horn for 10 seconds in case of an alarm and then switch it off again. The system can simply invoke an address similar to the one above. Another example – a smart system can access this address periodically, such as once per hour, to power up the connected appliance which measures some data, sends them somewhere, and then gets powered off again.

 

By the way, frequent switching should not stress the 230V circuit thanks to the ZCS (Zero Current Switching) technology: the appliance’s power is connected or disconnected when the alternating current waveform crosses the zero level.

JSON API

In addition to the simplest URL API, there is also JSON API and XML API. These APIs use HTTP POST instead of HTTP GET. This means that the commands are sent in the body of the request and not in the URL. In this mode, the smart socket also communicates everything it knows about its state.

 

 

It is also important to note that the PowerCable can only “speak” one API at a time. So, when you enable the JSON API, the simple URL API is automatically disabled, and vice versa.

 

What is JSON?

JSON historically stands for JavaScript Object Notation; however, today it is a de-facto standard for passing data among different technologies. It is not actually tied to Javascript and it is gradually replacing XML.

 

The notation uses curly brackets for key:value pairs and square brackets for arrays. This is an example of an object containing, let’s say, some personal data of a person:

{
   "name": "John",
   "sex": male,
   "weight": 83
}

 

Unlike XML that used to be very popular, the notation is more condensed because there are no start and end tags, such as <name>John</name>. But the biggest advantage is that a JSON parser is now included by default in practically every modern programming language (Javascript, Python etc.) and external libraries are available for the others – including libraries for the tiny Arduino and other microcontrollers where every byte of RAM counts.

JSON data about the socket and its consumption

After this short recap (apologies to seasoned coders), let’s go back to the JSON API on the PowerCable Wi-Fi socket. In this case, JSON is used to transfer electricity consumption data and other information.

 

 

So, I activate JSON instead of the URL API and point my browser to http://pc-rest.netio-products.com:22888/netio.json. First I have to authenticate myself with a username and a password, and then the complete JSON data about the socket appear:

 

{
  "Agent": {
    "Model": "101x",
    "DeviceName": "pc-rest-onlinedemo",
    "MAC": "24:A4:2C:38:D3:8Q",
    "JSONVer": "2.0",
    "Time": "2019-10-12T15:18:41+01:00",
    "Uptime": 10108,
    "Version": "2.2.1",
    "OemID": "5",
    "VendorID": "0",
    "NumOutputs": 1
  },
  "GlobalMeasure": {
    "Voltage": 236,
    "TotalLoad": 34,
    "TotalEnergy": 970,
    "OverallPowerFactor": 0.96,
    "Frequency": 50,
    "EnergyStart": "2019-05-03T11:11:10+01:00"
  },
  "Outputs": [
    {
      "ID": 1,
      "Name": "Power output1",
      "State": 1,
      "Action": 6,
      "Delay": 2000,
      "Current": 149,
      "PowerFactor": 0.96,
      "Energy": 970,
      "Load": 34
    }
  ]
}

 

Since everyone including complete beginners can read JSON by now, let us limit ourselves to a brief summary.

 

At the beginning, there is some basic info about the socket. However, what interests us the most is the GlobalMeasure object and the Outputs array. The GlobalMeasure section shows the total consumption statistics; however, since our PowerCable only has one switched power output, the data also apply to the output. This output is also the only item of the Outputs field, where we can also see the current state (State) and the immediate current (Current) in milliamps.

 

 

So, we can see that the PowerCable controlling our fan is currently reading 236V at 50Hz at its output. A current of 149mA flows through the circuit and the fan is consuming about 34W. Since the last reset, the socket has delivered 970Wh of energy, and as the State variable shows, it is now switched on.

 

This machine-readable data can be read by any automated system that can access this URL, such as an app running on a Raspberry Pi in your home, and further processed – e.g. stored in a database, plotted in a time-series graph, or used to calculate the daily costs of running the fan.

HTTP Basic authentication

As already mentioned, when the JSON API address is accessed with a web browser, a login dialog appears. The login credentials are configured in the device configuration for this API.

 

The web server built in the smart socket device uses the primitive HTTP Basic authentication. So, if we want to retrieve the data e.g. by accessing the address from our own program, we need to include the corresponding header in the HTTP request. For example:

 

Authorization: Basic d3JpdGU6ZGVtbw==
 
 

The sequence of characters in the third part is the username:password pair (including the colon) in Base64 encoding. The web server decodes it back and if the credentials are correct, JSON data is returned.

 

 

Let us try it in practice. First, let’s use the text-based cURL client that is available nowadays as a part of any Linux distribution, including the Raspbian on Raspberry Pi. A Windows version is also available.

Retrieving the data using cURL and the command line

Curl of course supports HTTP Basic authentication; the same JSON as in the browser example can be retrieved with this command:

 

curl --user admin:mops http:// pc-rest.netio-products.com:22888/netio.json

 

Notice the --user parameter followed by login:password pair in the command.

 

Retrieving data and saving as CSV in Python

Let us try it again in a programming language. Maybe we already have a tiny Raspberry Pi Zero W in our local network. It costs next to nothing and runs the Linux-based Raspbian with Python. So, let’s write a script in this language. We will also use the Requests library.

 



 

The simple Python code below connects to the socket, downloads JSON data, parses it to get the immediate voltage, current, power input and consumption values, and stores all these values (separated with semicolons) on a new line in a .csv file. This file can be later opened e.g. in MS Excel.

 

import requests
from datetime import datetime
 
http = requests.get("http://pc-rest.netio-products.com:22888/netio.json", auth=("write", "demo"))
if http.status_code == 200:
    data = http.json()
    my_voltage = data["GlobalMeasure"]["Voltage"]
    my_current = data["Outputs"][0]["Current"]
    my_load = data["Outputs"][0]["Load"]
    my_consumption = data["Outputs"][0]["Energy"]
 
    print("Voltage: " + str(my_voltage) + "V")
    print("Current: " + str(my_current) + "mA")
    print("Load: " + str(my_load) + "W")
    print("Energy consumption: " + str(my_consumption) + "Wh")
 
    with open("data.csv", "a") as my_file:
        my_file.write(datetime.today().strftime("%H:%M:%S %d.%m.%Y") + ";") 
        my_file.write(str(my_voltage) + ";")
        my_file.write(str(my_current) + ";")
        my_file.write(str(my_load) + ";")
        my_file.write(str(my_consumption) + "\n")
        print("Saved into data.csv")
 

When the script runs, a new line is added to the text file. It may look like this:

15:39:45 12.10.2019;237;113;40;952

 

All that is needed now is to run such code periodically to get a running overview of the connected appliance’s state and energy consumption history. When the load is zero, the appliance is probably switched off; when it is at maximum, the appliance is running at peak load.

 

Controlling the socket from the command line

So far, we have been able to read all interesting data from the socket, but how do we control it? With JSON API active, we can no longer use the primitive URL API. Instead, we need to send the commands via HTTP POST, which works like the submitting of a web form.

 

Let us try first with the text-based cURL:

curl --user admin:mops -d "{\"Outputs\": [{\"ID\": 1, \"Action\": 4}]}" http://pc-rest.netio-products.com:22888/netio.json

 

The command above sends data to the socket in the JSON format inside the body of a HTTP POST request (-d parameter). The following structure of values was sent:

 

{
  "Outputs":
  [
    {
      "ID": 1,
      "Action": 4
    }
  ]
}

 

With this, we instruct the PowerCable to perform action no. 4 with the output no. 1. It may sound a little complicated but remember that all Netio boxes have the same API and that the higher models have several sockets. Therefore, we have to identify the socket, even if the model in question only has one.

 

The numeric action codes were already explained together with the URL API, so we know that action 4 means toggling the state – the socket is switched on or off depending on whether it was off or on, respectively.

Controlling the socket from Python

Now let us do the same with Python and the Requests library. We will write a script that toggles the socket state and prints it.

 

 

The code looks like this:

import requests
import json
 
make_me_switch = {
  "Outputs":
  [
    {
      "ID": 1,
      "Action": 4
    }
  ]
}
 
http = requests.post("http://pc-modbus.netio-products.com:22888", auth=("admin", "mops"), data=json.dumps(make_me_switch))
 
if http.status_code == 200:
    data = http.json()
    my_state = data["Outputs"][0]["State"]
    if my_state == 0:
        print("Socket is off")
    elif my_state == 1:
        print("Socket is on")
    

The communication is again very simple. Instead of the GET method, the POST method of the Requests library is called. The plain-text JSON data as that we want to send are specified as a parameter. As soon as the Python script is started, the socket is switched to the other state.

Programmer and integration-friendly socket

The PowerCable REST smart socket device with its universal API is ultimately designed to be easily connected to your own smart home, business or enterprise system, without the need to flash custom firmware or any similar hassles.

 

As mentioned in the introduction, the manufacturer is also working on a mobile app for PowerCable that will make it possible to control the socket remotely even without having any smart home system installed.

 

We also promised to have a closer look at the PowerCable MQTT model that uses the very popular and complex MQTT protocol for Internet of Things. We haven’t yet covered MQTT in our portal so we will keep the PowerCable MQTT for a later time, after having covered in detail what can be done with MQTT and why is it so popular.

 

 

 

 

 

 

 


No comment(s)
Write your comments