Single Phase Energy Monitoring using Armtronix Wifi RS485 LoRa (IA013 D) and PZEM-016

Single Phase Energy Monitoring using Armtronix Wifi RS485 LoRa (IA013 D) and PZEM-016

In this blog we would like to show you Energy monitoring Using Peacefair’s single phase RS485 energy meter PZEM-016 and Armtronix (IA013 Type – D) device. In this setup we are reading PZEM-016 measurement values using STM32 via RS485(modbus) and publishing measurement data via LoRa (here LoRa is interfacing with STM32 via SPI), similarly STM32 is sending the data via UART to ESP32, hence ESP32 will publish the data via http or mqtt.

Components Required:

NOTE : In this blog we are using IA013-Type D device.

Loading the firmware on STM32:

To load the firmware on STM32 you require Arduino IDE installed with STM32 core by rogerclarkmelbourne and the following libraries and Atmtronix IA013 code

you can install the above libraries form Arduino library manager by clicking ctrl+shift+I

After all libraries are installed, make sure you have to connect the right jumpers on header J5 and J3 to upload the code on STM32.

ie, pin 3 (PA10_RX) connected to pin 4 (RXD) and pin 5 (PA9_TX) to pin 6 (TXD)

and connect pin 1(VCC) with pin 3(BOOT0) and pin 4(BOOT1) with pin 6 (GROUND) for booting STM32 on programming mode.


So now download the Armtronix IA013 code from github using the below link

And open the ttn-abp_pmz_016_STM32 code, you have to add your ttn network keys(Network Session Key, Device Address, Device Address)

ttn-abp device details

Now we can upload the code with the following upload setting and proper usb port.

NOTE: before uploading the code press Reset button.

Once the code is sucessfully uploaded place the MCU BOOT header(J3) jumpers as following

connect pin 3 (BOOT0) with pin 5(GND) and connect pin 4 (BOOT1) with pin 6 (GND)


Thus, STM32 firmware is uploaded.

We are reading the following Input registors:


Loading firmware on ESP32

 we have to install the following libraries in arduino ide

So to upload the code (Wifi_ESP32_v2.ino) on Esp32 of IA013 you need a micro – usb cable (As IA013 has in built USB to UART converter) and Arduino ide installed . and make sure the programming jumpers are placed properly on header J5

ie Connect Pin1 (TXDE) with Pin2 (TXD) and Pin7 (RXDE) and Pin8 (RXD) as shown in the below fig.


once you are done with adding all the libraries and code is compiled, you can upload the code by selecting proper port.

After all the coding part is over, we have to establish communication between ESP32 and STM32, hence you have to short pin 1 (TXDE) with pin 3 (PA10_RX) and pin 5 (PA9_TX) with pin 7(RXDE) on header J5.


make the connection as per the below wiring diagram shown below


Configurartion of ESP32:

Once you are done with the proper connection turn on the main supply and when the IA013 is turned on for first time you have to configure it using your wifi ssid password. so here IA013 will a host will be hosting as an access point (Eg : Armtronix-94-4f-54 where alphanumeric is mac last six digits of mac address of IA013), once you are connected to the AP enter in your browser, A webpage will be loaded as shown below. there are two IOT options available ie HTTP and MQTT. To operate IA013 on HTTP you have to just fill Ssid and password (if exist) and select http button and click on submit and for mqtt you have to fill all the credentials ie ssid, password(if exist), mqtt broker, mqtt username(if exist), mqtt password(if exist), Subscribe topic, Publish topic and click on submit.


Mqtt topics Example:

If the Device MQTT publish topic is “MP” and subscribe topic is “MS”

Then the following topics you have to subscribe to get the data

  • “MP/Voltage” for voltage
  • “MP/Current” for current
  • “MP/Power” for Power in watts
  • “MP/Wh” for energy in Wh
  • “MP/Frequency” for frequency in Hz
  • “MP/PF” for power factor
  • “MP” you will receive all data in a single String.

Now all the setup is done and we are receving date on ttn console and via mqtt

Date receiving via IA013 , payload formatting code is given below

function Decoder(bytes, port) {
  //var result = ""; for (var i = 0; i < bytes.length; i++) { result += String.fromCharCode(parseInt(bytes[i])); } return { Temperature: result, };
  var voltage =""; voltage = ((bytes[0] << 8) + bytes[1])/10.0; 
  var current =""; current = (((bytes[4]) << 24) + ((bytes[5]) << 16)+((bytes[2]) << 8)+((bytes[3])))/1000.0;
  var power = ""; power =((bytes[8] << 24)+ (bytes[9] << 16)+ (bytes[6] << 8)+ bytes[7])/10.0;
  var energy = ""; energy = ((bytes[12] << 24)+ (bytes[13] << 16)+ (bytes[10] << 8)+ bytes[11]);
  var frequency = ""; frequency = ((bytes[14] << 8)+ bytes[15])/10.0;
  var pf = ""; pf = ((bytes[16] << 8)+ bytes[17])/100.0;
  return { "Voltage": voltage,"Current":current, "Power": power,"Energy": energy,"Frequency" : frequency,"Powerfactor" : pf};

And even we are receiving data via mqtt and we have configured it with openhab2

Screenshot from 2019-07-05 18-38-53

here is our another blog to know more, how we have done the configuration on openhab2 link

Leave a Reply