diff --git a/Adafruit_HDC1008/Adafruit_HDC1008.py b/Adafruit_HDC1008/Adafruit_HDC1008.py new file mode 100644 index 00000000..7bfbcd80 --- /dev/null +++ b/Adafruit_HDC1008/Adafruit_HDC1008.py @@ -0,0 +1,111 @@ +#!/usr/bin/python3 + +""" +HDC1008 - A Python class for querying a HDC1008 +Temperature and Humidity Sensor created by Adafruit #2635 using the +Adafruit_I2C Python library. + +The HDC1008 class code was originally created and shared on Stackoverflow +at the following web address + +https://stackoverflow.com/questions/32656722/python-can-i-read-two-bytes-in-one-transaction-with-the-smbus-module + +It has been reproduced and modified here under the terms of the Creative Commons +Attribution Share Alike licence as explained on Stack Exchange + +https://stackexchange.com/legal + +Credit goes to the following people for the creation of this class + +kfricke (https://github.com/kfricke) + Work on micropython-hdc1008, which informed initial version of this class +(16-06-2015) + +Walter (https://stackoverflow.com/users/5351300/walter) + Incomplete creation of HDC1008, shared on stackoverflow and, based on work by kfricke +(18-09-2015) + +Romke (https://stackoverflow.com/users/5413479/romke) + Working version of HDC1008 on stackoverflow, based on work of Walter +(6-10-2015) + +Damien Clark - Porting of Romke's version from Stackoverflow to Adafruit's I2C library +(03-01-2016) + +It is an initial working implementation that can read temperature and humidity measurements + +_TODO_ +. Implement fetching of temperature and humidity in single i2c transaction +. Implement precision features for the measurements +. Implement features to control the heater + +""" + +import time +from Adafruit_I2C import Adafruit_I2C + +# =========================================================================== +# HDC1008 Class +# =========================================================================== + +class HDC1008: + + #Registers + REG_TEMP = 0 + REG_HUMID = 1 + REG_CONFIG = 2 + + #Default Bus + I2C_BUS = 1 + + # Configuration bits for config register + CFG_RST = 1<<15 + CFG_MODE_SINGLE = 0 << 12 + CFG_MODE_BOTH = 1 << 12 + + #Default I2C address for HDC1008 + ADDRESS = 0x40 + + # Constructor + def __init__(self, address=ADDRESS): + self.i2c = Adafruit_I2C(address) + self.address = address + + def readTemperature(self): + # configure the HDC1008 for one reading + config = 0 + config |= self.CFG_MODE_SINGLE + self.i2c.write16(self.REG_CONFIG, config) + + # now let's ask for a temperature reading + self.i2c.writeRaw8(self.REG_TEMP) + time.sleep(0.015) + + #get the reading back from the thing + raw = self.i2c.readRaw8() + raw = (raw<<8) + self.i2c.readRaw8() + + #use TI's formula to turn it into people numbers + temperature = (raw/65536.0)* 165 - 40 + + + #convert temp to farenheid + #temperature = temperature * (9.0/5.0) + 32 + return temperature + + def readHumidity(self): + # configure the HDC1008 for one reading + config = 0 + config |= self.CFG_MODE_SINGLE + self.i2c.write16(self.REG_CONFIG, config) + + # now let's ask for a humidity reading + self.i2c.writeRaw8(self.REG_HUMID) + time.sleep(0.015) + + #get the reading back from the thing + raw = self.i2c.readRaw8() + raw = (raw<<8) + self.i2c.readRaw8() + + hum=(raw/(2.0**16))*100 + return hum diff --git a/Adafruit_HDC1008/Adafruit_I2C.py b/Adafruit_HDC1008/Adafruit_I2C.py new file mode 120000 index 00000000..77f06164 --- /dev/null +++ b/Adafruit_HDC1008/Adafruit_I2C.py @@ -0,0 +1 @@ +../Adafruit_I2C/Adafruit_I2C.py \ No newline at end of file diff --git a/Adafruit_HDC1008/example.py b/Adafruit_HDC1008/example.py new file mode 100644 index 00000000..8c76f499 --- /dev/null +++ b/Adafruit_HDC1008/example.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 + +from Adafruit_HDC1008 import HDC1008 + +# =========================================================================== +# Example Code +# =========================================================================== + +#Get sensor object at it's default i2c address +Sensor=HDC1008() + +# Print the temperature +print(Sensor.readTemperature()) + +# Print the humidity +print(Sensor.readHumidity()) diff --git a/Adafruit_I2C/Adafruit_I2C.py b/Adafruit_I2C/Adafruit_I2C.py index 2b24b67f..a131ef9c 100755 --- a/Adafruit_I2C/Adafruit_I2C.py +++ b/Adafruit_I2C/Adafruit_I2C.py @@ -54,7 +54,7 @@ def reverseByteOrder(self, data): return val def errMsg(self): - print "Error accessing 0x%02X: Check your I2C address" % self.address + print("Error accessing 0x%02X: Check your I2C address" % self.address) return -1 def write8(self, reg, value): @@ -62,8 +62,8 @@ def write8(self, reg, value): try: self.bus.write_byte_data(self.address, reg, value) if self.debug: - print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg) - except IOError, err: + print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)) + except (IOError, err): return self.errMsg() def write16(self, reg, value): @@ -71,9 +71,9 @@ def write16(self, reg, value): try: self.bus.write_word_data(self.address, reg, value) if self.debug: - print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % + print("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % (value, reg, reg+1)) - except IOError, err: + except (IOError, err): return self.errMsg() def writeRaw8(self, value): @@ -81,18 +81,18 @@ def writeRaw8(self, value): try: self.bus.write_byte(self.address, value) if self.debug: - print "I2C: Wrote 0x%02X" % value - except IOError, err: + print("I2C: Wrote 0x%02X" % value) + except (IOError, err): return self.errMsg() def writeList(self, reg, list): "Writes an array of bytes using I2C format" try: if self.debug: - print "I2C: Writing list to register 0x%02X:" % reg - print list + print("I2C: Writing list to register 0x%02X:" % reg) + print(list) self.bus.write_i2c_block_data(self.address, reg, list) - except IOError, err: + except (IOError, err): return self.errMsg() def readList(self, reg, length): @@ -100,11 +100,21 @@ def readList(self, reg, length): try: results = self.bus.read_i2c_block_data(self.address, reg, length) if self.debug: - print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % + print("I2C: Device 0x%02X returned the following from reg 0x%02X" % (self.address, reg)) - print results + print(results) return results - except IOError, err: + except (IOError, err): + return self.errMsg() + + def readRaw8(self): + "Reads an 8-bit value from the I2C device" + try: + result = self.bus.read_byte(self.address) + if self.debug: + print("I2C: Read 0x%02X" % result) + return result + except (IOError, err): return self.errMsg() def readU8(self, reg): @@ -112,10 +122,10 @@ def readU8(self, reg): try: result = self.bus.read_byte_data(self.address, reg) if self.debug: - print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)) return result - except IOError, err: + except (IOError, err): return self.errMsg() def readS8(self, reg): @@ -124,10 +134,10 @@ def readS8(self, reg): result = self.bus.read_byte_data(self.address, reg) if result > 127: result -= 256 if self.debug: - print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % + print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)) return result - except IOError, err: + except (IOError, err): return self.errMsg() def readU16(self, reg, little_endian=True): @@ -139,9 +149,9 @@ def readU16(self, reg, little_endian=True): if not little_endian: result = ((result << 8) & 0xFF00) + (result >> 8) if (self.debug): - print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) + print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)) return result - except IOError, err: + except (IOError, err): return self.errMsg() def readS16(self, reg, little_endian=True): @@ -150,12 +160,12 @@ def readS16(self, reg, little_endian=True): result = self.readU16(reg,little_endian) if result > 32767: result -= 65536 return result - except IOError, err: + except (IOError, err): return self.errMsg() if __name__ == '__main__': try: bus = Adafruit_I2C(address=0) - print "Default I2C bus is accessible" + print("Default I2C bus is accessible") except: - print "Error accessing default I2C bus" + print("Error accessing default I2C bus")