Toggle Navigation
Hatchery
Eggs
stillSolunaAnyway
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import gc, wifi, badge, time, display, machine from time import sleep import urequests as requests #/** the width of the eink display */ msRaduis = 45 BADGE_EINK_WIDTH = 296 + msRaduis*2 #/** the height of the eink display */ BADGE_EINK_HEIGHT = 128 try: while True: try: wifi.init() while not wifi.sta_if.isconnected(): sleep(0.1) except: wifi.connect() print("Connecting to WiFi") if not wifi.wait(): time.sleep(2) print("Connection Error, Rebooting") machine.reset() wifi.ntp(False) rtc = machine.RTC() #(1970, 1, 1, 1, 1, 19, 5, 1) print(rtc) print(rtc.now()) #url="http://ip-api.com/line/?fields=city" url= 'http://wttr.in/?format="%S:+%m+%s"' while True: r = requests.get(url) print("API HTTP request status Code:",r.status_code) #print(r.status_code) if r.status_code == 200: break else: # Hope it won't 500 a little later print("Bad response") time.sleep(5) print("Data:",r.text) sunrise = r.text[1:6] moonPhase = r.text[11:12] sunset = r.text[13:18] #sunrise = sunrise[:8] r.close() gc.collect() #sunrise = r.text[:8] #sunset = r.text[:-8] print(sunrise) print(moonPhase) print(sunset) #sunriseSec = (sunrise[1]*10*60*60) + (sunrise[2]*60*60) + (sunrise[3]*10*60) + (sunrise[4]*60) sunriseSec = (int(sunrise[0]) *10*60*60) + (int(sunrise[1]) *60*60) + (int(sunrise[3]) *10*60) + (int(sunrise[4]) *60) sunsetSec = (int(sunset[0]) *10*60*60) + (int(sunset[1]) *60*60) + (int(sunset[3]) *10*60) + (int(sunset[4]) *60) nightLen = (24*3600) - sunsetSec + sunriseSec dayLen = sunsetSec - sunriseSec #now = (int(sunset[0]) *10*60*60) + (int(sunset[1]) *60*60) + (int(sunset[3]) *10*60) + (int(sunset[4]) *60) now = rtc.now() nowStr = str(now[3]) + ":" + str(now[4]) now = int(now[3])*60*60 + int(now[4])*60 dayornight = True print("now",now) if (now >= sunsetSec) & (now <= (24*3600)): print("Before midnight") nowNight = (now - sunsetSec) dayornight = False elif (now <= sunriseSec) & (now >= 0): print("After midnight") nowNight = ((24*3600)-sunsetSec) + now dayornight = False elif (now >= sunriseSec) & (now <= sunsetSec): print("Day time") nowDay = now - sunriseSec dayornight = True print("sunrise",sunriseSec) print("sunset",sunsetSec) print("night len",nightLen) print("day len",dayLen) print(24*3600,":",dayLen+nightLen) if dayornight: print("now day",nowDay) printColor = 0x000000 x = BADGE_EINK_WIDTH - int((nowDay/dayLen) * BADGE_EINK_WIDTH) print("Sun pos:",x) #x = int(x - (90/2)) #print("Sun pos:",x) display.drawFill(0xFFFFFF) # Fill the screen with black display.drawCircle(x-msRaduis,msRaduis+8, msRaduis,0,360,True,printColor) #display.drawPng(x,8,"/lib/stillsolunaanyway/moon.png") #print(display.pngInfo("/lib/stillsolunaanyway/moon.png")) print("printing sun") display.drawText(0, 105, sunset, printColor, "Roboto_Regular18") display.drawText(int(BADGE_EINK_WIDTH/2) - int(display.getTextWidth(nowStr,"Roboto_Regular18")/2) - msRaduis , 105, nowStr, printColor, "Roboto_Regular18") display.drawText(245, 105, sunrise, printColor, "Roboto_Regular18") else: print("now night",nowNight) printColor = 0xFFFFFF x = BADGE_EINK_WIDTH - int((nowNight/nightLen) * BADGE_EINK_WIDTH) print("Moon pos:",x) #x = int(x - (90/2)) #print("Moon pos:",x) display.drawFill(0x000000) # Fill the screen with black display.drawPng(x - msRaduis*2,8,"/lib/stillsolunaanyway/moon.png") print(display.pngInfo("/lib/stillsolunaanyway/moon.png")) print("printing moon") display.drawText(0, 105, sunrise, printColor, "Roboto_Regular18") display.drawText(int(BADGE_EINK_WIDTH/2) - int(display.getTextWidth(nowStr,"Roboto_Regular18")/2) - msRaduis , 105, nowStr, printColor, "Roboto_Regular18") display.drawText(245, 105, sunset, printColor, "Roboto_Regular18") #print("now str:" , nowStr , "len of: ", int(display.getTextWidth(nowStr,"Roboto_Regular18")/2)) display.flush() # Write the contents of the buffer to the display badge.eink_busy_wait() time.sleep(60) #display.drawpng(0,0,moon1.png) except: print("################################################################################################################################################################") print("################################################################################################################################################################") print("Something bad happened (blame lack of RAM), exception, we need a reboot") print("################################################################################################################################################################") print("################################################################################################################################################################") #system.reboot() time.sleep(1) machine.reset()