Re: WiFi coming out of deep sleep
import socketpool
import ssl
import adafruit_requests as requests
import wifi
import secrets
import time
import board
import adafruit_ahtx0
import alarm
def post_data(msg):
socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())
response = https.post(JSON_POST_URL, data=data)
json_resp = response.json()
# Create the sensor object using I2C
sensor = adafruit_ahtx0.AHTx0(board.I2C())
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
"""
This section of retreiving and printing the mac address,
scanning the wifi network for access points, and then stopping the scan was
taken from several Adafruit example programs.
It's only value in this program is it seems to get the wifi working after
a cold boot from the deep sleep call at the end of the program,
I could not find a way to make my code reliable without this logic.
"""
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
print("Available WiFi networks:")
for network in wifi.radio.start_scanning_networks():
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
network.rssi, network.channel))
wifi.radio.stop_scanning_networks()
# end of special code to prep the wifi after recovery from deep sleep.
print ("Connecting to %s"%secrets["ssid"])
try:
wifi.radio.connect(secrets["ssid"], secrets["password"])
except (ConnectionError, ValueError, RuntimeError):
print("error on wifi.radio.connect")
# If we get an error, set to be awakened from deep sleep in 2 seonds and try again.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 2)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
print ("Connected to %s!"%secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)
JSON_POST_URL = "http://192.168.0.25:1880/myserver" # URL for my NodeRed server
x = round(sensor.temperature * 1.8 + 32.0,1)
y = round(sensor.relative_humidity,1)
temp = str(x)
humid = str(y)
# format the readings data as InfluxDB expects: Array of 2 object:
# 1st is readings, 2nd is tag
data = """[{"Temperature": """ + temp + """ , "Humidity": """ + humid + """}, {"Location": "Back_Porch"}]"""
post_data(data)
# Create a an alarm that will trigger 60 seconds from now.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60)
# Exit the program, and then deep sleep until the alarm wakes us.
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
# Does not return, so we never get here.
Re: WiFi coming out of deep sleep
Re: WiFi coming out of deep sleep
tannewt wrote:It's really hard to tell without serial. Could you attach a display or monitor the debug UART? The debug UART will have messages if you load a debug CircuitPython build onto it.
Re: WiFi coming out of deep sleep
Re: WiFi coming out of deep sleep
tannewt wrote:Not a dumb question! You can build one yourself by doing DEBUG=1 on the make line. Or, you might be able to get help on the Discord. https://adafru.it/discord
How do you know it's crashing?
Re: WiFi coming out of deep sleep
Re: WiFi coming out of deep sleep
Re: WiFi coming out of deep sleep
Re: WiFi coming out of deep sleep
import socketpool
import ssl
import adafruit_requests as requests
import wifi
import secrets
import time
import board
import adafruit_ahtx0
import alarm
import busio
def post_data(msg):
try:
socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())
response = https.post(JSON_POST_URL, data=data)
json_resp = response.json()
except RuntimeError:
print("RuntimeError in post_data")
uart.write(bytearray("\n\rRuntimeError in post_data"))
# If we get an error, set to be awakened from deep sleep in 2 seonds and try again.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 2)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
# Create the sensor object using I2C
sensor = adafruit_ahtx0.AHTx0(board.I2C())
uart = busio.UART(board.TX, board.RX, baudrate=115200)
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
"""
This section of retreiving and printing the mac address,
scanning the wifi network for access points, and then stopping the scan was
taken from several Adafruit example programs.
It's only value in this program is it seems to get the wifi working after
a cold boot from the deep sleep call at the end of the program,
I could not find a way to make my code reliable without this logic.
"""
uart.write(bytearray("\n\rJust out of deep sleep, checking mac address"))
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
"""
After testing I eliminated the scanning of the wifi network but kept the
reading of the MAC address. Seems to work with just that.
"""
print("Available WiFi networks:")
uart.write(bytearray("\n\rAvailable WiFi networks: "))
for network in wifi.radio.start_scanning_networks():
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
network.rssi, network.channel))
uart.write(bytearray("\n\r\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
network.rssi, network.channel)))
wifi.radio.stop_scanning_networks()
#end of special code to prep the wifi after recovery from deep sleep.
#print ("Connecting to %s"%secrets["ssid"])
uart.write(bytearray("\n\rConnecting to %s"%secrets["ssid"]))
try:
wifi.radio.connect(secrets["ssid"], secrets["password"])
except (ConnectionError, ValueError, RuntimeError):
print("Error on wifi.radio.connect")
uart.write(bytearray("\n\rError on wifi.radio.connect"))
# If we get an error, set to be awakened from deep sleep in 2 seonds and try again.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 2)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
#print ("Connected to %s!"%secrets["ssid"])
uart.write(bytearray("\n\rConnected to %s!"%secrets["ssid"]))
#print("My IP address is ", wifi.radio.ipv4_address)
uart.write(bytearray("\n\rMy IP address is " + str(wifi.radio.ipv4_address)))
JSON_POST_URL = "http://192.168.0.25:1880/myserver" # URL for my NodeRed server
x = round(sensor.temperature * 1.8 + 32.0,1)
y = round(sensor.relative_humidity,1)
temp = str(x)
humid = str(y)
# format the readings data as InfluxDB expects: Array of 2 object:
# 1st is readings, 2nd is tag
data = """[{"Temperature": """ + temp + """ , "Humidity": """ + humid + """}, {"Location": "5th_wheel"}]"""
post_data(data)
uart.write(bytearray("\n\rJust posted data, now into deep sleep!\n\n"))
#print("Just posted data, now into deep sleep!")
# Create a an alarm that will trigger 60 seconds from now.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60)
# Exit the program, and then deep sleep until the alarm wakes us.
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
# Does not return, so we never get here.
Re: WiFi coming out of deep sleep
try:
wifi.radio.connect(secrets["ssid"], secrets["password"])
except Exception as e:
print("Exception on wifi.radio.connect ", str(e))
uart.write(bytearray("\n\rError on wifi.radio.connect " + str(e)))
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 2)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)