8 port Analog sensor board- converting hexadecimal to integer & convert 4-20ma readings to actual current or temperature

Hello,

I have a TCPIP analog data acquisition board with 8 analog inputs.The device is a 4ma - 20ma current loop type. I can connect and request sensor readings with Hercules SETUP Utility w/ hex send and receive.

Here is the request for readings…

80 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 81

And the Data Return (sensors only installed on ports 1,2 and 8)…

01 8b 0f da 29 00 00 00 00 00 00 00 00 00 00 af 0b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 81

Byte 1 = Function (01, sensor read in this case)
Byte 2,3 = Channel 1 L-H, 8b 0f (0xfab = 3979 μA = 3979/1000 = 3.979 ma)
Byte 4,5 = Channel 2 L-H, da 29 (0x29da = 10714 μA = 10714/1000 = 10.712 ma)
Byte 6,7 = Channel 3 L-H
Byte 8,9 = Channel 4 L-H
Byte 10,11 = Channel 5 L-H
Byte 12,13 = Channel 6 L-H
Byte 14,15 = Channel 7 L-H
Byte 16,17 = Channel 8 L-H, af, 0b (0xbaf = 2991 μA = 2991/1000 = 2.991 ma)
Byte 18-31 = Random unused data
Byte 32 = Checksum

From the guide:
Channel 8 corresponds to Byte[16-17](af 0b), 16th byte is low bit, 17th byte is high bit, so the combined hexadecimal is 0x0baf, and the converted decimal is 2991. What needs special attention is that the collected AD value has been converted by module, the unit of 2991 replied by module is μA,and mA is the unit: 2991/1000=2.991ma. The collected current value error of this module is ±0.002ma. The conversion method of channel 1 to channel 7 is same as above example.

The board’s analog ports input current range is 4ma - 20ma .

I need to set up these analog sensors in HA. I plan on using ACS712 current sensors on 5 of the ports and probably NTC 10K thermistors on the remaining three.

Here is the sequence based on my limited knowledge…

  • send a request to the tcpip server:port to retrieve the hexadecimal data
    
  • receive the hexamadecimal data and convert it to an integer
    
  • convert the integer (temperature or current) depending on the sensor type
    
  • send the converted data to the HA sensors sensor.1xx sensor.2xx… sensor.8xx
    
  • further automation, etc.
    

So I will be converting the analog current loop readings to actual current (or temperature) from the sensor?

I am stuck on the best way to do this. I think I have to use Templates but will one template do all this?

Thanks in advance!

Maybe given the complex conversion an external python script will be easier to implement.

If I understood it correctly, you can setup a “dummy” sensor and periodically call the script that updates the value via the hass object.

Thanks,

The Python scripts are looking like the best option. Not really complicated what I am doing but I can’t find too much HA info on HEX parsing except for the RGB light HEX color code conversions some people need.

Well, I ended up writing a python script that …

  • Connects to the ADC board tcpip server and retrieves the sensor data
  • Converts the return data from Base 16 HEX ==> Base 16 INT ==> Base 10 INT string
  • Strips out the bytes containing sensor data and puts them in the right order
  • Returns the actual sensor readings for each analog port as an INT in microamps
  • Converts the microamp readings to milliamps

Next step, I have to look at the actual sensors and their performance curves and write the code to convert from the sensor microamp reading to curves for Temperature, Current, etc. for each sensor type and model I use.

I am really getting a education in 4-20ma current loop and learned that I will need to convert between the Voltage sensors I am using to the 4-20ma current loop; I already ordered the conversion boards from ebay, from China of course. The primary reason to use the 4-20ma current loop is that very long wiring runs can be made that have no voltage drop issues (current is equal throughout the circuit) and superior resistance to inteference vs the Voltage type.

If anyone’s interested this code can be used where a HEX read and return must be made and the data converted for use on the HA backend / frontend (or wherever else). This type of scenario prevalent in the industrial sector where the 4-20ma current loop sensor circuit is used. I am not a bona-fide programmer, more of a hack, but luckily the code is working perfectly so far. I will probably make some code adjustments as I learn more about python, and I do need to add something for the program to do if something goes wrong.

Also, I need to find out more about HA and python scripts so I can incorporate into my HA setup.

Home Automation, what an adventure, but I really enjoy the experience and outcome.

Updated the python script referenced in last post…

-added conversion of 4-20ma value to actual current value for current sensors ACS712 5A
-added conversion of 4-20ma value to actual temperature for temperature sensors NTC 10K Thermistor
-added paho mqtt publishing to Mosquitto MQTT server for all 8 sensors
-added sensors to HA config & groups

-still need error handling
-still need to tune sensor data conversions
-automations (temperature warnings, pump shutdown / startup etc.)

Script runs fine in terminal on my Debian HA server.