Building a webserver on ESP32 using MicroPython

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
kazu755
Posts: 2
Joined: Tue Aug 03, 2021 11:36 am

Building a webserver on ESP32 using MicroPython

Post by kazu755 » 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
  • Windows10 (64bit)
    MicroPython firmware v1.16 on 2021-06-23
    Editor: Thonny Editor v3.3.11
    ESP32 DevKitc v4
    ampy v1.1.0
3. What I did

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()
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:

Code: Select all

> ampy --port COM4 put main.py
Tansfer was always successful and then connecting ESP32 from Thonny editor. firstly this message is shown in the shell console like this:

Code: Select all

MicroPython v1.16 on 2021-06-23; ESP32 module with ESP32
Type "help()" for more information.
And then when I press reset button on ESP32, Error was shown like this:

Code: Select all

Traceback(most recent call last)
  File "main.py", line 48, in <module>
TypeError: function mising 1 required positional arguments
The line 48 corresponds this line:

Code: Select all

addr = socket.getaddrinfo(("",80))
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:

Code: Select all

addr = socket.getaddrinfo(("0,0,0,0",80))
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.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Building a webserver on ESP32 using MicroPython

Post by davef » Tue Aug 03, 2021 8:09 pm

What I found in umail.py

Code: Select all

smtp = umail.SMTP('smtp.gmail.com', 587)
addr = usocket.getaddrinfo(host, port)[0][-1]
So try:

Code: Select all

addr = socket.getaddrinfo(host, port)[0][-1]
Maybe you are not allowed to use "" for host. Try " " or a real hostname.

An excellent job at presenting all the required information.

Lixas
Posts: 10
Joined: Fri Aug 21, 2020 9:09 am

Re: Building a webserver on ESP32 using MicroPython

Post by Lixas » Mon Aug 09, 2021 12:57 pm

kazu755 wrote:
Tue Aug 03, 2021 1:32 pm
... I also tried by writing like this:

Code: Select all

addr = socket.getaddrinfo(("0,0,0,0",80))
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.
You have to many parenthesis ( and ) symbol in declaration. Also, IP separator is dot, not comma
Try following:

Code: Select all

addr = socket.getaddrinfo("127.0.0.1", 80)

kazu755
Posts: 2
Joined: Tue Aug 03, 2021 11:36 am

Re: Building a webserver on ESP32 using MicroPython

Post by kazu755 » Tue Aug 10, 2021 11:09 pm

Hello Lixas

Thank you for your advice. I use "." instread of "," when specifying the address.
Lixas wrote:
Mon Aug 09, 2021 12:57 pm
kazu755 wrote:
Tue Aug 03, 2021 1:32 pm
... I also tried by writing like this:

Code: Select all

addr = socket.getaddrinfo(("0,0,0,0",80))
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.
You have to many parenthesis ( and ) symbol in declaration. Also, IP separator is dot, not comma
Try following:

Code: Select all

addr = socket.getaddrinfo("127.0.0.1", 80)

Post Reply