Hi
I have been working with the Grove Pi for only some days, but have worked with the RPi for about a year and have done a few projects.
I really like the Grove Pi as I can do things better and faster.
So I have been building a setup to measure air quality using the sensors for Humidity&Temperature and Dust.
I was trying to add the light and sound sensors, but eventhough I get output - its the same.
Somehow I think the data from the sound sensor is copied to the light sensor.
First I setup the light sensor to A2 and sound to A0. Same data was collected.
Then I moved the lightsensor to A1, but didnt update the code to reflect it. Same data was collected.
Then I updated the code. Same data was collected.
So what I conclude from this is that the data from the soundsensor is applied to the lightsensor. Either in code, firmware or hardware.
This is my code:
from __future__ import print_function
import paho.mqtt.publish as publish
import psutil
import string
import random
import time
import grovepi
import atexit
from grove_rgb_lcd import *
from grovepi import *
#Setting up sensors
atexit.register(grovepi.dust_sensor_dis)
sensor = 7 #D7
LightSensor = 1 #A2
SoundSensor = 0 #A0
LED_green = 4 #D4
LED_red = 3 #D3
pinMode(LED_green,"OUTPUT")
pinMode(LED_red,"OUTPUT")
grovepi.dust_sensor_en()
last_sound=0
#Setting up data to write data to Thingspeak
string.alphanum='1234567890avcdefghijklmnopqrstuvwxyzxABCDEFGHIJKLMNOPQRSTUVWXYZ'
channelID = "XXXXXXXXX"
writeAPIKey = "XXXXXXXXX"
mqttHost = "mqtt.thingspeak.com"
mqttUsername = "TSMQTTRpiDemo"
mqttAPIKey ="XXXXXXXXX"
tTransport = "websockets"
tPort = 80
topic = "channels/" + channelID + "/publish/" + writeAPIKey
while True:
try:
#Set green operation
digitalWrite(LED_green,1)
digitalWrite(LED_red,0)
#Gather data from sensors
[new_val,lowpulseoccupancy] = grovepi.dustSensorRead()
[temp,hum] = grovepi.dht(sensor,0)
light_intensity = grovepi.analogRead(LightSensor)
sound_level = grovepi.analogRead(SoundSensor)
if sound_level > 0:
last_sound=sound_level
#Prep connection to Thingspeak
clientID=''
# Create a random clientID.
for x in range(1,16):
clientID+=random.choice(string.alphanum)
#To avoid posting empty data as "0" from the dust sensor. New_val means there is new data
if new_val:
#Show data
print ("Temp =", temp, "C & Humidity =", hum,"% & Dust consentrantion =",lowpulseoccupancy," & Light =",light_intensity," & Sound =",last_sound)
#Prepare data for use on LCD & Thingspeak
t = str(temp)
h = str(hum)
d = str(lowpulseoccupancy)
l = str(light_intensity)
s = str(last_sound)
# build the payload string for Thingspeak
payload = "field1=" + t + "&field2=" + h + "&field3=" + d + "&field4=" + l + "&field5=" + s
# attempt to publish data to the topic on Teamspeak.
try:
publish.single(topic, payload, hostname=mqttHost, transport=tTransport, port=tPort,auth={'username':mqttUsername,'password':mqttAPIKey})
print (" Published to Teamspeak to host: " , mqttHost , " clientID= " , clientID)
except (KeyboardInterrupt):
break
except:
print ("There was an error while publishing the data.")
digitalWrite(LED_green,0)
digitalWrite(LED_red,1)
#Set LCD color
setRGB(0,128,64)
setRGB(0,0,255)
#Print on LCD
setText("T:" + t + "C" + " H:" + h + "%" + " Dust:" + d + "psc/L")
#Wait 5 seconds
time.sleep(5)
except (IOError,TypeError) as e:
print("Error")
digitalWrite(LED_green,0)
digitalWrite(LED_red,1)
I have checked it several times, but I cant find the fault in the code itself. Can you find fault?