|
| 1 | +""" |
| 2 | +This example will access an API, grab a number like hackaday skulls, github |
| 3 | +stars, price of bitcoin, twitter followers... if you can find something that |
| 4 | +spits out JSON data, we can display it! |
| 5 | +""" |
| 6 | +import gc |
| 7 | +import time |
| 8 | +import board |
| 9 | +import busio |
| 10 | +from digitalio import DigitalInOut |
| 11 | +import adafruit_espatcontrol |
| 12 | +from adafruit_ht16k33 import segments |
| 13 | +import neopixel |
| 14 | +import ujson |
| 15 | + |
| 16 | +# Get wifi details and more from a settings.py file |
| 17 | +try: |
| 18 | + from settings import settings |
| 19 | +except ImportError: |
| 20 | + print("WiFi settings are kept in settings.py, please add them there!") |
| 21 | + raise |
| 22 | + |
| 23 | +# CONFIGURATION |
| 24 | +PLAY_SOUND_ON_CHANGE = False |
| 25 | +NEOPIXELS_ON_CHANGE = False |
| 26 | +TIME_BETWEEN_QUERY = 60 # in seconds |
| 27 | + |
| 28 | +# Some data sources and JSON locations to try out |
| 29 | + |
| 30 | +# Bitcoin value in USD |
| 31 | +DATA_SOURCE = "http://api.coindesk.com/v1/bpi/currentprice.json" |
| 32 | +DATA_LOCATION = ["bpi", "USD", "rate_float"] |
| 33 | + |
| 34 | +# Github stars! You can query 1ce a minute without an API key token |
| 35 | +#DATA_SOURCE = "https://api.github.com/repos/adafruit/circuitpython" |
| 36 | +#if 'github_token' in settings: |
| 37 | +# DATA_SOURCE += "?access_token="+settings['github_token'] |
| 38 | +#DATA_LOCATION = ["stargazers_count"] |
| 39 | + |
| 40 | +# Youtube stats |
| 41 | +#CHANNEL_ID = "UCpOlOeQjj7EsVnDh3zuCgsA" # this isn't a secret but you have to look it up |
| 42 | +#DATA_SOURCE = "https://www.googleapis.com/youtube/v3/channels/?part=statistics&id=" \ |
| 43 | +# + CHANNEL_ID +"&key="+settings['youtube_token'] |
| 44 | +# try also 'viewCount' or 'videoCount |
| 45 | +#DATA_LOCATION = ["items", 0, "statistics", "subscriberCount"] |
| 46 | + |
| 47 | + |
| 48 | +# Subreddit subscribers |
| 49 | +#DATA_SOURCE = "https://www.reddit.com/r/circuitpython/about.json" |
| 50 | +#DATA_LOCATION = ["data", "subscribers"] |
| 51 | + |
| 52 | +# Hackaday Skulls (likes), requires an API key |
| 53 | +#DATA_SOURCE = "https://api.hackaday.io/v1/projects/1340?api_key="+settings['hackaday_token'] |
| 54 | +#DATA_LOCATION = ["skulls"] |
| 55 | + |
| 56 | +# Twitter followers |
| 57 | +#DATA_SOURCE = "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?" + \ |
| 58 | +#"screen_names=adafruit" |
| 59 | +#DATA_LOCATION = [0, "followers_count"] |
| 60 | + |
| 61 | +uart = busio.UART(board.TX, board.RX, timeout=0.1) |
| 62 | +resetpin = DigitalInOut(board.D5) |
| 63 | +rtspin = DigitalInOut(board.D9) |
| 64 | + |
| 65 | +# Create the connection to the co-processor and reset |
| 66 | +esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, run_baudrate=921600, |
| 67 | + reset_pin=resetpin, |
| 68 | + rts_pin=rtspin, debug=True) |
| 69 | +esp.hard_reset() |
| 70 | + |
| 71 | +# Create the I2C interface. |
| 72 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 73 | +# Attach a 7 segment display and display -'s so we know its not live yet |
| 74 | +display = segments.Seg7x4(i2c) |
| 75 | +display.print('----') |
| 76 | + |
| 77 | +# neopixels |
| 78 | +if NEOPIXELS_ON_CHANGE: |
| 79 | + pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.4, pixel_order=(1, 0, 2, 3)) |
| 80 | + pixels.fill(0) |
| 81 | + |
| 82 | +# music! |
| 83 | +if PLAY_SOUND_ON_CHANGE: |
| 84 | + import audioio |
| 85 | + wave_file = open("coin.wav", "rb") |
| 86 | + wave = audioio.WaveFile(wave_file) |
| 87 | + |
| 88 | +# we'll save the value in question |
| 89 | +last_value = value = None |
| 90 | +the_time = None |
| 91 | +times = 0 |
| 92 | + |
| 93 | +def chime_light(): |
| 94 | + """Light up LEDs and play a tune""" |
| 95 | + if NEOPIXELS_ON_CHANGE: |
| 96 | + for i in range(0, 100, 10): |
| 97 | + pixels.fill((i, i, i)) |
| 98 | + if PLAY_SOUND_ON_CHANGE: |
| 99 | + with audioio.AudioOut(board.A0) as audio: |
| 100 | + audio.play(wave) |
| 101 | + while audio.playing: |
| 102 | + pass |
| 103 | + if NEOPIXELS_ON_CHANGE: |
| 104 | + for i in range(100, 0, -10): |
| 105 | + pixels.fill((i, i, i)) |
| 106 | + pixels.fill(0) |
| 107 | + |
| 108 | +def get_value(response, location): |
| 109 | + """Extract a value from a json object, based on the path in 'location'""" |
| 110 | + try: |
| 111 | + print("Parsing JSON response...", end='') |
| 112 | + json = ujson.loads(response) |
| 113 | + print("parsed OK!") |
| 114 | + for x in location: |
| 115 | + json = json[x] |
| 116 | + return json |
| 117 | + except ValueError: |
| 118 | + print("Failed to parse json, retrying") |
| 119 | + return None |
| 120 | + |
| 121 | +while True: |
| 122 | + try: |
| 123 | + while not esp.is_connected: |
| 124 | + # settings dictionary must contain 'ssid' and 'password' at a minimum |
| 125 | + esp.connect(settings) |
| 126 | + # great, lets get the data |
| 127 | + # get the time |
| 128 | + the_time = esp.sntp_time |
| 129 | + |
| 130 | + print("Retrieving data source...", end='') |
| 131 | + header, body = esp.request_url(DATA_SOURCE) |
| 132 | + print("Reply is OK!") |
| 133 | + except (RuntimeError, adafruit_espatcontrol.OKError) as e: |
| 134 | + print("Failed to get data, retrying\n", e) |
| 135 | + continue |
| 136 | + #print('-'*40, "Size: ", len(body)) |
| 137 | + #print(str(body, 'utf-8')) |
| 138 | + #print('-'*40) |
| 139 | + value = get_value(body, DATA_LOCATION) |
| 140 | + if not value: |
| 141 | + continue |
| 142 | + print(times, the_time, "value:", value) |
| 143 | + display.print(int(value)) |
| 144 | + |
| 145 | + if last_value != value: |
| 146 | + chime_light() # animate the neopixels |
| 147 | + last_value = value |
| 148 | + times += 1 |
| 149 | + # normally we wouldn't have to do this, but we get bad fragments |
| 150 | + header = body = None |
| 151 | + gc.collect() |
| 152 | + print(gc.mem_free()) # pylint: disable=no-member |
| 153 | + time.sleep(TIME_BETWEEN_QUERY) |
0 commit comments