Simple Rh, T and p sensor with UART communication

The Pressure, temperature and humidity sensor based on MS5637 HDC1080 originally (Rev. 2) operates via RS485 interface and multidrop HDLC-like protocol.  I decided to simplify this for use with Raspberry Pi, arduion or any other mass platforms. First, I took away the RS485 transceiver and second, I simplified the communication.

First adaptation was easy:

Remove RS485 driver and connect Rx/Tx to A/B

With above change, the line A operates as UART Tx and line B as UART Rx.

The software part is on my GitHub repository.

Commands

The communication is simple. Attach some serial port to A and B lines and type  commands with the following syntax:

@<addr>:<device> <readcmd>

where….

  • <addr> is 0 to 7 and represents binary combination of the three address bits which are set with the resistors on lines PA6, PA7 and PB1.
  • <device> is HDC1080 or MS5637 and the
  • <readcmd> depends on the device, see below.

 

The MS5637 read commands:

  • p for pressure
  • t for temperature
  • c for cal[0]
  • d for cal[0]
  • e for cal[0]
  • f for cal[0]
  • g for cal[0]
  • h for cal[0]
  • i for cal[0]
  • j for cal[0]
  • x for raw ADC1
  • y for raw ADC2

Commands for HDC1080 are:

 

  • h for humidity
  • t for temperature
  • v for battery status bit

 

The letters for readout command can be repeated in any order. Maximum length for readout string is 32.

Example to readout temperature and humidity from hdc1080 on module with address 7:

 

@7:hdc1080 th

Air pressure and aitr temperature from MS5637 from module with same address can be read by:

@7:MS5637 pt

The return value is in the following format:

#%d:%s

where integer %d is the device address and string %s is readout. Pressure, temperature and humidity readouts are floating point with format:

%c=%3.2f

where char %c is either p, t or h for pressure, temperature or humidity, respectively.

Calibration coefficients have format %c=%04X where char %c is equal to char in <readcmd>.

The raw ADC readouts have format %c=%08X, where char %c is either x or y.

 

Example:

@7:hdc1080 h

will return

#7:h=25.76

 

Use with the Raspberry pi

The simplest way is to hook the Rx/Tx to the UART pins on R.Pi and try with the following python script (save it to the file rhtp.py):

import serial
import time
 
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1.0)
port.write("@7:hdc1080 h\n")
time.sleep(0.5)   #Pause time in seconds
x=port.readline()
print x

The response from above is:

pi:~/serialtest $ python rhtp.py
#7:h=25.61

pi:~/serialtest $

 

More advanced script with some run time parameters could be (save it to demo.py):

 

import serial, sys
 
 
if len(sys.argv) == 4# 1 + 3
  (cmd, adr, dev, par) = sys.argv
  port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
  port.write("@"+adr+":"+dev+" "+par+"\n")
  rcv = port.read(10)
  (x,t) = rcv.split("=")
  print (t)
else:
  print ("Error")

 

 

and run with

 python demo.py 7 hdc1080 h

 

output from this is only the number:

26.01

 

 

Comments are closed.