|
| 1 | +""" |
| 2 | +This example will query ThingSpeak channel 1417 "CheerLights" and display the |
| 3 | +color on a NeoPixel ring or strip |
| 4 | +""" |
| 5 | +import gc |
| 6 | +import time |
| 7 | +import board |
| 8 | +import busio |
| 9 | +from digitalio import DigitalInOut |
| 10 | +from Adafruit_CircuitPython_ESP_ATcontrol import adafruit_espatcontrol |
| 11 | +import neopixel |
| 12 | +import ujson |
| 13 | +import adafruit_fancyled.adafruit_fancyled as fancy |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +# Get wifi details and more from a settings.py file |
| 18 | +try: |
| 19 | + from settings import settings |
| 20 | +except ImportError: |
| 21 | + print("WiFi settings are kept in settings.py, please add them there!") |
| 22 | + raise |
| 23 | + |
| 24 | +# CONFIGURATION |
| 25 | +TIME_BETWEEN_QUERY = 10 # in seconds |
| 26 | + |
| 27 | +# Cheerlights! |
| 28 | +DATA_SOURCE = "http://api.thingspeak.com/channels/1417/feeds.json?results=1" |
| 29 | +DATA_LOCATION = ["feeds", 0, "field2"] |
| 30 | + |
| 31 | +uart = busio.UART(board.TX, board.RX, timeout=0.1) |
| 32 | +resetpin = DigitalInOut(board.D5) |
| 33 | +rtspin = DigitalInOut(board.D9) |
| 34 | + |
| 35 | +# Create the connection to the co-processor and reset |
| 36 | +esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, run_baudrate=460800, |
| 37 | + reset_pin=resetpin, |
| 38 | + rts_pin=rtspin, debug=True) |
| 39 | +esp.hard_reset() |
| 40 | + |
| 41 | +# neopixels |
| 42 | +pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3) |
| 43 | +pixels.fill(0) |
| 44 | +builtin = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1) |
| 45 | +builtin[0] = 0 |
| 46 | + |
| 47 | +# we'll save the value in question |
| 48 | +last_value = value = None |
| 49 | +the_time = None |
| 50 | +times = 0 |
| 51 | + |
| 52 | +def get_value(response, location): |
| 53 | + """Extract a value from a json object, based on the path in 'location'""" |
| 54 | + try: |
| 55 | + print("Parsing JSON response...", end='') |
| 56 | + json = ujson.loads(response) |
| 57 | + print("parsed OK!") |
| 58 | + for x in location: |
| 59 | + json = json[x] |
| 60 | + return json |
| 61 | + except ValueError: |
| 62 | + print("Failed to parse json, retrying") |
| 63 | + return None |
| 64 | + |
| 65 | +while True: |
| 66 | + try: |
| 67 | + while not esp.is_connected: |
| 68 | + builtin[0] = (100, 0, 0) |
| 69 | + # settings dictionary must contain 'ssid' and 'password' at a minimum |
| 70 | + esp.connect(settings) |
| 71 | + builtin[0] = (0, 100, 0) |
| 72 | + # great, lets get the data |
| 73 | + print("Retrieving data source...", end='') |
| 74 | + builtin[0] = (100, 100, 0) |
| 75 | + header, body = esp.request_url(DATA_SOURCE) |
| 76 | + builtin[0] = (0, 0, 100) |
| 77 | + print("Reply is OK!") |
| 78 | + except (RuntimeError, adafruit_espatcontrol.OKError) as e: |
| 79 | + print("Failed to get data, retrying\n", e) |
| 80 | + continue |
| 81 | + print('-'*40, "Size: ", len(body)) |
| 82 | + print(str(body, 'utf-8')) |
| 83 | + print('-'*40) |
| 84 | + # For mystery reasons, there's two numbers before and after the json data |
| 85 | + lines = body.split(b'\r\n') # so split into lines |
| 86 | + value = get_value(lines[1], DATA_LOCATION) # an get the middle chunk |
| 87 | + builtin[0] = (100, 100, 100) |
| 88 | + if not value: |
| 89 | + continue |
| 90 | + print(times, the_time, "value:", value) |
| 91 | + |
| 92 | + if last_value != value: |
| 93 | + color = int(value[1:],16) |
| 94 | + red = color >> 16 & 0xFF |
| 95 | + green = color >> 8 & 0xFF |
| 96 | + blue = color& 0xFF |
| 97 | + gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack() |
| 98 | + |
| 99 | + pixels.fill(gamma_corrected) |
| 100 | + last_value = value |
| 101 | + times += 1 |
| 102 | + |
| 103 | + # normally we wouldn't have to do this, but we get bad fragments |
| 104 | + header = body = None |
| 105 | + gc.collect() |
| 106 | + print(gc.mem_free()) # pylint: disable=no-member |
| 107 | + time.sleep(TIME_BETWEEN_QUERY) |
0 commit comments