Last active
March 30, 2021 15:46
-
-
Save imliubo/1ed0953c94ab25e9e93e5cb891a36d5c to your computer and use it in GitHub Desktop.
M5Core2 display images through HTTP request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urequests | |
import time | |
import wifiCfg | |
from m5stack import lv, rtc | |
wifiCfg.auto_connect() | |
ticks_ms = 0 | |
old_tick = 0 | |
scr = lv.scr_act() | |
scr.clean() | |
style = lv.style_t() | |
style.init() | |
img = lv.img(scr) | |
time_label = lv.label(scr) | |
style.set_text_color(lv.STATE.DEFAULT, lv.color_hex(0xff0000)) | |
time_label.add_style(time_label.PART.MAIN, style) | |
style.set_text_font(lv.STATE.DEFAULT, lv.font_montserrat_20) | |
time_label.add_style(time_label.PART.MAIN, style) | |
while True: | |
if wifiCfg.is_connected(): | |
ticks_ms = time.ticks_ms() | |
if time.ticks_diff(ticks_ms, old_tick) > 40000: | |
old_tick = ticks_ms | |
try: | |
# Only support PNG format image | |
req = urequests.get("http://xxxxxx.xxxxxx.com/xxxxxxxx.png") | |
if (req.status_code) == 200: | |
img_dsc = lv.img_dsc_t({"data_size": len(req.content), | |
"data": req.content}) | |
img.set_src(img_dsc) | |
rtc_time = list(rtc.printRTCtime()) | |
time_label.set_text(''.join(rtc_time)) | |
time_label.align(None, lv.ALIGN.IN_TOP_MID, 0, 0) | |
# Save to SD card. | |
# rtc_time[10] = '-' | |
# rtc_time[13] = '-' | |
# rtc_time[16] = '-' | |
# file_name = "/sd/timer-cam/" + ''.join(rtc_time[8:19]) + '.png' | |
# img_save = open(file_name, "wb") | |
# img_save.write(req.content) | |
# img_save.close() | |
del img_dsc | |
except (KeyboardInterrupt, Exception) as e: | |
print('caught exception {} {}'.format(type(e).__name__, e)) | |
else: | |
wifiCfg.auto_connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment