You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
from csms12 import CSMS12 |
|
import socket |
|
import network |
|
|
|
def connect(): |
|
sta_if = network.WLAN(network.STA_IF) |
|
|
|
if not sta_if.isconnected(): |
|
print('connecting to network...') |
|
|
|
sta_if.active(True) |
|
sta_if.connect('<ssid>', '<psk>') |
|
|
|
while not sta_if.isconnected(): |
|
pass |
|
|
|
print('network config:', sta_if.ifconfig()) |
|
|
|
def server(): |
|
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] |
|
sensor = CSMS12() |
|
|
|
html = """ |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>ESP8266 Moisture Sensor v1.2</title> |
|
<meta http-equiv="refresh" content="1"> |
|
</head> |
|
<body> |
|
<h1>Current moisture classification</h1> |
|
<h2>%s</h2> |
|
</body> |
|
</html> |
|
""" |
|
|
|
s = socket.socket() |
|
s.bind(addr) |
|
s.listen(1) |
|
|
|
print('listening on', addr) |
|
|
|
while True: |
|
cl, addr = s.accept() |
|
print('client connected from', addr) |
|
cl_file = cl.makefile('rwb', 0) |
|
|
|
while True: |
|
line = cl_file.readline() |
|
if not line or line == b'\r\n': |
|
break |
|
|
|
response = html % sensor.getInterval() |
|
cl.send(response) |
|
cl.close()
|
|
|