I've hooked up a few sensors to my raspberry PI 3 and now I getting stuck attempting to get read a second gpio, I just bought it so I likely to have missed something obvious.
Below is the code, I have removed the API key. The error I get is below the code.
Secondly what is the purpose of LT and the RCtime function?
Code: Select all
"""
# Import all the libraries we need to run
import Adafruit_BMP.BMP085 as BMP085
import sys
import RPi.GPIO as GPIO
import os
from time import sleep
import Adafruit_DHT
import urllib2
DEBUG = 1
# Setup the pins we are connect to
RCpin = 23
RCpin2 = 24
DHTpin = 4
sensor = BMP085.BMP085()
#Setup our API and delay
myAPI = "xxxx"
myDelay = 10 #how many seconds between posting data
GPIO.setmode(GPIO.BCM)
GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def getSensorData():
RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)
PRS = sensor.read_pressure()/100
TW2 = sensor.read_temperature()
#Convert from Celius to Farenheit
TWF = 9/5*TW+32
return (str(RHW), str(TW), str(TWF), str(PRS), str(TW2))
def RCtime(RCpin):
LT = 0
if (GPIO.input(RCpin) == True):
LT += 1
return (str(LT))
def RClight (RCpin2):
LIGHT = 0
GPIO.setup(RCpin2, GPIO.OUT)
GPIO.output(RCpin2, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(RCpin2, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin2) == GPIO.LOW):
LIGHT += 1
return (str(LIGHT))
# main() function
def main():
print 'starting...'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
print baseURL
while True:
try:
RHW, TW, TWF, PRS, TW2 = getSensorData()
LT = RCtime(RCpin)
LIGHT = RClight(RCpin2)
#print'got the data' + LIGHT
f = urllib2.urlopen(baseURL +
"&field1=%s" % (TW) +
"&field2=%s" % (RHW) +
"&field3=%s" % (PRS) +
"&field4=%s" % (TW2))
print f.read()
print "Temp: " + TW + " Humidity: " + RHW + " Presure: " + PRS + " Temp2: " + TW2 + " LT: " + LT #+ " Light: " + LIGHT #
f.close()
sleep(int(myDelay))
except:
print 'exiting.'
break
# call main
if __name__ == '__main__':
main()
starting...
https://api.thingspeak.com/update?api_key=NBNNDAT51A6XOR86
Warning (from warnings module):
File "/home/pi/build/WeatherStation/Temp_Humid_Presure_Light_Thingspeak.py", line 52
GPIO.setup(RCpin2, GPIO.OUT)
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
exiting.
>>>