Using Raspi and GrovePi+ cape with 1.3.0 firmware and python library, I can get digital relays and I2C RGB LCD to work, but the readings I’m getting from the analog sensors (light 1.1, gas MQ9, UV) are very sporadic and don’t correlate with reality (e.g. covering the light sensor). Indeed, I still get same range of values when the sensors are disconnected. I am having a similar issue with the TH02 sensor readings on I2C, although it at least throws an error when disconnected.
#-*- coding: utf-8 -*-
#!/usr/bin/env python
from datetime import datetime as dt
import grove_rgb_lcd as LCD
from grove_i2c_temp_hum_mini import th02
import grovepi, time, json, traceback
try:
with open("ioMap.json") as f:
ioMap = json.load(f)
relays = ioMap["digital"]["relay"]
ls_port = int(ioMap["analog"]["light"])
buzzer = int(ioMap["digital"]["buzzer"])
gas_port = int(ioMap["analog"]["MQ9"])
except:
msg = 'JSON Failed:\n{}'.format(traceback.format_exc())
print(msg)
#initialize sensors
thSense=th02()
grovepi.pinMode(ls_port,"INPUT")
grovepi.pinMode(gas_port,"INPUT")
grovepi.pinMode(buzzer,"OUTPUT")
for key in relays:
grovepi.pinMode(relays[key],"OUTPUT")
print(str(key)+" initialized on port D",relays[key])
def LCD_tempColor(temp):
"set LCD color based on temperature"
tooCold = 55
tooHot = 85
justRightH = 75
justRightC = 65
if(temp < justRightC):
R = 0
G = max(int(255*(temp-tooCold)/(justRightC-tooCold)),0)
B = min(int(255-255*(temp-tooCold)/(justRightC-tooCold)),255)
elif(temp > justRightH):
R = min(int(255-255*(tooHot-airTemp)/(tooHot-justRightH)),255)
G = max(int(255*(tooHot-airTemp)/(tooHot-justRightH)),0)
B = 0
else: #just right
R = 0
G = 255
B = 0
LCD.setRGB(R,G,B)
return
def Relay(number, toggle):
if toggle == "on":
grovepi.digitalWrite(number,0)
elif toggle == "off":
grovepi.digitalWrite(number,1)
else:
print("relay error")
return
def Light(port):
# Get light sensor value time averaged over 2s
period_s = 2
freq_Hz = 10
measurements = int(period_s*freq_Hz)
sensor_value = grovepi.analogRead(port)
for i in range(measurements):
time.sleep(1/freq_Hz)
sensor_value += grovepi.analogRead(port)
return sensor_value/(measurements+1)
def MQ9(port):
sensor_value = grovepi.analogRead(port)
return sensor_value
def Buzzer():
# Buzz for 1 second
grovepi.digitalWrite(buzzer,1)
time.sleep(1)
grovepi.digitalWrite(buzzer,0)
return
while True:
try:
airHum = thSense.getHumidity()
airTemp = thSense.getTemperature()
light = Light(ls_port)
gas = MQ9(gas_port)
msg = str(dt.now().__format__("%Y.%m.%d %H:%M "))
msg += "T:%.0fC RH:%.0f%% L:%.0f G:%.0f" %(airTemp, airHum, light, gas)
print(msg)
if light > 500:
Buzzer()
msg = "ALERT!"
LCD_tempColor(airTemp)
LCD.setText(msg)
time.sleep(10)
except:
msg = 'Failed:\n{}'.format(traceback.format_exc())
print(msg)
exit()
The output is as follows, with the sensors located in my dining room ( and NOT in the oven as the th02 readings might have you believe )
At various points in that data collection, I disconnected the gas sensor and shone a flashlight on the light sensor. In other runs, I’ve gotten temperature readings as low as -20. Something is clearly not working. I’ve had some of these same sensors working on a Galileo cape previously so am inclined to think it is an issue with this cape, its firmware, or something I’m missing in RPi configuration.
I thought it might have been the old Pi model B I was using originally, but am seeing the same issues with a newer Pi3 and a fresh raspbian stretch lite install.
Any help is much appreciated.