Grove DHT temp+hum. sens. via gopigo board


#1

Hi,

I’m new to this robotics stuff, but super excited. I love the gopigo bot - motors work, servo works, got video streaming working via VLC and the pi cam, the bot now produces and records sound via Creative Labs Soundblaster USB sound adapter and alsa tools, and the ultrasound ranger should be arriving Monday. Been wanting to do this for 20 years- now so glad it is possible with a more-or-less plug-and-play set up.

But I’m not quite there. I would like to use the additional ports on the gopigo to directly attach additional sensors. Thinking this would be easy - like everything else has been - I went ahead and bought the Grove temperature and humidity sensor, and I’ve been trying to write a program to read t. and h. from this sensor. The grovepi code gave some hints, but I’ve had no luck with the sensor attached directly to the gopigo board. Reading the specs, it sounds like this sensor needs a serial port. But the grovepi diagrams have it connected to a digital port. Then the code seems to be reading and writing from i2c. So, I tried setting it up using the grovepi configuration with the sensor attached to gopigo digital port D11 (front right and bottom port) and I used the code pasted at the bottom of this message. The code throws an error when trying to write a block to i2c. I was hoping you could help with a few questions:

  1. Which port should I be using?
  2. Does the D11 port correspond to pin 11? Likewise, which pins are associated with the gopigo ports i2c, A1, and serial?
  3. How is the command number set? Is this a unique, arbitrary identifier, or does this correspond to an address in the hardware (or something different)?
  4. Is the ‘address’ variable always set to 0x08 for gopigo / raspberry pi?
  5. My grove t. & h. sensor is version 1.2. Does this influence the module_type variable or something else (or nothing else!)?
  6. Do the yellow and white wires need to be switched? Will pulling the wires out and simply pushing them into new slots work or will this break the cabling?
  7. If I need to use the serial port, how would I connect to and read from the serial port? I looked at the code for the ultrasonic sensor, and that code connects through a device file. How would I know what file corresponds to the t & h. sensor? How would I “mount” this device file to begin with, since I doubt it is plug-and-play?
  8. Is there a general tutorial or documentation about how to read / write from / to gopigo ports? The example code is very helpful, but not all the gopigo ports have example code.
  9. What information needs to be in the block variable of the write_i2c_block function?

Many, many thanks!

Here is the code I used, pasted below (I included some of the functions from the gopigo library). It throws an IOError at bus.read_i2c_block_data(address,1) - It may produce error earlier, but I don’t catch exceptions for write_i2c_block:


#!/usr/bin/env python

#Plug DHT humidity and temperature sensor into D11 digital port on gopigo card. Pin 11?
#command number 11 arbitrarily set
#module_type values for different dht:

DHT11 0

DHT22 1

DHT21 2

DHT2301 3

#from gopigo import *
import time
import math
import struct
import sys
import smbus
import RPi.GPIO as GPIO
import subprocess

for RPI version 1, use “bus = smbus.SMBus(0)”

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

pin=11
dht_command=[11]
address=0x08
unused=0

if sys.version_info<(3,0):
p_version=2
else:
p_version=3

print("P version is ", p_version); #2 for me

def write_i2c_block(address,block):
try:
op=bus.write_i2c_block_data(address,1,block)
time.sleep(.005)
return op
except IOError:
if debug:
print "IOError"
return -1
return 1

def dht(pin, module_type):
print(“In dht”)
write_i2c_block(address,dht_command + [pin,module_type,unused])
print(“wrote block”)
time.sleep(0.6)
try:
bus.read_byte(address)
print(“read byte”)
number = bus.read_i2c_block_data(address,1)
print(“read block”)
time.sleep(0.1)
if number == -1:
return -1
except(TypeError, IndexError):
return -1

if p_version==2:
	h=''
	for element in (number[1:5]):
		h+=chr(element)
	
	t_val=struct.unpack('f',h)
	t = round(t_val[0],2)

	h=''
	for element in (number[5:9]):
		h+=chr(element)
	
	hum_val=struct.unpack('f',h)
	hum=round(hum_val[0],2)

else:
	t_val = bytearray(number[1:5])
	h_val = bytearray(number[5:9])
	t=round(struct.unpack('f',t_val)[0],2)
	hum=round(struct.unpack('f',h_val)[0],2)
return[t,hum]

while True:
try:
[temp,humidity] = dht(pin,1)
print("temp = ", temp , " humidity = ", humidity)

except IOError:
	print("Error")

#2

Just a quick follow up - ultrasound ranger, light sensor (both analog port) and motion sensor (digital port) work well. Still can’t figure out how to read from DHT T & H sensor. Tried analog, digital, i2c, serial ports with no luck. Any help / insights about how to get the DHT T & H sensor working on the GoPiGo board would be greatly appreciated.


#3

Hey David,
I just looked up the source for the firmware and the software and for the DHT sensors, there is no code in the firmware to actually read the data from the sensor so it wont work for now. We can add that in the next firmware but no ETA on it yet. The GoPiGo does support HDC1000 sensor http://www.seeedstudio.com/depot/Grove-TemperatureHumidity-Sensor-HDC1000-p-2535.html on the I2C port which works very well and has a better resolution too and might be the best option for you right now.

-Karan


#4

Hi Karan,

Thanks very much for the reply. Glad to know it’s a firmware issue. I’ll look into the option you recommended.

David