Kwagga
Posts: 5
Joined: Thu Nov 10, 2016 9:22 am

GPIO fail on second attemp to open

Thu Nov 10, 2016 9:32 am

Hi all,

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.
>>> 
Thanks all

User avatar
Burngate
Posts: 6303
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: GPIO fail on second attemp to open

Thu Nov 10, 2016 10:31 am

Kwagga wrote:Hi all,

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?

"""
starting...
https://api.thingspeak.com/update?api_k ... T51A6XOR86

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.
>>>

Thanks all
I've put your code inside code - /code tags, to maintain the white spaces

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()

Kwagga
Posts: 5
Joined: Thu Nov 10, 2016 9:22 am

Re: GPIO fail on second attemp to open

Thu Nov 10, 2016 10:39 am

thanks mate

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPIO fail on second attemp to open

Thu Nov 10, 2016 12:10 pm

You aren't using GPIO.cleanup() at the end of your program.

See this link for a helpful reminder of how it should be used.
http://raspi.tv/2013/rpi-gpio-basics-3- ... ct-your-pi
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Kwagga
Posts: 5
Joined: Thu Nov 10, 2016 9:22 am

Re: GPIO fail on second attemp to open

Thu Nov 10, 2016 7:11 pm

Hi mate,

Thanks for pointing me in the direction of the cleanup task. I've run the cleanup after the call to TL and again after the call to LIGHT (as well as in finally).

If I comment LIGHT out TL returns a value and the program works.
If I comment TL out LIGHT returns a value and the program works.

If I run both statements the program fails on "time.sleep(0.1)": Code and output below.

Code: Select all

def RCtime(RCpin):
    LT = 0
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(RCpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    if (GPIO.input(RCpin) == True):
        LT += 1
    return (str(LT))

def RClight (RCpin):
        reading = 0
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        print'1'
        time.sleep(0.1)
        print'2'
        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == 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:
            print'Start Try'
            RHW, TW, TWF, PRS, TW2 = getSensorData()
            print'Get LT'
            #LT = RCtime(RCpin)
            #print'Got LT: ' + LT
            print'GPIO cleanup'
            #GPIO.cleanup()
            print'Get Light'          
            LIGHT = RClight(24)
            print'Got light' + LIGHT
            print'light: ' + LIGHT
            GPIO.cleanup()
            #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
        finally:
            print'cleaning up ports'
            GPIO.cleanup()
OUTPUT

starting...
https://api.thingspeak.com/update?api_k ... T51A6XOR86
Start Try
Get LT
GPIO cleanup
Get Light
1
exiting.
cleaning up ports
>>>

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPIO fail on second attemp to open

Thu Nov 10, 2016 7:43 pm

Kwagga wrote: I've run the cleanup after the call to TL and again after the call to LIGHT (as well as in finally).
No. Call GPIO.cleanup once and once only right at the end of the program.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Kwagga
Posts: 5
Joined: Thu Nov 10, 2016 9:22 am

Re: GPIO fail on second attemp to open

Fri Nov 11, 2016 9:55 am

Hi, thank I removed the additional ones, It's still exiting on the 'time.sleep(0.1)'

How do I catch the exception?

thanks

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPIO fail on second attemp to open

Fri Nov 11, 2016 10:03 am

You've essentially hidden all exceptions with your try except.

Change it to

Code: Select all

except Exception as err:
    print(err)
    print("exiting")
This time when you run the code, it should tell you what exception was raised. Post what you get.

Generally speaking an except without a specific exception type isn't a good thing to do particularly around your main program loop. You should be explicit about which exception you are trying to catch. For example

Code: Select all

except FileNotFoundError:
If there is a scenario where a file might not exist.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”