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)