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>WEATHER -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()
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>WEATHER -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()
html = """<html><head> <title>ESP Web Server</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>ESP Web Server</h1>
<p>Temperature: +"""+str(temp)+"""</p><p>LED state: <strong>""" + led_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
<p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""