|
import network |
|
import json |
|
import urequests |
|
from machine import Pin, Timer, I2C |
|
import time |
|
from lcd_api import LcdApi |
|
from i2c_lcd import I2cLcd |
|
|
|
HTTP_OK = 200 |
|
AUTH_HEADER = {"Authorization":"Bearer API_KEY"} |
|
|
|
I2C_ADDR = 0x27 |
|
I2C_NUM_ROWS = 4 |
|
I2C_NUM_COLS = 20 |
|
|
|
def get_new_notification(last_id): |
|
r = None |
|
if last_id: |
|
r = urequests.get(f"https://mastodon.social/api/v1/notifications?since_id{last_notification_id}=&limit=1", headers=AUTH_HEADER) |
|
else: |
|
r = urequests.get(f"https://mastodon.social/api/v1/notifications?limit=1", headers=AUTH_HEADER) |
|
|
|
if r.status_code == HTTP_OK: |
|
text = r.text |
|
data = json.loads(text) |
|
return data[0] |
|
else: |
|
print(r.status_code) |
|
return None |
|
|
|
|
|
def get_last_toot(last_id): |
|
r = None |
|
if last_id: |
|
r = urequests.get(f"https://mastodon.social/api/v1/timelines/home?since_id{last_id}=&limit=1", headers=AUTH_HEADER) |
|
else: |
|
r = urequests.get(f"https://mastodon.social/api/v1/timelines/home?limit=1", headers=AUTH_HEADER) |
|
|
|
if r.status_code == HTTP_OK: |
|
text = r.text |
|
try: |
|
data = json.loads(text) |
|
return data[0] |
|
except: |
|
print(text) |
|
return None |
|
|
|
else: |
|
print(r.status_code) |
|
return None |
|
|
|
|
|
def blink(timer): |
|
led.toggle() |
|
|
|
|
|
# print(r.status_code) |
|
#t = r.text |
|
#j = json.loads(t) |
|
#print(j[0]["account"]["username"]) |
|
#print(j[0]["content"]) |
|
|
|
|
|
|
|
|
|
led = Pin(11, Pin.OUT) |
|
timer = Timer() |
|
|
|
wlan=network.WLAN(network.STA_IF) |
|
wlan.active(True) |
|
wlan.connect(WIFI_NAME, WIFI_PASSWD) |
|
print(wlan.isconnected()) |
|
|
|
last_id = None |
|
last_notification_id = None |
|
is_a_new_notification = False |
|
|
|
last_toot_id = None |
|
|
|
i2c = I2C(1, sda=machine.Pin(14), scl=machine.Pin(15), freq=400000) |
|
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) |
|
|
|
while True: |
|
notification = get_new_notification(last_notification_id) |
|
if notification: |
|
is_a_new_notification = last_notification_id != notification["id"] |
|
last_notification_id = notification["id"] |
|
last_notification_time = time.gmtime() |
|
print(last_notification_id) |
|
if is_a_new_notification: |
|
print(f"es nueva:{is_a_new_notification}") |
|
timer.init(freq=3.0, mode=Timer.PERIODIC, callback=blink) |
|
|
|
toot = get_last_toot(last_toot_id) |
|
if toot and toot["id"] != last_toot_id: |
|
last_toot_id = toot["id"] |
|
if "reblog" in toot.keys(): |
|
lcd.putstr(toot["reblog"]["content"]) |
|
else: |
|
lcd.putstr(toot["content"]) |
|
|
|
|
|
time.sleep(30) |
|
timer.deinit() |
|
led.off() |
|
|
ideas: