Thanks to RS485, we can connect and read/write data from many devices. Industrial Raspberry Pi shield named MiniIOEx has RS485 ınterface for Raspberry Pi and MiniIOEx is low-cost IO solution for Raspberry Pi. So, Raspberry Pi can behave as master/slave via RS485. Besides, with Raspberry Pi and MiniIOEx, this data can be sent via 3G or Ethernet / Wireless. Although this kind of process seems easy, the PLC or embedded PCs cost quite a lot for this basic process.
Used devices for this part:
- Raspberry Pi 3
- MiniIOEx
- Entes MPBR63 Energy Analyzer
- Additional computer to monitor read/write data [this chapter can be passed]
We will use Python library to access Energy Analyzer. So, you should install these library before :
$sudo apt-get install python-serial
$sudo pip install -U pymodbus
At below, there is the topology that actually we use as the system interface. There is also a computer outside the topology. The purpose of our computer use is to show how we receive this data because we have exchanged data with the MODBUS RTU protocol over the RS485 physical serial path.
In the above system, the computer behaves as master / slave, the MiniIOEx is master and the analyzer is slave. A and Bterminals of MiniIOEx RS485 are all short-circuited in the system. "Modbus Master" that Computer Serial Port Program and "Terminal v1.9b" programs installed on the computer, we can see the reference codes you need to go to read data from the Entes Analyzer. Of course, before we do this, we need to know which registers the analyzer has which information. We can also extract this from the "Data Mapping" document found on the analyzer web page of the Entes Energy Analyzer.
As you can see on the tablade there is cosq values 19, 20, 21in the register. (It appears that the first value starts from 0 in the Register table.) Since the resolution for cosq value is 1000, it must be divided by 1000 so that the value coming from real cosq value.
For example, when the value of 999 is reached, this value should be cosq = 999/1000 = 0.99. Since no current / voltage terminals are connected to the energy analyzer, there is no phase difference between them and therefore we need to see cosq = 1. Other information (voltage, current, etc.) can also be taken using this tissue when a field application is made.
When we connect the analyzer to the terminals of the computer and MiniIOEx terminal, we can proceed to read data.
First we need to write basic information such as node slave address, how many registers we want to read in the MODBUS MASTER program installed on the computer. The following screen shot also provides sample information:
The computer sends the query: 01 03 00 00 1E C5 C2. According to this query, the Analyzer gave a long answer. Rx, the query sent by the computer; Tx is the answer from the analyzer. Data can not be retrieved without inquiry on RS485. These long answers are actually parameters such as Voltage / Current / Frequency / Power factor measured by the Analyzer. In order to use Cosq from these parameters, let us first examine the question sent by the Analyzer. The analyzer sent us the 03 E8 replies in sequence. This answer is actually HEX 0x3E8. In the decimal number system, it is expressed as "1000". In other words, the result of the query we made is 1000/1000 = 1 in the cosq register. If the value of 999 (0x3E7) came, it would be 999/1000 = 0.99. These operations are the most basic events that take place in the RS485 serial path. We make these operations simple by using protocols. If we want to make a computer-like query from Raspberry Pi, we can use the following code.
The following code appears to have the "python serial" library. You can run this library by typing the following command at the terminal.
$sudo apt-get install python-serial
After uploading the library, we can send the related query by running the following program:
import os,time
import serial
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1 )
ser.write(serial.to_bytes([0x01,0x03,0x00,0x00,0x00,0x1E,0xC5,0xC2]))
In order to receive the data from the analyzer, we have mentioned above that we need to send the relevant parameter to the analyzer: 01 03 00 00 1E C5 C2 When we send this parameter through the serial port to the analyzer, it returns us the parameters in HEX again. We can see that the values again on "Terminal v1.9b" computer program.
In the picture above, the query (01 03 00 00 1E C5 C2) to the Analyzer and the query from the analyzer and the answers from the analyzer according to this query. In this question, it is not really complicated. As an example,
"01" defines Node address;
"1E" shows how many registers are defined.
The first query in the RS485 protocol then takes place in its reply. Both write and read process via RS485 serial port at the same time can not be querried synchronously. This might be fault.
When we run this basic code, the energy analyzer will give a result like above. But when we write this code with ModbusRTU protocol, understanding of parameters, Check-Sum calculations, CRC calculations are more comfortable. It can be automatically done.
There are many open source ModbusRTU libraries running on Raspberry Pi. There are many documents related to this subject on the internet. ModbusRTU is a protocol that you can create if you wish but you do not need to "reinvent the wheel" here, which is also true in most areas like software. In this document "PYMODBUS" library is used. You can download the PYMODBUS library by entering the command at the following terminal.
$sudo pip install -U pymodbus
When the "PYMODBUS" library is installed, we can use the ModbusRTU protocol to get the parameters received from the Analyzer from the field. The analyzer used in this example has no voltage or current source connected. Therefore, we can only query "cosq" data. Having the "cosq" data means that other data can be easily accessed. In order to receive other information, it is only necessary to know the correct "register" addresses.
import serial
import pymodbusfrom pymodbus.pdu
import ModbusRequestfrom pymodbus.client.sync
import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer
import time
client = ModbusClient(method = 'rtu', port = '/dev/ttyS0',baudrate = 9600,timeout = 1,parity = 'N')
client.connect()
while 1:
try:
result = client.read_holding_registers(0x00,40,unit = 0x01)
#print(result.registers)
print("cosqL1 : {}, cosqL2 : {}, cosqL3 : {}".format(result.registers[19]/1000.0,result.registers[20]/1000.0,result.registers[21]/1000.0))
except:
pass
time.sleep(1)
Usage of read_holding_registers in python program:
result = client.read_holding_registers(0x00,40,unit = 0x01)
In the above software example, the registers between 0 and 40 registers on the analyzer are being queried. The register table has been given above and we have seen that there are many parameters between these registers. Since the analyzer is the communication node "1", we add "0x01" or only "1" ID query parameter to the parameter of "client.read_holding_registers". That is, the analyzer with unit ID:1 actually wants to send the parameters between 0. and 40. Registers. These parameters include voltage, current, cosq, energy. If there is no analyzer with this ID number, the system will wait in timeout.
The output of the code is the value of the cosq values in the Active Power. If information such as voltage / current is to be received, inquiry should be made from the relevant Register table. We can also calculate COSQ through other informations from the Analyzer.
Basic Cosq calculation:
- P = VIcosq == cosq=P/VI
- Sample#1 P = 3.4kW, V=400V, I=10A
- Cosq = 3400W / (400Vx10A)
- Cosq = 0.85
Here we can calculate CosQ as 0.85. In general, we always want CosQ to be close to 1. Since we can get the values angle of Voltage, Current, Active Power etc. from the analyzer, we can calculate cosq and accordingly angle calculations. If the system of cosq is not below 0.98, fines shall be paid.
You can also see the energy consumed and the energy generated from the analyzer. If you have a solar power plant, you can also get Spent and Generated Energy information and record it on a daily / weekly / monthly basis and send your data to your server without any internet service provider by using 3G module on MiniIOEx-3G. When there is energy production, the sign of the current will change. It should be noted.
SUMMARYMeasuring energy production and monitoring energy is a very important task. Doing this in expensive and log-on PLCs is an unnecessary effort in today's technology. With Raspberry Pi, these processes can be done very quickly and effectively. Additional, energy values can be kept on the SD card over many years. This data can be written to Firebase / AWS / Azure or it can be sent to another SQL server. MiniIOEx, which provides RS485 interface for Raspberry Pi, is a very cheap and powerful solution. It has also 3G option and it can be easily used in industrial environments with 5V or 24V supply.
If you have any question about it, please let me know.
Note: You can find actual code sample with this link: https://github.com/pe2a/miniIOEx3G/
Comments
Please log in or sign up to comment.