I'm almost to the point of...

...so any help would be gratefully received.
This is the product in question.
https://www.sparkfun.com/products/12064 (has a link to the datasheet)
I've got a working C program for it here which uses GordonDrogon's wiringPi library;
Code: Select all
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "wiringPi.h"
#include "wiringPiI2C.h"
#define ADDR 0x40
// cTemp:
// Convert sensor reading into temperature.
// See page 14 of the datasheet
double cTemp (int sensorTemp)
{
double tSensorTemp = sensorTemp / 65536.0 ;
return -46.85 + (175.72 * tSensorTemp) ;
}
// cHumid:
// Convert sensor reading into humidity.
// See page 15 of the datasheet
double cHumid (int sensorHumid)
{
double tSensorHumid = sensorHumid / 65536.0 ;
return -6.0 + (125.0 * tSensorHumid) ;
}
int main (void)
{
int fd ;
unsigned int x ;
unsigned char buf [4] ;
unsigned int temp, humid ;
wiringPiSetup () ;
if ((fd = wiringPiI2CSetup (ADDR)) < 0)
{
fprintf (stderr, "Unable to open I2C device: %s\n", strerror (errno)) ;
exit (-1) ;
}
// Temperature
wiringPiI2CWrite (fd, 0xF3) ;
delay (100) ;
x = read (fd, buf, 3) ;
if (x != 3)
printf ("%d: %02X %02X %02X\n", x, buf[0], buf[1], buf[2]) ;
temp = (buf [0] << 8 | buf [1]) & 0xFFFC ;
printf ("%4d %5.1fC\n", temp, cTemp (temp)) ;
// Yoomidity
wiringPiI2CWrite (fd, 0xF5) ;
delay (100) ;
x = read (fd, buf, 3) ;
if (x != 3)
printf ("%d: %02X %02X %02X\n", x, buf[0], buf[1], buf[2]) ;
humid = (buf [0] << 8 | buf [1]) & 0xFFFC ;
printf ("%4d %5.2f %%rh\n", humid, cHumid (humid)) ;
return 0 ;
}
This is what I have so far;
Code: Select all
#!/usr/bin/python
import time
import smbus
msleep = lambda x: time.sleep(x/1000.0)
ADDR = 0x40
i2c = smbus.SMBus(1)
CMD_READ_TEMP_HOLD = 0xe3
CMD_READ_HUM_HOLD = 0xe5
CMD_READ_TEMP_NOHOLD = 0xf3
CMD_READ_HUM_NOHOLD = 0xf5
CMD_WRITE_USER_REG = 0xe6
CMD_READ_USER_REG = 0xe7
CMD_SOFT_RESET= 0xfe
i2c.write_byte(ADDR, CMD_SOFT_RESET)
msleep(100)
i2c.write_byte(ADDR, CMD_READ_TEMP_NOHOLD)
msleep(100)
data = i2c.read_i2c_block_data(ADDR, CMD_READ_TEMP_NOHOLD, 3)
msleep(100)
I am aware of some existing libraries for this sensor, but they all exhibit this same issue for me. These include;
https://github.com/jwineinger/quick2wir ... /htu21d.py
https://github.com/randymxj/Adafruit-Ra ... _HTU21D.py
Can anyone see what is going wrong with the python code?