Skip to content

Instantly share code, notes, and snippets.

@daemonhorn
Created July 16, 2023 19:56
Show Gist options
  • Save daemonhorn/18a7f3019bec2fd58e4ff4ad748f7dbf to your computer and use it in GitHub Desktop.
Save daemonhorn/18a7f3019bec2fd58e4ff4ad748f7dbf to your computer and use it in GitHub Desktop.
Inkplate10_Example_NOAA_Weather
import network
import time
from soldered_inkplate10 import Inkplate
ssid = "My_SSID"
password = "My_Pass"
# Function which connects to WiFi
# More info here: https://docs.micropython.org/en/latest/esp8266/tutorial/network_basics.html
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print("connecting to network...")
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print("network config:", sta_if.ifconfig())
# Does a HTTP GET request
# More info here: https://docs.micropython.org/en/latest/esp8266/tutorial/network_tcp.html
# See ussl for details on the ssl configuration and wrapper function for sockets
# https://docs.micropython.org/en/latest/library/ssl.html
def http_get(url):
import usocket
import ussl
res = ""
scheme, _, host, path = url.split("/", 3)
#print("scheme: %s, host: %s, path: %s" % (scheme, host, path))
print("url: %s" % url)
if scheme == 'https:':
port = 443
if scheme == 'http:':
port = 80
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
ai = ai[0]
s = usocket.socket(ai[0], ai[1], ai[2])
s.connect(ai[-1])
if scheme == "https:":
s = ussl.wrap_socket(s, server_hostname=host)
buffer = "GET /%s HTTP/1.0\r\n" % (path)
buffer += "Host: %s\r\n" % (host)
buffer += "User-Agent: micropython/1.2.0 exampleweather esp32\r\n"
buffer += "Accept: application/geo+json\r\n"
# HTTP requests must end in an extra CRLF (aka \r\n)
buffer += "\r\n"
# print("Debug HTTP REQUEST: \r\n%s" % (buffer))
s.write(bytes(buffer, "utf8"))
while True:
data = s.read(1000)
#print("data: %s" % str(data))
if data:
res += str(data, "utf8")
else:
break
s.close()
return res
# Calling functions defined above
do_connect()
# Do a GET request to the micropython test page
#print ("Debug: Starting fetching data")
#response = http_get("http://micropython.org/ks/test.html")
response = http_get("https://api.weather.gov/gridpoints/LWX/80%2C76/forecast")
print ("Debug: Finished fetching data")
import json
jsondata = ""
for x in response.split("\n"):
if (x.find("{") >= 0 or x.find("}") >= 0 or x.find("\":") >= 0 or x.find("]") >= 0 or x.find("[") >= 0):
jsondata += x
#print("MATCH Line: %s" % x)
#else:
# print("NOMATCH Line: %s" % x)
#print("jsondata: BEGIN---\r\n%s\r\nEND---" % jsondata)
jsonelements = json.loads(jsondata)
#print(jsonelements["properties"])
jsonproperties = jsonelements["properties"]
#print("Updated: %s" % jsonproperties["updated"])
#print("Detailed Forecast: %s" % jsonproperties["periods"][1]["detailedForecast"])
displaytext = "Updated: %s\n\n" % jsonproperties["updated"]
#Period 1 is Current period, 2 is +12 hours, etc.
#dict has members:
# "name": "Saturday Night",
# "startTime": "2023-07-22T18:00:00-04:00",
# "endTime": "2023-07-23T06:00:00-04:00",
# "isDaytime": false,
# "temperature": 65,
# "temperatureUnit": "F",
# "temperatureTrend": null,
# "probabilityOfPrecipitation": {
# "unitCode": "wmoUnit:percent",
# "value": null },
# "dewpoint": {
# "unitCode": "wmoUnit:degC",
# "value": 17.222222222222221 },
# "relativeHumidity": {
# "unitCode": "wmoUnit:percent",
# "value": 84 },
# "windSpeed": "6 to 10 mph",
# "windDirection": "NW",
# "icon": "https://api.weather.gov/icons/land/night/tsra_hi/few?size=medium",
# "shortForecast": "Slight Chance Showers And Thunderstorms then Mostly Clear",
# "detailedForecast": "A slight chance of showers and thunderstorms before 8pm. Mostly clear, with a low around 65."
displaytext += "Period: %s\n" % jsonproperties["periods"][1]["name"]
displaytext += "Temperature: %s %s\n" % (jsonproperties["periods"][1]["temperature"],jsonproperties["periods"][1]["temperatureUnit"])
displaytext += "Humidity: %s\n" % jsonproperties["periods"][1]["relativeHumidity"]["value"]
displaytext += "Wind: %s at %s\n" % (jsonproperties["periods"][1]["windDirection"],jsonproperties["periods"][1]["windSpeed"])
displaytext += "Forecast: %s\n\n" % jsonproperties["periods"][1]["shortForecast"]
displaytext += "Period: %s\n" % jsonproperties["periods"][2]["name"]
displaytext += "Temperature: %s %s\n" % (jsonproperties["periods"][2]["temperature"],jsonproperties["periods"][2]["temperatureUnit"])
displaytext += "Humidity: %s\n" % jsonproperties["periods"][2]["relativeHumidity"]["value"]
displaytext += "Wind: %s at %s\n" % (jsonproperties["periods"][2]["windDirection"],jsonproperties["periods"][2]["windSpeed"])
displaytext += "Forecast: %s\n\n" % jsonproperties["periods"][2]["shortForecast"]
displaytext += "Period: %s\n" % jsonproperties["periods"][3]["name"]
displaytext += "Temperature: %s %s\n" % (jsonproperties["periods"][3]["temperature"],jsonproperties["periods"][3]["temperatureUnit"])
displaytext += "Humidity: %s\n" % jsonproperties["periods"][3]["relativeHumidity"]["value"]
displaytext += "Wind: %s at %s\n" % (jsonproperties["periods"][3]["windDirection"],jsonproperties["periods"][3]["windSpeed"])
displaytext += "Forecast: %s\n\n" % jsonproperties["periods"][3]["shortForecast"]
# Initialise our Inkplate object
display = Inkplate(Inkplate.INKPLATE_1BIT)
display.begin()
# Get display temperature from built-in sensor, requires calling display.display() first for some reason
display.display()
temperature_C = display.readTemperature()
temperature_F = (temperature_C * 1.8) + 32
inside_temp = str(temperature_F)
#print("inside temp: %s" % inside_temp)
displaytext += "Current Inside Temperature: %s F\n" % inside_temp
#rotation int 0 = none 1 = 90deg clockwise rotation, 2 = 180deg, 3 = 270deg
display.setRotation(0)
# GFX draw functions do nothing if drawn outside known range, and don't respect rotation, so you can muck with width/height
#display.GFX.height = 1024
#display.GFX.width = 758
display.setTextSize(3)
# Print response in lines
cnt = 0
for x in displaytext.split("\n"):
display.printText(
10, 28 + cnt, x.upper()
) # Default font has only upper case letters
cnt += 28
#display.printText(10, 28, displaytext)
#display.setTextSize(1)
#display.printText(10,500,jsonproperties["periods"][1]["detailedForecast"])
# Display image from buffer
display.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment