Trouble reading sensors


#1

Hi,

I recently bought my GrovePI. My led test worked, but using sensors Ultrasonic en DHT only gives IOerrors. I have updated the firmware as described in this forum. I have copied a part of the grovepi.py script and added some code to see what is going wrong. The problem is in function read_i2c_block. This is where the IOerror appears. I also have test i2cdetect –y 1 to see if the board is connected (led test also works).

My script:
import smbus
import time
import math
import RPi.GPIO as GPIO
import struct

rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)

I2C Address of Arduino

address = 0x04

Command Format

digitalRead() command format header

dRead_cmd = [1]

digitalWrite() command format header

dWrite_cmd = [2]

analogRead() command format header

aRead_cmd = [3]

analogWrite() command format header

aWrite_cmd = [4]

pinMode() command format header

pMode_cmd = [5]

Ultrasonic read

uRead_cmd = [7]

Get firmware version

version_cmd = [8]

Accelerometer (+/- 1.5g) read

acc_xyz_cmd = [20]

RTC get time

rtc_getTime_cmd = [30]

DHT Pro sensor temperature

dht_temp_cmd = [40]

This allows us to be more specific about which commands contain unused bytes

unused = 0

Function declarations of the various functions used for encoding and sending

data from RPi to Arduino

Write I2C block

def write_i2c_block(address, block):
try:
return bus.write_i2c_block_data(address, 1, block)
except IOError:
print "IOError #1"
return -1

Read I2C byte

def read_i2c_byte(address):
try:
return bus.read_byte(address)
except IOError:
print "IOError #2"
return -1

Read I2C block

def read_i2c_block(address):
try:
return bus.read_i2c_block_data(address, 1)
except IOError:
print "IOError #3"
return -1

Read analog value from Pin

def analogRead(pin):
bus.write_i2c_block_data(address, 1, aRead_cmd + [pin, unused, unused])
time.sleep(.1)
bus.read_byte(address)
number = bus.read_i2c_block_data(address, 1)
return number[1] * 256 + number[2]

Read value from Grove Ultrasonic

def ultrasonicRead(pin):

    write_i2c_block(address, uRead_cmd + [pin, unused, unused])
    time.sleep(.2)
    try:
            read_i2c_byte(address)
            number = read_i2c_block(address)
            return (number[1] * 256 + number[2])
    except (TypeError, IndexError):
            return -1

Read and return temperature and humidity from Grove DHT Pro

def dht(pin, module_type):
write_i2c_block(address, dht_temp_cmd + [pin, module_type, unused])

    # Delay necessary for proper reading fron DHT sensor
    time.sleep(.6)
    try:
            read_i2c_byte(address)
            number = read_i2c_block(address)
            if number == -1:
                    return -1
    except (TypeError, IndexError):
            return -1
    # data returned in IEEE format as a float in 4 bytes
    f = 0
    # data is reversed
    for element in reversed(number[1:5]):
            # Converted to hex
            hex_val = hex(element)
            #print hex_val
            try:
                    h_val = hex_val[2] + hex_val[3]
            except IndexError:
                    h_val = '0' + hex_val[2]
            # Convert to char array
            if f == 0:
                    h = h_val
                    f = 1
            else:
                    h = h + h_val
    # convert the temp back to float
    t = round(struct.unpack('!f', h.decode('hex'))[0], 2)

    h = ''
    # data is reversed
    for element in reversed(number[5:9]):
            # Converted to hex
            hex_val = hex(element)
            # Print hex_val
            try:
                    h_val = hex_val[2] + hex_val[3]
            except IndexError:
                    h_val = '0' + hex_val[2]
            # Convert to char array
            if f == 0:
                    h = h_val
                    f = 1
            else:
                    h = h + h_val
    # convert back to float
    hum = round(struct.unpack('!f', h.decode('hex'))[0], 2)
    return [t, hum]

if name == “main”:
print dht(7,1)
print ultrasonicRead(4)


#2

Hey,
We just updated the GrovePi library on Github so it should not return a tuple [-1,-1] in case of DHT errors which should be easier for handling the errors. We also added a debug flag so that the error’s won’t be printed when debug is disabled.

Please update your local repo and run sudo python setup.py install to install the library.

-Karan


#3

Thank you for the input, I have updated the library, but this will not solve my problem.


#4

Hey Maurice,
Can you tell us more about the problem that you are facing. We’ll try our best to help you out.

-Karan


#5

I have installed the GrovePi as described. I also updated the firmware as described in the forum.The board is working if I do the led blinking test. The board is not working for any sensor that requires reading values like the DHT. I have posted already the script I used for testing and also found in what function in grovepi.py the IOerror is reased.

I have tried a lot of things, but keeps getting the IOerror.

Maurice


#6

Hey Maurice,
Can you post what errors you get when the sensors are connected. Also, can you just double check that the sensors are connected to same ports that you are using in the Software too. I can’t seems to think of a good reason why the sensors are not working properly. Another issue might be that the sensors that you have might be broken too.

-Karan


#7

I have posted my error in the first post. After updating the GrovePi dir, I now get the values -1 -1.

This function is causing the error:
def read_i2c_block(address):
try:
return bus.read_i2c_block_data(address, 1)
except IOError:
print “IOError #3
return -1


#8

Maurice,

Try something like this:

Note that I am at work, so I haven’t explicitly tested that, but I do the equivalent bit of exception handling with my own GPi DHT sensor.

I have found that the call to bus.read_i2c_block_data is not completely reliable. The idea of the while block is to call it until it returns a sensible result (usually in one to three tries), with a pause between calls (which you might want to make a bit longer). I also included a count so that it won’t loop forever.

I found that the DHT sensor that came in my GPi starter kit was defective. Happily, I had another one lying around. If you cannot get a sensible result out of your sensor even after doing some decent exception handling, you might try one of these: http://www.adafruit.com/products/393 – worked great for me. I just clipped the connector off of the defective sensor and twisted the leads together.

Hope that helps,
B


#9

Hi,

Thank you for your help. I have changed the bus.read_i2c_block_data but it did not work. I have also tried the other sensors in my starterkit, but none of them are working. Only thing that is working is the blinking led. I can’t believe alle sensors are defect.

After buying a disapointing BrickPi this is the second board that is not working for me. I don’t think I will buy a new GrovePi board to see of mine is broken.

Too bad I really wanted to play with the GrovePi and teach my children.

Maurice


#10

Hey Maurice,

We’re again sorry to hear about all the troubles. If we shipped you a defective device from Dexter Industries, and you think the issue is defective hardware, we’ll replace it immediately. Can you contact us through our website with information about your order:

http://dexterindustries.com/contact.html

John