forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
My code will actually execute and complete, but ends in safe mode. Shorter other bpm280 code piece will run correctly. Control +C has been unresponsive except in REPL.
Code top and REPEL as jpeg

text version of code[Hard crash code from ciruit.py
# while loop installed; no rate code rate call in time wait loop
# Cambria 8-31-2022 8 56 PM text backup, 1-2-2023 version 1 second wait
# Water Tanks 7 26 2022 working needs mode added and valve improved, cleanup
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""CircuitPython Essentials Internal RGB LED red, green, blue example"""
import time
# import datetime
import board
import digitalio
import neopixel
import adafruit_mprls
import ssl
import adafruit_bmp280
# from random import randint
import adafruit_requests
import socketpool
import wifi
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# from digitalio import digitalio.DigitalInOut
# set up switch input boad led on
sw_in = digitalio.DigitalInOut(board.IO7)
sw_in.switch_to_input(pull=digitalio.Pull.UP)
print("switch value io7", sw_in.value)
print("start of code water tank")
# set up neopixel
neo = neopixel.NeoPixel(board.NEOPIXEL, 1)
# default run rate to one hour
# Add rate check in end of code count down
defaultrate = 0
rate = defaultrate
print("rate is ", rate)
# repeating loop start
while True:
print("Water Tank Level Code Running")
print("iod7 to gound for sw_status False")
print("switch value io7", sw_in.value)
# Turn on neo white for pressure measuremnets
neo.brightness = 0.2
neo[0] = (255, 255, 255)
#
# CircuitPython Essentials Internal RGB LED red, green, blue example
# led = digitalio.DigitalInOut(board.LED)
# led.direction = digitalio.Direction.OUTPUT
# from digitalio import digitalio.DigitalInOut
j = 0
# while j < 5:
# led.value = True
# time.sleep(0.5)
# led.value = False
# time.sleep(0.5)
# j = j + 1
led.value = False
# set up switch inpu boad led on
# sw_in = digitalio.DigitalInOut(board.IO7)
# sw_in.switch_to_input(pull=digitalio.Pull.UP)
# set up neopixel
# neo = neopixel.NeoPixel(board.NEOPIXEL, 1)
# turn it off with 0 value
print("RGB at 0.2 brightness")
neo.brightness = 0.2
# red neo RGB indicates start of measurements
neo[0] = (255, 0, 0)
# Add air only correction
# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
bmp280.sea_level_pressure = 1013.25
print("\nTemperature: %0.1f C" % bmp280.temperature)
print("Pressure: %0.1f hPa" % bmp280.pressure)
print("Altitude = %0.2f meters" % bmp280.altitude)
air_only_pressure = ("%0.1f" % bmp280.pressure)
print("air only:" , air_only_pressure)
x = float(air_only_pressure)
print("x as float :", x)
print(type(air_only_pressure))
# Change this from sea level to mprl reads
corrected_hpa = bmp280.sea_level_pressure - x
print("corrected hPa :" , corrected_hpa)
time.sleep(1)
# July 18 2022 MPRLS example working
# for merge comment out duplicate items
# add list and take pressure measurement, sum ,
# average convert to gallons aproximation
i2c = board.I2C()
# Simplest use, connect to default over I2C
i = 0
sum = 0.0
avg = 0.0
psi = 0.0
while i < 5:
mpr = adafruit_mprls.MPRLS(i2c, psi_min=0, psi_max=25)
print(mpr.pressure, "pa")
psi = mpr.pressure * 0.014503773
print(psi, "PSI")
time.sleep(2)
sum = sum + psi
i = i + 1
avg = sum/i
print(sum, avg, )
pressure_feed = avg
# water adds 1.3 PSI for every foot of height
if avg < 14.7:
gallons = 0
else:
gallons = (avg - 14.7) * 128.7
print(gallons)
tanks = gallons * 3
print(tanks, "=gallons")
# Add logic to report negatives as 0
# 14 shoud go to 14.7 for sea level
# turn neo red
neo.brightness = 0.2
neo[0] = (255, 255, 255)
# connect to network and update IOT
# Add a secrets.py to your filesystem that has a dictionary called
# secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file
# or commit it into Git or othersource control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
# get rate value
try:
# Get the 'rate' feed from Adafruit IO
rate_feed = io.get_feed("rate")
except AdafruitIO_RequestError:
# If no 'rate' feed exists, create one
rate_feed = io.create_new_feed("rate")
# try:
# Send random integer values to the feed
# random_value = randint(40, 60)
# print("Sending {0} to temperature feed...".format(random_value))
# io.send_data(temperature_feed["key"], random_value)
# print("Data sent!")
# Retrieve data value from the feed
# print("Retrieving data from temperature feed...")
# received_data = io.receive_data(temperature_feed["key"])
# print("Data from temperature feed: ", received_data["value"])
# Temerature done, now raw_pressure
try:
# Get the 'raw_pressure' feed from Adafruit IO
pressure_feed = io.get_feed("pressure")
except AdafruitIO_RequestError:
# If no 'pressure' feed exists, create one
pressure = io.create_new_feed("pressure")
print("sending last pressure as psi")
io.send_data(pressure_feed["key"], psi)
print("Data sent pressure!")
# Retrieve data value from the feed
print("Retrieving data from pressure feed...")
received_data = io.receive_data(pressure_feed["key"])
print("Data from pressure: ", received_data["value"])
# Temerature, pressure done do gallons and then switch
try:
# Get the 'gallons' feed from Adafruit IO
gallons_feed = io.get_feed("gallons")
except AdafruitIO_RequestError:
# If no 'gallons' feed exists, create one
gallons = io.create_new_feed("gallons")
print("sending total for 3 tanks gallons")
io.send_data(gallons_feed["key"], tanks)
print("Data sent 3 Tanks gallons")
# Retrieve data value from the feed
print("Retrieving data from gallons feed...")
received_data = io.receive_data(gallons_feed["key"])
print("Data from gallons: ", received_data["value"])
# Send the switch value to feed
# switch status io7
try:
# Get the 'valve status' feed from Adafruit IO
valve_feed = io.get_feed("valve")
except AdafruitIO_RequestError:
# If no 'valve' feed exists, create one
valve_feed = io.create_new_feed("valve")
print("made new feed valve")
x = sw_in.value
if x == 1:
print("switch = True")
io.send_data(valve_feed["key"], "Manual")
print("switch status True Manual sent to feed")
elif x == 0:
print("switch status False Auto sent to feed")
io.send_data(valve_feed["key"], "Auto")
# Retrieve data value from the feed
print("Retrieving data from valve feed...")
received_data = io.receive_data(valve_feed["key"])
print("retrive switch value: ", received_data["value"])
print("code completed pass")
neo.brightness = 0.0
neo[0] = (0, 0, 0)
# delay to limit number of measurements taken
# import time moved to top
# import datetime, see begining
#
# Create class that acts as a countdown
# def countdown(h, m, s):
h = 0
m = 0
s = 5
if defaultrate == rate:
if rate == 0:
s = 5
if rate == 1:
m = 1
if rate == 2:
m = 20
if rate == 3:
h = 1
if rate == 4:
h = 6
if rate == 5:
h = 8
if rate == 6:
h = 24
# Calculate the total number of seconds
# h * 3600 + m * 60 + s removed for testing
total_seconds = 1
# if rate_feed > 6:
# total_seconds = rate_feed
# While loop that checks if total_seconds reaches zero
# If not zero, decrement total time by one second
while total_seconds > 0:
neo.brightness = 0.2
neo[0] = (0 , 255, 0)
try:
# Get the 'rate' feed from Adafruit IO
rate_feed = io.get_feed("rate")
except AdafruitIO_RequestError:
# If no 'rate' feed exists, create one
rate_feed = io.create_new_feed("rate")
# test of rate
# print("IO rate value =", rate)
# Delays the program one second
time.sleep(1)
# Reduces total time by one second
total_seconds -= 1
print("countdown at ", total_seconds)
# add code to check if rate was changed
print("Bzzzt! The countdown is at zero seconds!")
neo.brightness = 0.2
neo[0] = (150, 150, 0)
time.sleep(3)
neo.brightness = 0.2
neo[0] = (0 , 255, 0)
# ]Metadata
Metadata
Assignees
Labels
No labels