Sample Python code for reading the

HDC1000EVM Temperature and Humidity Sensor

HDC1000EVM sensor image

import serial

import binascii

#sample program to read Texas Instruments HDC1000EVM

#initialization and open the port

#possible timeout values:

# 1. None: wait forever, block call

# 2. 0: non-blocking mode, return immediately

# 3. x, x is bigger than 0, float allowed, timeout block call

ser= serial.Serial()

# Use which ever serial port works.

# the /dev/serial/by-path can change so do an ls to see what the

# device logged as

#ser.port= “/dev/ttyUSB0”

#ser.port= “COM4:”

#ser.port= “/dev/ttyACM1”

ser.port= “/dev/serial/by-path/platform-bcm2708_usb-usb-0:1.3:1.0”

ser.baudrate= 115200

ser.bytesize= serial.EIGHTBITS
#number of bits per bytes

ser.parity= serial.PARITY_NONE
#set parity check: no parity

ser.stopbits= serial.STOPBITS_ONE
#number of stop bits

#ser.timeout= None #block read

#ser.timeout= 1 #non-block read

ser.timeout= 2 #timeout block read

ser.xonxoff= False
#disable software flow control

ser.rtscts= False
#disable hardware (RTS/CTS) flow control

ser.dsrdtr= False
#disable hardware (DSR/DTR) flow control

ser.writeTimeout= 2 #timeout for write

try:

ser.open()

except Exception as e:

print(“error openserial port: “ + str(e))

exit()

if ser.isOpen():

try:

ser.flushInput() #flush input buffer, discarding all its contents

ser.flushOutput()#flush output buffer, aborting current output

#and discard all that is in buffer

#write data

ser.write(serial.to_bytes([0x4C,0x12,0x01,0x00,0x03,0x40,0xFB,0x02,0x7a]))

response = ser.readline()

print(“read Serial Number MSB = : 0x”str(binascii.hexlify(response[7])) + str(binascii.hexlify(response[8])))

ser.write(serial.to_bytes([0x4C,0x12,0x01,0x00,0x03,0x40,0xFc,0x02,0x11]))

response = ser.readline()

print(“read Serial Number MID = : 0x”str(binascii.hexlify(response[7]))
+
str(binascii.hexlify(response[8])))

ser.write(serial.to_bytes([0x4C,0x12,0x01,0x00,0x03,0x40,0xFd,0x02,0x04]))

response = ser.readline()

print(“read Serial Number LSB = : 0x”str(binascii.hexlify(response[7]))
+
str(binascii.hexlify(response[8])))

ser.write(serial.to_bytes([0x4C,0x12,0x01,0x00,0x03,0x40,0xFe,0x02,0x3b]))

response = ser.readline()

print(“read Manufacturer = :”str(response[7]) + str(response[8]))

ser.write(serial.to_bytes([0x4C,0x12,0x01,0x00,0x03,0x40,0xFF,0x02,0x2e]))

response = ser.readline()

print(“read Device ID = : 0x”str(binascii.hexlify(response[7])) + str(binascii.hexlify(response[8])))

ser.write(serial.to_bytes([0x33]))

numOfLines = 0

while True:

response = ser.readline()

if (str(response).find(“,”) == -1):

print(“start data: “ + str(response))

else:

print(“Temperature: “str((“{0:.2f}”.format(((float(int((str(response).split(‘,’)[0]),16))/float(65536))
* 165) -40)))+
u”\u00B0″ + “C”)

print(“Temperature: “ +
str((“{0:.2f}”.format((((((float(int((str(response).split(‘,’)[0]),16))/float(65536))
* 165) -40) * 9) / 5) + 32)))+
u”\u00B0″ + “F”)

print(“Relative Humidity: “ +
str((“{0:.2f}”.format((float(int((str(response).split(‘,’)[1]),16))/float(65536))
* 100)))+
“%”)

numOfLines = numOfLines + 1

if (numOfLines > 10):

#ser.write(bytes(44))

ser.write(serial.to_bytes([0x34]))

response = ser.readline()

print(“read data: “ + str(response))

break

ser.close()

except Exception as e1:

print(“error ” + str(e1))

#ser.write(bytes(44))

ser.write(serial.to_bytes([0x34]))

response = ser.readline()

print(“last data: “ + str(response))

ser.close()

else:

print(“cannot open serial port “)

written by

2 comments

  1. said…

    fantastic issues altogether, you just won a new reader.
    What could you suggest about your put up that you made a few
    days ago? Any certain?

    • said…

      My apologies. I do not understand the question. For information, I am redoing the entire coop control system and will document it soon. The HDC1000EVM died and I am replacing it with a different module. The BME280 which reads temperature, humidity and barometric pressure. If you could restate the question, I will try and answer. I understand English is probably not your main language but that is fine, use whichever suits you and I will try and translate. This is one reason the Internet id great. It joins people from all over the globe.

Leave a Reply

Your email address will not be published. Required fields are marked *