try:
import usocket as socket #importing socket
except:
import socket
import network #importing network
import esp #importing ESP
esp.osdebug(None)
import gc
gc.collect()
from machine import Pin
ssid = 'ESP-SERVER' #Set your own
password = '12345678' #Set your own password
led = Pin(2, Pin.OUT)
led_state = "OFF"
temp=0
ap = network.WLAN(network.AP_IF)
ap.active(True) #activating
ap.config(essid=ssid, password=password)
import dht
sensor = dht.DHT11(Pin(15))
while ap.active() == False:
pass
print('Connection is successful')
print(ap.ifconfig())
sensor.measure()
temp = sensor.temperature()
print('Temperature: %3.1f C' %temp)
def web_page():
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><h1>Welcome to ESP-SERVER</h1><p>Suhu: """+str(temp)+""" Celcius<br>LED state: <strong>""" + led_state + """</strong></p><p><a href=\"?led_2_on\"><button class="button">LED ON</button></a></p><p><a href=\"?led_2_off\"><button class="button button1">LED OFF</button></a></p></body></html>"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #creating socket object
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
request = str(request)
print('GET Rquest Content = %s' % request)
led_on = request.find('/?led_2_on')
led_off = request.find('/?led_2_off')
if led_on == 6:
print('LED ON -> GPIO2')
led_state = "ON"
led.on()
if led_off == 6:
print('LED OFF -> GPIO2')
led_state = "OFF"
led.off()
temp = sensor.temperature()
print('Temperature: %3.1f C' %temp)
response = web_page()
conn.send(response)
conn.close()