Black Lives Matter - Action and Equality.
0

AIO CLIENT ERROR HANDLING
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

AIO CLIENT ERROR HANDLING

by kcweedblog on Wed Apr 29, 2020 10:25 am

I am having some problems with an AIO Raspberry Pi. I used one of the demo scripts and adjusted for my sensor a Si7024. Works well until the WiFi falls offline. The Pi is in a outdoor greenhouse, so connection is mostly stable... Able to VNC with the occasional drop. But every 3-5 minutes AIO fails and the script crashes. I only need to poll the device once a minute or maybe even once every 5 minutes. We are just tracking grow cycles. In my limited knowledge I was thinking there could be a way to error handle the connection loss or maybe even Stop and Restart the script every 5-30 minutes just to keep data flowing even if a gap of Wifi coverage happens. Cron job? In code?

Code: Select all | TOGGLE FULL SIZE
# import standard python modules.
import time
import board
import busio
import adafruit_si7021
# import adafruit dht library.
#import Adafruit_DHT

# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed

# Delay in-between sensor readings, in seconds.
DHT_READ_TIMEOUT = 5

# Pin connected to DHT22 data pin
DHT_DATA_PIN = 26

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_si7021.SI7021(i2c)

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = ''

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'kcweedblog'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')

# Set up DHT22 Sensor.
#dht22_sensor = Adafruit_DHT.DHT22

while True:
    tempsi = ((sensor.temperature) * 1.8 + 32)
    tempF = float("{0:.2f}".format(tempsi))
    humidsi = (sensor.relative_humidity)
    humidP = float("{0:.2f}".format(humidsi))
    # humidity, temperature = Adafruit_DHT.read_retry(dht22_sensor, DHT_DATA_PIN)
    if humidsi is not None and tempsi is not None:
        print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(tempsi, humidsi))
        # Send humidity and temperature feeds to Adafruit IO
        temperature = '%.2f'%(tempF)
        humidity = '%.2f'%(humidP)
        aio.send(temperature_feed.key, str(temperature))
        aio.send(humidity_feed.key, str(humidity))
    else:
        print('Failed to get Si7024 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
    # Timeout to avoid flooding Adafruit IO
    time.sleep(DHT_READ_TIMEOUT)

kcweedblog
 
Posts: 1
Joined: Wed Apr 29, 2020 10:06 am

Re: AIO CLIENT ERROR HANDLING

by brubell on Wed Apr 29, 2020 12:35 pm

Works well until the WiFi falls offline. The Pi is in a outdoor greenhouse, so connection is mostly stable... Able to VNC with the occasional drop. But every 3-5 minutes AIO fails and the script crashes. I only need to poll the device once a minute or maybe even once every 5 minutes.


Do you have an example of the error you get when you lose WiFi connection? Could you please copy and paste it below, thanks

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

Please be positive and constructive with your questions and comments.