Skip to content

Instantly share code, notes, and snippets.

@Kartoffel
Created August 17, 2022 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kartoffel/c9275059c1404b3b980dab1d38a1e4cc to your computer and use it in GitHub Desktop.
Save Kartoffel/c9275059c1404b3b980dab1d38a1e4cc to your computer and use it in GitHub Desktop.
Proof of concept to get day-ahead electricity prices on the SHA2017 badge (old firmware)
# Quick POC, for the SHA2017 badge on the last firmware before the ESP32-platform
# Test through serial monitor, 115200 baud
# 1. Press some buttons on the badge to wake it up
# 2. Spam ctrl+C in the terminal until you get an empty prompt
# 3. Type `import shell` to get a blank display and clean shell
# 4. Press ctrl+E to enter paste mode
# 5. Paste the first 14 lines of the below code, press ctrl+D to exit paste mode and execute
# 6. Repeat above two steps for lines 15-27 and 29-41
# Note: paste mode doesn't work well for large paste buffers, use mpfshell or similar to push whole files to the badge
import badge, easywifi, ugfx, gc, easydraw, deepsleep, time, math, machine
import urequests as requests
ugfx.set_lut(ugfx.LUT_FASTER)
ugfx.clear(ugfx.WHITE)
easywifi.enable(True)
easydraw.msg("hoi bertrik")
if easywifi.state==False:
easydraw.msg("Unable to connect to network!","FAILURE")
badge.eink_busy_wait()
machine.deepsleep(10000) # deep sleep 10s (resets afterwards)
easydraw.msg("Downloading JSON...")
try:
data = requests.get("http://stofradar.nl:9001/electricity/price")
content = data.text
data.close()
gc.collect()
except:
easydraw.msg("Failed to obtain json!", "FAILURE")
badge.eink_busy_wait()
machine.deepsleep(10000)
easydraw.msg("No crash yet, data obtained?")
easydraw.msg("Trying to parse..")
import ujson
try:
result = ujson.loads(content)
except:
easydraw.msg("Invalid response or could not parse json","FAILURE")
badge.eink_busy_wait()
machine.deepsleep(10000)
gc.collect()
print(result['day-ahead'])
easydraw.msg("Success! See serial terminal for day-ahead results")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment