OSError - Failed SSL handshake

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.
Locked
User avatar
basvdk
 
Posts: 6
Joined: Sun May 23, 2021 7:33 pm

OSError - Failed SSL handshake

Post by basvdk »

I'm running a little script that will send temp/pressure/humidity from my Adafruit Funhouse to Adafruit IO.

After succesfully sending data from anywhere between 15 to 30 minutes the communication will fail with the mentioned error:

OSError: Failed SSL handshake

After rebooting the device communication will continue.

Below the script I'm running:

Code: Select all

import board
import time
import wifi
from adafruit_funhouse import FunHouse
import digitalio            

#################### FUNCTIONS ####################

def reset_dotstar():
    dots = funhouse.peripherals.dotstars
    dots.brightness = 0.0
    return dots

def connected(client):
    print("Connected to Adafruit IO! Subscribing...")

#################### FUNCTIONS ####################
#################### VARIABLES ####################

RED = 0xFF0000  # (255,0,0)
YELLOW = 0xFFFF00  # (255,255,0)
GREEN = 0x00FF00  # (0,255,0)

#################### VARIABLES ####################
#################### Board initialization ####################

# Initialize Funhouse object
funhouse = FunHouse(default_bg=None)

# Create a dots object to config individual leds
dots = reset_dotstar()

# Initialize a new MQTT Client object
print("Setting up network and Adafruit IO connections")
funhouse.network.init_io_mqtt()
funhouse.network.on_mqtt_connect = connected
funhouse.network.mqtt_connect()

# Turn of screen
l = digitalio.DigitalInOut(board.TFT_BACKLIGHT)
l.direction = digitalio.Direction.OUTPUT
l.value = False # turns the display backlight off / True will enable the backlight

# startup timestamp
event_timestamp = time.monotonic()

#################### Board initialization ####################
#################### Running code ####################

while True:

    # Process Adafruit IO messages
    funhouse.network.mqtt_loop()

    # Send sensor data to Adafruit IO every 10 seconds
    if (time.monotonic() - event_timestamp) > 10:
        funhouse.peripherals.led = True
        print("Sending data to adafruit IO!")
        funhouse.network.mqtt_publish("temperature", float(funhouse.peripherals.temperature))
        funhouse.network.mqtt_publish("humidity", float(funhouse.peripherals.relative_humidity))
        funhouse.network.mqtt_publish("pressure", float(funhouse.peripherals.pressure))
        event_timestamp = time.monotonic()
        funhouse.peripherals.led = False   
    
    # Use the UP button to turn on/ off the display backlight
    if funhouse.peripherals.button_up:
        if l.value:
            l.value = False
        else:
            l.value = True

    # Color the first dotstar red or green based onn wifi connection  
    if wifi.radio.ipv4_address:
        dots[0] = GREEN
        dots.brightness = 0.01
    else:
        print("No network connection!")
        dots[0] = RED
        dots.brightness = 0.01

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: OSError - Failed SSL handshake

Post by mikeysklar »

How far is your Funhouse from the nearest WiFi device? Does it make a difference if you re-orient the Funhouse so the WiFi chip has line of site to the AP? Obviously a bit of overkill, but just trying to understand if this is a WiFi signal issue.

User avatar
basvdk
 
Posts: 6
Joined: Sun May 23, 2021 7:33 pm

Re: OSError - Failed SSL handshake

Post by basvdk »

I tested it in several locations, one of which was directly besides the AP. These are Ubiquiti AP's and provice excellent coverage throughout the house.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: OSError - Failed SSL handshake

Post by mikeysklar »

@basvdk,

Thank you for confirming we are not losing WiFi signal.

There have been cases on this topic before that should be resolved with the current CircuitPython release.

https://github.com/adafruit/circuitpython/issues/3651

What are you currently running in terms of CircuitPython, bootloader and library bundle on the funhouse? It's quite new so I would be surprised if you are running something earlier than CircuitPython 6.2.0. There is a library bundle drop from 20210525 and a CircuitPython 6.3.0 release candidate you could try to see if it the issue resolves.

https://circuitpython.org/board/adafruit_funhouse/
https://circuitpython.org/libraries

User avatar
basvdk
 
Posts: 6
Joined: Sun May 23, 2021 7:33 pm

Re: OSError - Failed SSL handshake

Post by basvdk »

I've updated to Circuit Python 6.3.0 RC0
Adafruit CircuitPython 6.3.0-rc.0 on 2021-05-25; Adafruit FunHouse with ESP32S2
Also updated the script to do a ping test if another OSError occurs.

Code: Select all

    # Send sensor data to Adafruit IO every 10 seconds
    if (time.monotonic() - event_timestamp) > 10:
        funhouse.peripherals.led = True
        print("Sending data to adafruit IO!")
        try:
            funhouse.network.mqtt_publish("temperature", float(funhouse.peripherals.temperature))
            funhouse.network.mqtt_publish("humidity", float(funhouse.peripherals.relative_humidity))
            funhouse.network.mqtt_publish("pressure", float(funhouse.peripherals.pressure))
        except OSError:
            print("Current IP address: %s" % str(wifi.radio.ipv4_address))
            ipv4 = ipaddress.ip_address("8.8.8.8")
            print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
        finally:
            funhouse.peripherals.led = False
            event_timestamp = time.monotonic()
Will keep you posted :-)

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: OSError - Failed SSL handshake

Post by mikeysklar »

Thank you for trying the release candidated CP 6.3.0. Please confirm that you also updated the libraries to be current as well.

User avatar
basvdk
 
Posts: 6
Joined: Sun May 23, 2021 7:33 pm

Re: OSError - Failed SSL handshake

Post by basvdk »

After the update I had it running for 24 hours without loosing connection, so the connection seems more stable after the upgrade.

I then placed the funhouse further away from the AP. The latency for the connection is higher (as expected) but when the device has a bad connection when trying to send messages to IO the OSError returns. I used a simple ping monitor to see if the device was still connected and I can see it occasionally missing a single ping (time out).

It looks like the connection to IO isn't restored automatically after this, should I do a

Code: Select all

funhouse.network.mqtt_connect()
in the

Code: Select all

except:
block to restore the connection?

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: OSError - Failed SSL handshake

Post by brubell »

Yep, within the except I'd also write some code to verify that the WiFi connection is still established prior to attempting a MQTT reconnection

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”