Miceuz / i2c-moisture-sensor

I2C based soil moisture sensor
Apache License 2.0
240 stars 72 forks source link

Trouble getting reading from Chirp! with Raspberry PI #17

Closed dpassarella closed 7 years ago

dpassarella commented 7 years ago

Hi, I have 2 Chirp!s and I can't get them to give meaningful readings from the Raspberry Pi. I have them wired like this

I am using the following code from your github readme

import smbus, time, sys

class Chirp: def init(self, bus=1, address=0x20): self.bus_num = bus self.bus = smbus.SMBus(bus) self.address = address

def get_reg(self, reg):

read 2 bytes from register

val = self.bus.read_word_data(self.address, reg)
# return swapped bytes (they come in wrong order)
return (val >> 8) + ((val & 0xFF) << 8)

def reset(self):

To reset the sensor, write 6 to the device I2C address

self.bus.write_byte(self.address, 6)

def set_addr(self, new_addr):

To change the I2C address of the sensor, write a new address

# (one byte [1..127]) to register 1; the new address will take effect after reset
self.bus.write_byte_data(self.address, 1, new_addr)
self.reset()
self.address = new_addr

def moist(self):

To read soil moisture, read 2 bytes from register 0

return self.get_reg(0)

def temp(self):

To read temperature, read 2 bytes from register 5

return self.get_reg(5)

def light(self):

To read light level, start measurement by writing 3 to the

# device I2C address, wait for 3 seconds, read 2 bytes from register 4
self.bus.write_byte(self.address, 3)
time.sleep(1.5)
return self.get_reg(4)

def repr(self): return "<Chirp sensor on bus %d, addr %d>" % (self.bus_num, self.address) if name == "main": addr = 0x20 if len(sys.argv) == 2: if sys.argv[1].startswith("0x"): addr = int(sys.argv[1], 16) else: addr = int(sys.argv[1]) chirp = Chirp(1, addr)

print chirp print "Moisture\tTemperature\tBrightness" while True: print "%d\t%d\t%d" % (chirp.moist(), chirp.temp(), chirp.light()) time.sleep(1) At first, I would get:

<Chirp sensor on bus 1, addr 32> Moisture Temperature Brightness Traceback (most recent call last): File "chirp.py", line 57, in print "%d\t%d\t%d" % (chirp.moist(), chirp.temp(), chirp.light()) File "chirp.py", line 29, in moist return self.get_reg(0) File "chirp.py", line 12, in get_reg val = self.bus.read_word_data(self.address, reg) IOError: [Errno 121] Remote I/O error

Then, I hit the button on the chirp right before I ran the code and started getting this

pi@raspberrypi:~ $ python chirp.py <Chirp sensor on bus 1, addr 32> Moisture Temperature Brightness 65535 65535 65535 65535 65535 65535

I have checked the reviews and the open issues on the Arduino library from Apollon77 and I even tried adding a 10K pullup on both i2c lines and still get similar results. The only time it seems to work is the first time it runs on arduno code, but then goes back to 65535 readings.

Do you think this is a hardware issue? Do you have any guidance? I have tried this with 2 Chirp!'s and still the same thing.

Miceuz commented 7 years ago

I have answered your question for plant watering alarm. BTW, where did you get the Chirp?