So I’m trying to read some sensors from the grovepi with python.
I’m not really sure why, but the reading from the moisture sensor keeps getting mixed up with the reading from the light sensor…
import time
import grovepi
import math
moisture_sensor = 1
light_sensor = 2
temp_humidity_sensor = 4
def read_sensor():
try:
moisture = grovepi.analogRead(moisture_sensor)
light = grovepi.analogRead(light_sensor)
[temp,humidity] = grovepi.dht(temp_humidity_sensor,0)
if math.isnan(temp) or math.isnan(humidity):
return [-1,-1,-1,-1]
return [moisture,light,temp,humidity]
except IOError as TypeError:
return [-1,-1,-1,-1]
print(read_sensor())
moisture = grovepi.analogRead(moisture_sensor)
print(moisture)
light = grovepi.analogRead(light_sensor)
print(light)
[temp,humidity] = grovepi.dht(temp_humidity_sensor,0)
print(temp)
print(humidity)
The ouput is:
[211, 762, 21.0, 34.0]
762
762
21.0
34.0
So in the first instance it’s getting the right value but when I’m trying to get the value again, it shows the same value for moisture and light. When I put the print(read_sensor())
behind the other print functions the first ones are right and the last one is not working.
Any ideas why this is happening and how to fix it would be greatly appreciated!