Konubinix' opinionated web of thoughts

IOT Heart Again, With Micropython

Fleeting

IOT 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()

    @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 try_get():
    try:
        return requests.get("http://192.168.1.46:9696/get2").json()
    except:
        return None

def step():
    print("Do step")
    if not connection.isconnected:
        connection.connect()
    if connection.isconnected:
        commands = try_get()
    else:
        # grey
        commands = {
            "left": [50, 50, 50, 50],
            "right": [50, 50, 50, 50],
        }
    if commands is None:
        # pink
        commands = {
            "left": [50, 0, 50, 50],
        }
        try:
            connection.wlan.disconnect()
        except:
            # blue + red
            commands["right"] = [50, 50, 0, 50]
        else:
            try:
                connection.connect()
                if connection.isconnected:
                    # blue
                    commands["right"] = [0, 0, 50, 50]
                else:
                    # red
                    commands["right"] = [50, 0, 0, 50]
            except:
                # green
                commands["right"] = [0, 50, 0, 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 > 60:
            sleeptime = 60
        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}")
    print(f"Waiting for {sleeptime}s")
    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 3001 bytes
Sent 1024 of 3001 bytes
Sent 2048 of 3001 bytes
Sent 3001 of 3001 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
Waiting for 1s
Traceback (most recent call last):
  File "main.py", line 119, in <module>
KeyboardInterrupt:

MicroPython v1.23.0 on 2024-06-02; ESP module (1M) with ESP8266
Type "help()" for more information.
>>>