0
votes

Before reading my question, my english skill is poor, so please send me feedback or advise in easy words. Thank you.

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 inside ESP32 and collecting the data of sensors connected to ESP32 periodically.

Environment

  • Windows10(64bit)
  • MicroPython (v1.16 on 2021-06-23)
  • Editor : Thonny Editor (v3.3.11)
  • ESP32 Devkitc_v4
  • ampy (v1.1.0)

What I did

Firstly I wrote a code according to this article : RandomNerdTutorial: ESP32/ESP8266 MicroPython Web Server – Control Outputs. My code is as follows:

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(("0,0,0,0",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 PC and Windows10 recognize this device as COM4. I wrote this code on Thonny Editor on windows10(64bit) and then transfer this code as main.py using ampy command on command line like this:

> ampy --port COM4 put main.pye

Tansfer was always successful and then connecting ESP32 from Thonny editor. firstly this message is shown in the shell console like this:

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:

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:

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

According to the official documentation 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:

addr = socket.getaddrinfo(('',80))[@][-1]

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

1 Answers

0
votes

The getaddrinfo method expects two arguments:

socket.getaddrinfo('hostname', port)

You are passing a single argument -- a 2-tuple. Instead of:

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

You want:

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

Note that I've removed one set of parentheses.

Here's a complete example at the REPL:

>>> import socket
>>> s = socket.socket()
>>> addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
>>> s.bind(addr)
>>> s.listen(1)
>>> conn,addr = s.accept()
>>> addr
('192.168.1.175', 43384)
>>> conn.send('HTTP/1.0 200 OK\r\n')
17
>>> conn.close()