However I'm trying to write the same program in Python but am running into problems. I need it in the python language for consistency across the rest of the project since the code will be used for educational resources.
This is what I have so far;

JFI - I find that once I get an Python I2C error 5 - I have to power cycle before re-running any program as the PI seems to "remember" the errored state and you can end up applying a fix but not know that it would have worked had you just power cycled

Pain in the neck but I can fix I2C bugs quicker nowadays that way

Simon

Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

I think its am SMBUS python module bug - its certainly pops up in my ScratchGPIO developement and it took a while before I relasied what was going on so now I correct what I think gives the error - re-save - reboot and then retry

Slow but its faster than not doing it I find

Simon

Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Thank you for this post, this gave me a huge boost getting my own humidity and temperature project working. I have included my own code below which takes the original code and adds a loop which logs to a CSV file on a network share every 5 minutes. The output file is flushed every time it is written and the format works well with excel using a scatter chart.

WOW, what is going on here? I've been miserably trying to use the SMBUS library to interface a CO2 sensor which was never going to work then resorted to installing the PIGPIO library and having to slow the clock speed right down as it wouldn't pause to a delayed clock cycle - a terrible mess although I got it to work.
Is this 'io' and 'fcntl' a software implementation? Sorry I'm learning, if this seems like a basic question but I haven't found any other example like it and SMBus often doesn't work for I2C devices. I nearly abandoned my project and started again using a Arduino to interface the sensors as I was getting so frustrated with the SMBus protocol.

This is kind of an old thread, however, searching the internet today for scripts using smbus and the HTU21D shows no results. Then, I believe posting here the working script may help many others as the script using smbus is very simple, in fact, Dave almost made it! It is just a question of using the hold option.

The hold option is that the sensor will hold the clock low while measuring which means the master must wait for it. Also the sensor ACKs the first read command while the NO_HOLD option uses a NACK that seems to confuse the smbus and produce the error.

Above code is not using the checksum byte ( which is available at data[2]). But if you are not interested on the checksum at all, you can do the block read just changing the number of expected bytes to 2 as shown below.

A bit late, but I ran into a lot of frustration with controlling the HTU21D sensor. So I made a repository which wraps the whole thing up cleanly: https://github.com/MSeal/htu21df_sensor, or from pip htu21df. I found the NACK, or firmware issues in Pi I2C implementation, to constantly cause problems or prevent block reads from (consistently) returning without error. It uses non-holding commands with a direct io connection to the i2c file and sleeps at the maximum sampling interval times. It also checks the checksum based on the reference above to the arduino library's implementation.

Hopefully this simplifies working through this for the next person to stumble into the thread.