Skip to content

Commit

Permalink
Merge branch 'pimoroni:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroshift authored Nov 9, 2024
2 parents c719ca7 + 2c62557 commit fbe2070
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 62 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/release-zip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ jobs:

env:
RELEASE_FILE: enviro-${{github.event.release.tag_name || github.sha}}
FIRMWARE_NAME: pimoroni-picow_enviro-v1.19.10-micropython
FIRMWARE_URL: https://github.com/pimoroni/pimoroni-pico/releases/download/v1.19.10/
FIRMWARE_NAME: pimoroni-enviro-v1.22.2-micropython
FIRMWARE_URL: https://github.com/pimoroni/pimoroni-pico/releases/download/v1.22.2

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
path: enviro
submodules: true

- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
repository: gadgetoid/dir2uf2
ref: v0.0.1
Expand All @@ -31,31 +31,31 @@ jobs:
- name: Prepare repository
shell: bash
run: |
python3 -m pip install littlefs-python
python3 -m pip install littlefs-python==0.4.0
wget ${{env.FIRMWARE_URL}}/${{env.FIRMWARE_NAME}}.uf2
./dir2uf2/dir2uf2 --append-to ${{env.FIRMWARE_NAME}}.uf2 --manifest enviro/uf2-manifest.txt --filename ${{env.RELEASE_FILE}}.uf2 enviro/
rm -rf enviro/.git*
rm -rf enviro/phew/.git*
- uses: vimtor/action-zip@v1
- uses: vimtor/action-zip@v1.2
with:
files: enviro/
dest: ${{env.RELEASE_FILE}}.zip

- name: Store .zip as artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ${{env.RELEASE_FILE}}
path: enviro/

- name: Store filesystem .uf2 as artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ${{env.RELEASE_FILE}}-filesystem-only
path: ${{env.RELEASE_FILE}}.uf2

- name: Store full .uf2 as artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ${{env.FIRMWARE_NAME}}-${{env.RELEASE_FILE}}.uf2
path: ${{env.FIRMWARE_NAME}}-${{env.RELEASE_FILE}}.uf2
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
# Enviro MicroPython firmware <!-- omit in toc -->

- [New firmware!](#new-firmware)
- [About Enviro](#about-enviro)
- [Powering Enviro boards](#powering-enviro-boards)
- [Supported products](#supported-products)
- [Supported endpoints](#supported-endpoints)
- [Documentation](#documentation)

## New firmware!

v.0.0.9 is an **alpha** release of the firmware which may contain bugs and other instabilities. Thanks for your continued feedback and bug reports - we'll get it fixed up as soon as we can :)

Download the alpha firmware here: https://github.com/pimoroni/enviro/releases/tag/v0.0.9

If you want to return to the shipping firmware you can re-flash your Enviro with v.0.0.2: https://github.com/pimoroni/enviro/releases/tag/v0.0.2

## About Enviro

Our Enviro range of boards offer a wide array of environmental sensing and data logging functionality. They are designed to be setup in location for months at a time and take regular measurements.
Expand Down Expand Up @@ -46,7 +37,6 @@ You can use 3xAA or 3xAAA (either alkaline or NiMH), a single cell LiPo battery,
- Enviro Grow ([store link](https://shop.pimoroni.com/products/enviro-grow))
- Enviro Weather ([store link](https://shop.pimoroni.com/products/enviro-weather))
- Enviro Urban ([store link](https://shop.pimoroni.com/products/enviro-urban))
- Enviro Camera ([store link](https://shop.pimoroni.com/products/enviro-camera)) - coming soon!

## Supported endpoints
- [Adafruit IO](documentation/destinations/adafruit-io.md)
Expand Down
2 changes: 1 addition & 1 deletion documentation/upgrading-firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Hit reset and your board should resume logging (or enter provisioning if that's

### Starting from scratch

Need to delete everything from your board and start from scratch? You can do that by downloading [this special .uf2 file](https://www.raspberrypi.org/documentation/pico/getting-started/static/6f6f31460c258138bd33cc96ddd76b91/flash_nuke.uf2) and copying it to Enviro whilst it's in bootloader/DFU mode. Once you've done that, you'll need to follow all the steps in this guide again.
Need to delete everything from your board and start from scratch? You can do that by downloading [this special .uf2 file](https://datasheets.raspberrypi.com/soft/flash_nuke.uf2) and copying it to Enviro whilst it's in bootloader/DFU mode. Once you've done that, you'll need to follow all the steps in this guide again.

⚠ Beware! Doing this will delete **everything** - including all your logged data and configuration details.

145 changes: 104 additions & 41 deletions enviro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,56 +153,110 @@ def stop_activity_led():
print(" - -- ---- -----=--==--=== hey enviro, let's go! ===--==--=----- ---- -- - ")
print("")

def reconnect_wifi(ssid, password, country, hostname=None):
import time
import network
import math
import rp2
import ubinascii

start_ms = time.ticks_ms()

# Set country
rp2.country(country)

# Set hostname
if hostname is None:
hostname = f"EnviroW-{helpers.uid()[-4:]}"
network.hostname(hostname)

# Reference: https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf
CYW43_LINK_DOWN = 0
CYW43_LINK_JOIN = 1
CYW43_LINK_NOIP = 2
CYW43_LINK_UP = 3
CYW43_LINK_FAIL = -1
CYW43_LINK_NONET = -2
CYW43_LINK_BADAUTH = -3

status_names = {
CYW43_LINK_DOWN: "Link is down",
CYW43_LINK_JOIN: "Connected to wifi",
CYW43_LINK_NOIP: "Connected to wifi, but no IP address",
CYW43_LINK_UP: "Connect to wifi with an IP address",
CYW43_LINK_FAIL: "Connection failed",
CYW43_LINK_NONET: "No matching SSID found (could be out of range, or down)",
CYW43_LINK_BADAUTH: "Authenticatation failure",
}

import network # TODO this was removed from 0.0.8
def connect_to_wifi():
""" TODO what it was changed to
if phew.is_connected_to_wifi():
logging.info(f"> already connected to wifi")
return True
"""

wifi_ssid = config.wifi_ssid
wifi_password = config.wifi_password

logging.info(f"> connecting to wifi network '{wifi_ssid}'")
""" TODO what it was changed to
ip = phew.connect_to_wifi(wifi_ssid, wifi_password, timeout_seconds=30)
wlan = network.WLAN(network.STA_IF)

if not ip:
logging.error(f"! failed to connect to wireless network {wifi_ssid}")
def dump_status():
status = wlan.status()
logging.info(f"> active: {1 if wlan.active() else 0}, status: {status} ({status_names[status]})")
return status

# Return True on expected status, exception on error status (negative) and False on timeout
def wait_status(expected_status, *, timeout=10, tick_sleep=0.5):
for i in range(math.ceil(timeout / tick_sleep)):
time.sleep(tick_sleep)
status = dump_status()
if status == expected_status:
return True
if status < 0:
raise Exception(status_names[status])
return False

logging.info(" - ip address: ", ip)
"""
import rp2
rp2.country("GB")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(wifi_ssid, wifi_password)
# Disable power saving mode if on USB power
if vbus_present:
wlan.config(pm=0xa11140)

start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < 30000:
if wlan.status() < 0 or wlan.status() >= 3:
break
time.sleep(0.5)
# Print MAC
mac = ubinascii.hexlify(wlan.config('mac'),':').decode()
logging.info("> MAC: " + mac)

# Disconnect when necessary
status = dump_status()
if status >= CYW43_LINK_JOIN and status < CYW43_LINK_UP:
logging.info("> Disconnecting...")
wlan.disconnect()
try:
wait_status(CYW43_LINK_DOWN)
except Exception as x:
raise Exception(f"Failed to disconnect: {x}")
logging.info("> Ready for connection!")

# Connect to our AP
logging.info(f"> Connecting to SSID {ssid} (password: {password})...")
wlan.connect(ssid, password)
try:
wait_status(CYW43_LINK_UP)
except Exception as x:
raise Exception(f"Failed to connect to SSID {ssid} (password: {password}): {x}")
logging.info("> Connected successfully!")

seconds_to_connect = int(time.ticks_diff(time.ticks_ms(), start) / 1000)
ip, subnet, gateway, dns = wlan.ifconfig()
logging.info(f"> IP: {ip}, Subnet: {subnet}, Gateway: {gateway}, DNS: {dns}")

elapsed_ms = time.ticks_ms() - start_ms
logging.info(f"> Elapsed: {elapsed_ms}ms")
return elapsed_ms

if wlan.status() != 3:
logging.error(f"! failed to connect to wireless network {wifi_ssid}")
def connect_to_wifi():
try:
logging.info(f"> connecting to wifi network '{config.wifi_ssid}'")
elapsed_ms = reconnect_wifi(config.wifi_ssid, config.wifi_password, config.wifi_country)
# a slow connection time will drain the battery faster and may
# indicate a poor quality connection
seconds_to_connect = elapsed_ms / 1000
if seconds_to_connect > 5:
logging.warn(" - took", seconds_to_connect, "seconds to connect to wifi")
return True
except Exception as x:
logging.error(f"! {x}")
return False

# a slow connection time will drain the battery faster and may
# indicate a poor quality connection
if seconds_to_connect > 5:
logging.warn(" - took", seconds_to_connect, "seconds to connect to wifi")

ip_address = wlan.ifconfig()[0]
logging.info(" - ip address: ", ip_address)

return True

# log the error, blink the warning led, and go back to sleep
def halt(message):
logging.error(message)
Expand Down Expand Up @@ -367,7 +421,7 @@ def get_sensor_readings():
def save_reading(readings):
# open todays reading file and save readings
helpers.mkdir_safe("readings")
readings_filename = f"readings/{helpers.datetime_file_string()}.txt"
readings_filename = f"readings/{helpers.date_string()}.csv"
new_file = not helpers.file_exists(readings_filename)
with open(readings_filename, "a") as f:
if new_file:
Expand Down Expand Up @@ -464,6 +518,15 @@ def upload_readings():
logging.error(f"! cannot find destination {destination}")
return False

finally:
# Disconnect wifi
import network
logging.info("> Disconnecting wireless after upload")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.active(False)

return True

def startup():
Expand Down
5 changes: 5 additions & 0 deletions enviro/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def add_missing_config_settings():
warn_missing_config_setting("usb_power_temperature_offset")
config.usb_power_temperature_offset = DEFAULT_USB_POWER_TEMPERATURE_OFFSET

try:
config.wifi_country
except AttributeError:
warn_missing_config_setting("wifi_country")
config.wifi_country = "GB"

def warn_missing_config_setting(setting):
logging.warn(f"> config setting '{setting}' missing, please add it to config.py")
1 change: 1 addition & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# network access details
wifi_ssid = None
wifi_password = None
wifi_country = "GB"

# how often to wake up and take a reading (in minutes)
reading_frequency = 15
Expand Down
2 changes: 1 addition & 1 deletion enviro/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# version
ENVIRO_VERSION = "0.0.9"
ENVIRO_VERSION = "0.0.10"

# modules
ENVIRO_UNKNOWN = None
Expand Down

0 comments on commit fbe2070

Please sign in to comment.