0

python temperature program
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

python temperature program

by cycloptic on Thu Apr 04, 2019 10:40 pm

I modified the example program to work with my sensors. It runs correctly a few times through the loop, but eventually fails and reports this:

Adafruit IO request failed: 422 Unprocessable Entity - request failed - failed to save data to feed h1. Undefined method '>' for nil:NilClass

Are there settings that can provide more information? How would I diagnose or debug such a thing?

cycloptic
 
Posts: 7
Joined: Fri Mar 30, 2018 7:28 am

Re: python temperature program

by brubell on Fri Apr 05, 2019 10:53 am

Could you provide more information about your hardware - are you using this library on a comptuer, a raspberry pi, etc?

Can you link the example program, or copy/paste the code you're running?

Can you paste the full error message?

brubell
 
Posts: 332
Joined: Fri Jul 17, 2015 10:33 pm

Re: python temperature program

by cycloptic on Fri Apr 05, 2019 12:37 pm

Thank you for helping. This is for a raspberry that has two SHT sensors and reads temperature and humidity at regular intervals. An earlier version prior to sending data to AIO runs without trouble, so I decided to try AIO. AIO appears to work and displays the data through a few loops, but then fails and will not recover. After some searching, I added the try statements in an attempt to catch the error and recover, but they do not (they do still let me terminate the run). I do need to learn more about the try and error handling but this is as far as I have gotten. It runs the loop correctly feeding AIO and responding with:
Done
Yup (repeating)
Then after a few loops it halts feeding and repeatedly responds (for either H or T) with:
Double OUCH! Adafruit IO request failed: 422 Unprocessable Entity - request failed - failed to save data to feed h1. undefined method `>' for nil:NilClass

Code: Select all | TOGGLE FULL SIZE
import time
import RPi.GPIO as GPIO
import pi_sht1x
import matplotlib.pyplot as plt
from Adafruit_IO import Client
import os.path
#print(os.path.abspath(pi_sht1x.__file__))
#print(os.path.dirname(pi_sht1x.__file__))
ADAFRUIT_IO_USERNAME = "??"
ADAFRUIT_IO_KEY = "??"
DATA_PIN = 23
SCK_PIN = 26
D2 = 21
C2 = 24
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
pi_T1 = aio.feeds('t1')
pi_T2 = aio.feeds('t2')
pi_H1 = aio.feeds('h1')
pi_H2 = aio.feeds('h2')


def main():
    for i in range(1000):
        sensor=pi_sht1x.sht1x.SHT1x(DATA_PIN, SCK_PIN, gpio_mode=GPIO.BOARD)
        sensor2=pi_sht1x.sht1x.SHT1x(D2, C2, gpio_mode=GPIO.BOARD)
        try:
            temp = sensor.read_temperature()
            humidity = sensor.read_humidity(temp)
            localtime = time.asctime( time.localtime(time.time()) )
            temp2 = sensor2.read_temperature()
            humidity2 = sensor2.read_humidity(temp)
       
            aio.send(pi_T1.key, sensor.temperature_fahrenheit)
            aio.send(pi_T2.key, sensor2.temperature_fahrenheit)
            aio.send(pi_H1.key, humidity)
            aio.send(pi_H2.key, humidity2)       
            time.sleep(1)
        except KeyboardInterrupt:
            print ("OUCH!\n")
        except Exception as ex:
            print ("Double OUCH!", ex, "\n")
        finally:
            print ("Done\n")
            #break
        print ("yup\n")

if __name__ == "__main__":
    while True:
        try:
            main()
        except KeyboardInterrupt:
            break
    GPIO.cleanup()
Last edited by brubell on Fri Apr 05, 2019 12:51 pm, edited 1 time in total.
Reason: formatting code

cycloptic
 
Posts: 7
Joined: Fri Mar 30, 2018 7:28 am

Re: python temperature program

by cycloptic on Fri Apr 05, 2019 5:28 pm

Here is a simpler program that is not complicated with sensor interfaces. It also fails after a few loops.
Last edited by brubell on Mon Apr 08, 2019 10:03 am, edited 1 time in total.
Reason: removed testing file, had credentials

cycloptic
 
Posts: 7
Joined: Fri Mar 30, 2018 7:28 am

Re: python temperature program

by millercommamatt on Fri Apr 05, 2019 5:37 pm

What's the for loop for?

millercommamatt
 
Posts: 298
Joined: Tue Jul 31, 2018 4:57 pm

Re: python temperature program

by cycloptic on Sun Apr 07, 2019 4:58 pm

Oh yes - I forgot to say that the AIO dashboard is a 2 line chart so the for loop just continues feeding the random numbers (one of which has smaller deviations and stays centered within the other). This shows the same problem - it works for a few cycles then shows the 422 error.

cycloptic
 
Posts: 7
Joined: Fri Mar 30, 2018 7:28 am

Re: python temperature program

by brubell on Mon Apr 08, 2019 10:03 am

cycloptic wrote:Oh yes - I forgot to say that the AIO dashboard is a 2 line chart so the for loop just continues feeding the random numbers (one of which has smaller deviations and stays centered within the other). This shows the same problem - it works for a few cycles then shows the 422 error.


This may be throwing a throttling error incorrectly - we're looking into it. For now, try increasing the time.sleep() value from 1 second. If you're on IO Free, you can only send 30 data points per minute, so it'll throttle you around the 30th iteration.

brubell
 
Posts: 332
Joined: Fri Jul 17, 2015 10:33 pm

Re: python temperature program

by cycloptic on Mon Apr 08, 2019 1:16 pm

False alarm - Sort Of : It does actually work if the loop cycle time is extended to 10 seconds. This seems to indicate something was overrun, although the message does not provide that indication (422) and the feed rate seems to fit within the AIO criteria. It might be a limitation of a Pi 1. Still, can anyone point me to the listings of the errors and some explanation of how to better diagnose AIO communications?

cycloptic
 
Posts: 7
Joined: Fri Mar 30, 2018 7:28 am

Re: python temperature program

by brubell on Mon Apr 08, 2019 1:26 pm

cycloptic wrote:False alarm - Sort Of : It does actually work if the loop cycle time is extended to 10 seconds. This seems to indicate something was overrun, although the message does not provide that indication (422) and the feed rate seems to fit within the AIO criteria. It might be a limitation of a Pi 1. Still, can anyone point me to the listings of the errors and some explanation of how to better diagnose AIO communications?


AIO Python will usually respond with a ThrottlingError, we're looking into this error (it's thrown from IO, not the IO Python).

brubell
 
Posts: 332
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.