Building a webserver on ESP32 using MicroPython
Posted: Tue Aug 03, 2021 1:32 pm
Before reading my question, my english skill is poor, so please send me feedback or advise in easy words. Thank you.
1. What I Want To Do
I want to build a webserver on ESP32-WROOM-32(ESP32-DevKitc_V4) with MicroPython(v1.16 on 2021-06-23: latest firmware available). My Goal is building a webserver on ESP32 and collecting the data of sensors connected to ESP32 periodically.
2. Environment
Firstly I wrote a code according to this article : https://randomnerdtutorials.com/esp32-e ... eb-server/(Article Title: RandomNerdTutorial: ESP32/ESP8266 MicroPython Web Server) – Control Outputs. My code is as follows:
I connect ESP32 board with USB cable to Windows10 PC and then Windows10 recognize this device as COM4. I wrote this code on Thonny Editor on windows10 and then transfer this code as main.py using ampy command on command line like this:
Tansfer was always successful and then connecting ESP32 from Thonny editor. firstly this message is shown in the shell console like this:
And then when I press reset button on ESP32, Error was shown like this:
The line 48 corresponds this line:
According to the official documentation https://docs.micropython.org/en/latest/ ... ocket.html(Docs » MicroPython libraries » usocket – socket module),it seems that prividing only two parameters: hostname nad port is enough. I also tried by writing like this:
But the result was same. I do not understand what is the required another positional argument. So I want advice or feedback to solve this issue.
Thank you for your cooperation.
1. What I Want To Do
I want to build a webserver on ESP32-WROOM-32(ESP32-DevKitc_V4) with MicroPython(v1.16 on 2021-06-23: latest firmware available). My Goal is building a webserver on ESP32 and collecting the data of sensors connected to ESP32 periodically.
2. Environment
- Windows10 (64bit)
MicroPython firmware v1.16 on 2021-06-23
Editor: Thonny Editor v3.3.11
ESP32 DevKitc v4
ampy v1.1.0
Firstly I wrote a code according to this article : https://randomnerdtutorials.com/esp32-e ... eb-server/(Article Title: RandomNerdTutorial: ESP32/ESP8266 MicroPython Web Server) – Control Outputs. My code is as follows:
Code: Select all
import network
import usys
try:
import usocket as socket
except:
import socket
def connect(SSID,PASS):
AP = network.WLAN(network.AP_IF)
if AP.isconnected():
print("Already connected.")
return AP
else:
AP.active(True)
AP.connect(SSID,PASS)
while not AP.isconnected():
print(".",end="")
utime.sleep(0.5)
if AP.isconnected():
print("Network connection is established.")
return AP
else:
print("Connection failed.")
return False
wifi = connect("myssid","mypass")
if not wifi:
usys.exit(0)
html_strings = """
<!DOCTYPE html>
<html lang='ja'>
<head>
<meta charset='utf-8'>
<title>SERVER TEST</title>
</head>
<body>
<h1>SERVER TEST</h1>
</body>
</html>
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = socket.getaddrinfo(("",80))[0][-1]
s.bind(addr)
s.listen(1)
print("Linstening on ", addr)
while True:
conn,addr = s.accept()
print("connected from : ",addr)
request = conn.recv(1024)
respose = html_strings
conn.send("HTTP/1.0 200 OK\r\n")
conn.send("Content-type: text/html\r\n")
conn.send("\r\n")
conn.send(response)
conn.close()
Code: Select all
> ampy --port COM4 put main.py
Code: Select all
MicroPython v1.16 on 2021-06-23; ESP32 module with ESP32
Type "help()" for more information.
Code: Select all
Traceback(most recent call last)
File "main.py", line 48, in <module>
TypeError: function mising 1 required positional arguments
Code: Select all
addr = socket.getaddrinfo(("",80))
Code: Select all
addr = socket.getaddrinfo(("0,0,0,0",80))
Thank you for your cooperation.