The Error :
Code: Select all
Connection successful
('192.168.43.150', '255.255.255.0', '192.168.43.72', '192.168.43.72')
Traceback (most recent call last):
File "<stdin>", line 80, in <module>
TypeError: can't convert function to int
Code: Select all
try:
import usocket as socket
except:
import socket
from time import sleep
import urequests
from machine import Pin,Timer
import network
import esp
esp.osdebug(None)
import gc
gc.collect()
ssid = 'Memesterr'
password = 'asdfghjkl'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
# Complete project details at https://RandomNerdTutorials.com
print('Connection successful')
print(station.ifconfig())
led = Pin(2, Pin.OUT)
button=Pin(5,Pin.IN)
def web_page():
if led.value() == 1:
state="ON"
elif led.value()==0:
state="OFF"
html = """<html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body> <h1>ESP Web Server</h1>
<p>LED is <strong>""" + state + """</strong></p><p>
<form>
<br><button name="LED" value="0" type="submit">ON</button>
<br><button name="LED" value="1" type="submit">OFF</button>
</form>
</body>
</html>"""
return html
def req_page():
html="""<html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body> <h1>REQUEST</h1>
<p>Request From ESP8266</p>
</body>
</html>
"""
return html
def SendPage(self):
SendState=True
SendState=False
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)
button.irq(machine.Pin.IRQ_FALLING,SendPage)
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=0")
led_off = request.find("/?LED=1")
#led_on.irq(request)
if SendState==False:
if led_on == 6:
print('LED ON')
led.value(1)
elif led_off == 6:
print('LED OFF')
led.value(0)
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)
#print("response",response)
conn.close()
elif SendState==True:
req=req_page()
conn.sendall(req)
conn.close()