More Clever Night Light in Micropython
Fleetingmore clever night light using a Adafruit Feather HUZZAH with ESP8266 with micropython
using a lot of code from IOT heart again, with micropython and more clever night light in kivy
spawn webrepl_cli.py -p 0000 192.168.1.219
op:repl, host:192.168.1.219, port:8266, passwd:0000.
Remote WebREPL version: (1, 25, 0)
Use Ctrl-] to exit this shell
>>>
cat<<EOF > /tmp/main.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import time
import machine
import neopixel
import network
import esp
import gc
import ntptime
import requests
led = machine.Pin(2, machine.Pin.OUT)
led.off() # on
time.sleep(2)
led.on() # off
# * SLEEP_LIGHT - light sleep, shuts down the WiFi Modem circuit and suspends the processor periodically.
# The system enters the set sleep mode automatically when possible.
esp.sleep_type(esp.SLEEP_LIGHT)
light = neopixel.NeoPixel(machine.Pin(4), 5)
holiday_schedule = {
"almost_hour": 7,
"almost_minute": 30,
"getup_hour": 8,
"getup_minute": 0,
"day_hour": 8,
"day_minute": 40,
"evening_hour": 20,
"evening_minute": 0,
"night_hour": 20,
"night_minute": 30,
}
def ease_debugging(schedule, today=None, duration=1, start_time="night"):
now = datetime.now()
global force_today
force_today = today
start_assoc = {"night": 0, "almost": 1, "getup": 2, "day": 3, "evening": 4,
"tomorrow": 5,}
schedule["almost_hour"] = (now + timedelta(minutes=duration*(1 - start_assoc[start_time]))).hour
schedule["almost_minute"] = (now + timedelta(minutes=duration*(1 - start_assoc[start_time]))).minute
schedule["getup_hour"] = (now + timedelta(minutes=duration*(2 - start_assoc[start_time]))).hour
schedule["getup_minute"] = (now + timedelta(minutes=duration*(2 - start_assoc[start_time]))).minute
schedule["day_hour"] = (now + timedelta(minutes=duration*(3 - start_assoc[start_time]))).hour
schedule["day_minute"] = (now + timedelta(minutes=duration*(3 - start_assoc[start_time]))).minute
schedule["evening_hour"] = (now + timedelta(minutes=duration*(4 - start_assoc[start_time]))).hour
schedule["evening_minute"] = (now + timedelta(minutes=duration*(4 - start_assoc[start_time]))).minute
schedule["night_hour"] = (now + timedelta(minutes=duration*(5 - start_assoc[start_time]))).hour
schedule["night_minute"] = (now + timedelta(minutes=duration*(5 - start_assoc[start_time]))).minute
ease_debugging(holiday_schedule, duration=60)
ease_debugging(school_schedule, duration=60)
class Connection:
def __init__(self):
self.wlan = network.WLAN()
def connect(self):
self.wlan.connect()
try:
ntptime.settime()
except OSError as e:
requests.post(
"http://home:9705/nightlight",
headers={"title": "nightlight error"},
data=f"""{e}
while trying to set the time""")
@property
def isconnected(self):
return self.wlan.status() == network.STAT_GOT_IP
connection = Connection()
connection.connect()
def run_command(led, command, name):
r, g, b, l = command
command = (int(r*l/255),int(g*l/255),int(b*l/255),)
now = time.localtime()
h = now[3]
m = now[4]
print(f"{h:02d}:{m:02d} - {gc.mem_free()}: {name}: Running command: {command}")
led.fill(command)
led.write()
def step():
print("Do step")
if not connection.isconnected:
connection.connect()
now = time.localtime()
h = now[3]
m = now[4]
requests.post(
"http://home:9705/nightlight",
headers={"title": f"{h:02d}:{m:02d} nightlight"},
data=f"""""")
# connection.wlan.disconnect()
commands = {
"left": [50, 50, 50, 50],
"right": [50, 50, 50, 50],
}
print("Starting")
while True:
step()
machine.lightsleep(10 * 1000)
EOF
webrepl_cli.py -p 0000 /tmp/main.py ${ip}:main.py
op:put, host:192.168.1.219, port:8266, passwd:0000.
/tmp/main.py -> main.py
Remote WebREPL version: (1, 25, 0)
Sent 0 of 1848 bytes
Sent 1024 of 1848 bytes
Sent 1848 of 1848 bytes