|
| 1 | +# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import ssl |
| 7 | +import time |
| 8 | +import wifi |
| 9 | +import socketpool |
| 10 | +import board |
| 11 | +import busio |
| 12 | +import microcontroller |
| 13 | +import adafruit_requests |
| 14 | +import neopixel |
| 15 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 16 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 17 | + |
| 18 | +pixel_pin = board.NEOPIXEL |
| 19 | +num_pixels = 1 |
| 20 | + |
| 21 | +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.05, auto_write=False) |
| 22 | +pixels.fill((255, 255, 0)) |
| 23 | +pixels.show() |
| 24 | + |
| 25 | +try: |
| 26 | + wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) |
| 27 | +except Exception as e: # pylint: disable=broad-except |
| 28 | + pixels.fill((100, 100, 100)) |
| 29 | + pixels.show() |
| 30 | + print("Error:\n", str(e)) |
| 31 | + print("Resetting microcontroller in 5 seconds") |
| 32 | + time.sleep(5) |
| 33 | + microcontroller.reset() |
| 34 | + |
| 35 | +aio_username = os.getenv('aio_username') |
| 36 | +aio_key = os.getenv('aio_key') |
| 37 | + |
| 38 | +try: |
| 39 | + pool = socketpool.SocketPool(wifi.radio) |
| 40 | + requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 41 | + # Initialize an Adafruit IO HTTP API object |
| 42 | + io = IO_HTTP(aio_username, aio_key, requests) |
| 43 | + print("connected to io") |
| 44 | +except Exception as e: # pylint: disable=broad-except |
| 45 | + pixels.fill((100, 100, 100)) |
| 46 | + pixels.show() |
| 47 | + print("Error:\n", str(e)) |
| 48 | + print("Resetting microcontroller in 5 seconds") |
| 49 | + time.sleep(5) |
| 50 | + microcontroller.reset() |
| 51 | + |
| 52 | +try: |
| 53 | +# get feed |
| 54 | + ikea_pm25 = io.get_feed("ikeapm25") |
| 55 | +except AdafruitIO_RequestError: |
| 56 | +# if no feed exists, create one |
| 57 | + ikea_pm25 = io.create_new_feed("ikeapm25") |
| 58 | + |
| 59 | +uart = busio.UART(board.TX, board.RX, baudrate=9600) |
| 60 | + |
| 61 | +measurements = [0, 0, 0, 0, 0] |
| 62 | +measurement_idx = 0 |
| 63 | + |
| 64 | +def valid_header(d): |
| 65 | + headerValid = (d[0] == 0x16 and d[1] == 0x11 and d[2] == 0x0B) |
| 66 | + # debug |
| 67 | + # if not headerValid: |
| 68 | + # print("msg without header") |
| 69 | + return headerValid |
| 70 | + |
| 71 | +start_read = False |
| 72 | +clock = ticks_ms() |
| 73 | +pixels.fill((0, 255, 0)) |
| 74 | +pixels.show() |
| 75 | + |
| 76 | +io_time = 60000 |
| 77 | + |
| 78 | +while True: |
| 79 | + try: |
| 80 | + data = uart.read(32) # read up to 32 bytes |
| 81 | + #print(data) # this is a bytearray type |
| 82 | + time.sleep(0.01) |
| 83 | + if ticks_diff(ticks_ms(), clock) >= io_time: |
| 84 | + pixels.fill((0, 0, 255)) |
| 85 | + pixels.show() |
| 86 | + io_data = measurements[0] |
| 87 | + if io_data != 0: |
| 88 | + io.send_data(ikea_pm25["key"], io_data) |
| 89 | + print(f"sent {io_data} to {ikea_pm25['key']} feed") |
| 90 | + time.sleep(1) |
| 91 | + clock = ticks_add(clock, io_time) |
| 92 | + pixels.fill((0, 0, 0)) |
| 93 | + pixels.show() |
| 94 | + if data is not None: |
| 95 | + v = valid_header(data) |
| 96 | + if v is True: |
| 97 | + measurement_idx = 0 |
| 98 | + start_read = True |
| 99 | + if start_read is True: |
| 100 | + pixels.fill((255, 0, 0)) |
| 101 | + pixels.show() |
| 102 | + pm25 = (data[5] << 8) | data[6] |
| 103 | + measurements[measurement_idx] = pm25 |
| 104 | + if measurement_idx == 4: |
| 105 | + start_read = False |
| 106 | + measurement_idx = (measurement_idx + 1) % 5 |
| 107 | + print(pm25) |
| 108 | + print(measurements) |
| 109 | + else: |
| 110 | + pixels.fill((0, 255, 0)) |
| 111 | + pixels.show() |
| 112 | + except Exception as e: # pylint: disable=broad-except |
| 113 | + print("Error:\n", str(e)) |
| 114 | + print("Resetting microcontroller in 5 seconds") |
| 115 | + time.sleep(5) |
| 116 | + microcontroller.reset() |
0 commit comments