Home > Back-end >  Change url of a webpage on a local network
Change url of a webpage on a local network

Time:02-28

I built a webpage on an ESP32 chip, charged to create an access point allowing my computer to connect in order to access this page.
For the moment I can only access it using the IP of my ESP by typing it in a browser but it can be very bothersome.
I'd like to know if it was possible to change the url of the page using words instead of the ESP's IP.
Maybe I'm missing some technical terms but I didn't find any solution on the internet.

PS: I'm using micropython with sockets to serve html files from the board:

def handleClient(client_socket):
    headers, data = loadRequest(client_socket.recv(1024).decode('utf-8'))
    # print('[*] Received:\n%s\n%s\n' % (headers, data))
    if headers['method'] == 'GET' and '/connect' == headers['route']:#'/connect' in headers['route']:
        ssid, password, status, code = connect(headers)
        client_socket.sendall(RESPONSE_TEMPLATE % (code, status, {'ssid': ssid, 'password': password}, code))
        return ssid, password
    elif headers['method'] == 'GET' and headers['route'] == '/':
        renderWebPage(client_socket)
    client_socket.close()
    return None, None

CodePudding user response:

there are two parts needed to solve your Q:

  1. publish a name (using mdns)
  2. resolve that name from a client

MicroPython has built-in support for mdns since v1.12

Note that your client also needs to have mdns support in order to be able to resolve that address. Than may/will depend on your client.

import network
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
    wlan.active(True)
    mac = wlan.config('mac')
    host = 'esp32-'   ''.join('{:02x}'.format(b) for b in mac[3:])
    wlan.config(dhcp_hostname = host)
    wlan.connect('myssid', 'mypassword')
    while not wlan.isconnected():
        pass
        
host = wlan.config('dhcp_hostname')
print('Wifi connected as {}/{}, net={}, gw={}, dns={}'.format(
    host, *wlan.ifconfig()))

Source: MicroPython Forum

CodePudding user response:

Certainly. The easiest option is enabling mDNS. This allows hosts in the same local network to resolve the device's name (e.g. espressif.local) into its IP. Only works in local network and requires an mDNS client on the computer (Mac, Linux and Windows all tend to have it built in these days).

No idea how to do it in Micropython, though. Give Google a try.

  • Related