I'm experimenting with micropython on esp8266, especially WEMOS D1 mini, in order to replace some old projects I have done.
I started with a simple switch in connection with a http web interface.
After some hours the system becomes unresponsable, now responses to pings and the web interface is not opening anymore. The same system on Arduino programming language works fine for 2 years now.
Attache my code for boot.py and main.py
Any suggestions?
boot.py
Code: Select all
import uos, machine
import gc
import webrepl
import network
import time
import os
webrepl.start()
gc.collect()
try:
import usocket as socket
except:
import socket
from machine import Pin
import network
import esp
esp.osdebug(None)
import gc
gc.collect()
ssid = 'xxx'
password = 'xxx'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
led = Pin(2, Pin.OUT)
led.value(1)
Code: Select all
def web_page():
if led.value() == 0:
gpio_state="ON"
else:
gpio_state="OFF"
html = """<html><head> <title>Kaffeemachine einschalten</title> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none;
border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
.button2{background-color: #4286f4;}</style></head><body> <h1>Kaffeemachine einschalten</h1>
</p><p><a href="/?led=on"><button class="button">ON</button></a></p></body></html>"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
print('Content = %s' % request)
led_on = request.find('/?led=on')
led_off = request.find('/?led=off')
print('LED ON')
led.value(0)
time.sleep(0.8)
led.value(1)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
Daniel