IOT Heart Again, With Micropython
FleetingIOT heart again, with micropython on my wemos/lolin d1
See how to play with the wemos d1.
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)
left = neopixel.NeoPixel(machine.Pin(4), 5)
right = neopixel.NeoPixel(machine.Pin(5), 5)
class Connection:
def __init__(self):
self.wlan = network.WLAN()
def connect(self):
self.wlan.connect()
ntptime.settime()
connection = Connection()
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():
if not connection.wlan.isconnected():
connection.wlan.connect()
if connection.wlan.isconnected():
commands = requests.get("http://192.168.1.46:9696/get2").json()
else:
commands = {
"left": [50, 50, 50, 50],
"right": [50, 50, 50, 50],
}
run_command(left, commands["left"], "left")
run_command(right, commands["right"], "right")
initialsleeptime = 1
sleeptime = initialsleeptime
while True:
try:
step()
sleeptime = initialsleeptime
except Exception as e:
sleeptime *= 2
if sleeptime > 3600:
sleeptime = 3600
try:
now = time.localtime()
h = now[3]
m = now[4]
requests.post(
"http://192.168.1.46:9705/n",
headers={"title": f"{h:02d}:{m:02d} iot heart error"},
data=f"""{e}
will wait for {sleeptime}s""")
except Exception as e2:
print(f"{h:02d}:{m:02d}: Could not notify: {e2}")
machine.lightsleep(sleeptime * 1000)
EOF
webrepl_cli.py -p 0000 /tmp/main.py 192.168.1.230:main.py
op:put, host:192.168.1.230, port:8266, passwd:0000.
/tmp/main.py -> main.py
Remote WebREPL version: (1, 23, 0)
Sent 0 of 2104 bytes
Sent 1024 of 2104 bytes
Sent 2048 of 2104 bytes
Sent 2104 of 2104 bytes
spawn webrepl_cli.py -p 0000 192.168.1.230
op:repl, host:192.168.1.230, port:8266, passwd:0000.
Remote WebREPL version: (1, 23, 0)
Use Ctrl-] to exit this shell
Traceback (most recent call last):
File "main.py", line 82, in <module>
KeyboardInterrupt:
MicroPython v1.23.0 on 2024-06-02; ESP module (1M) with ESP8266
Type "help()" for more information.
>>>