I want create two classes. One for controll hardware I/O and one for services HTTP Server on ESP8266. So, I create Two instance of class object. But when I run program websocket blocking system.
This is a code:
- main.py
Code: Select all
from nphttpd import HttServ
from esphw import mySuperHWClass
import uasyncio
a = HttServ()
b = mySuperHWClass()
loop = uasyncio.get_event_loop()
loop.run_until_complete(a.run_socket())
loop.run_until_complete(b.my_hardware())
- Hardware Controll Class
Code: Select all
from machine import Pin
import uasyncio
p2 = Pin(2, Pin.OUT)
p2.off()
class mySuperHWClass(object):
def __init__(self):
self.name = "LED_2"
async def my_hardware(self):
while True:
print(self.name + "_ON")
p2.on()
await uasyncio.sleep_ms(100)
p2.off()
print(self.name + "_OFF")
await uasyncio.sleep_ms(100)
- HTTP WebServer
Code: Select all
import socket
from machine import Pin, reset
from network import WLAN, AP_IF
from time import sleep
import uasyncio
class HttServ(object):
def __init__(self):
self.ip_address = '192.168.0.1' # set your ip here
self.ssid = 'MicroPython-AP-Ext'
self.password = '123456789'
ip_change = self.set_ip()
if ip_change:
self.port = 80 # set your port here
self.conn = None
self.s = None
self.run_socket()
def set_ip(self):
ap_if = WLAN(AP_IF)
ap_if.active(True)
ap_if.config(essid=self.ssid, password=self.password)
ap_if.ifconfig((self.ip_address,'255.255.255.0','192.168.0.1','192.168.0.1'))
sleep(1)
return True
def connection(self, html):
try:
self.conn.sendall(html)
sleep(0.2)
except Exception as exc:
print("Send Error", exc.args[0])
pass
finally:
self.conn.close()
def parse_request(self):
self.request = b''
async def run_socket(self):
html = None
try:
with open("index.html", "r") as fhtml:
html = fhtml.read()
except OSError:
pass
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(("", self.port))
self.s.listen(1)
except Exception as exc:
print("Address in use, restarting", exc.args[0])
sleep(2)
reset()
pass
while True:
try:
self.conn, addr = self.s.accept()
except Exception as exc:
print("Socket Accept Error ", exc.args[0])
reset()
pass
print('Connected by', addr)
self.conn.settimeout(1)
self.request = b''
while True:
try:
self.request_chunk = self.conn.recv(8)
self.request += self.request_chunk
if (len(self.request_chunk) < 8):
break
except Exception as TimeoutException:
print("Timeout. Break Loop.", TimeoutException.args[0])
break
self.connection(html)