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:
- Which port should I be using?
- Does the D11 port correspond to pin 11? Likewise, which pins are associated with the gopigo ports i2c, A1, and serial?
- 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)?
- Is the ‘address’ variable always set to 0x08 for gopigo / raspberry pi?
- My grove t. & h. sensor is version 1.2. Does this influence the module_type variable or something else (or nothing else!)?
- 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?
- 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?
- 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.
- 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:
#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:
#from gopigo import *
import RPi.GPIO as GPIO
for RPI version 1, use “bus = smbus.SMBus(0)”
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
bus = smbus.SMBus(0)
print("P version is ", p_version); #2 for me
def dht(pin, module_type):
write_i2c_block(address,dht_command + [pin,module_type,unused])
number = bus.read_i2c_block_data(address,1)
if number == -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,2) h='' for element in (number[5:9]): h+=chr(element) hum_val=struct.unpack('f',h) hum=round(hum_val,2) else: t_val = bytearray(number[1:5]) h_val = bytearray(number[5:9]) t=round(struct.unpack('f',t_val),2) hum=round(struct.unpack('f',h_val),2) return[t,hum]
[temp,humidity] = dht(pin,1)
print("temp = ", temp , " humidity = ", humidity)
except IOError: print("Error")