ESP32 connected local Wi-FI how to get it's IP using socket

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
arishy
Posts: 4
Joined: Sun Mar 15, 2020 4:32 am

ESP32 connected local Wi-FI how to get it's IP using socket

Post by arishy » Sun Mar 15, 2020 4:40 am

I connected my ESP32 to the local Wi-Fi network and I want to use socket to get it's ip
Also I have no idea of the "hostname" assigned to it

Do I have control over these data ...( ip, hostname ...etc) ???

By control I mean can I assign "static data" to them

arishy
Posts: 4
Joined: Sun Mar 15, 2020 4:32 am

Re: ESP32 connected local Wi-FI how to get it's IP using socket

Post by arishy » Sun Mar 15, 2020 4:46 pm

In Python you can do this:
import socket
def main():
hostName = socket.gethostname()

ipaddr = socket.gethostbyname(hostName)
print(" Host Name Is : {} " .format(hostName))
print(" IP Address Is : {} " .format(ipaddr))

if __name__ == "__main__":
main()

But I have no clue how to do it in micropython........ Is usocket viable solution if yes please let me know how to do it with an example

DJShadow1966
Posts: 57
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Re: ESP32 connected local Wi-FI how to get it's IP using socket

Post by DJShadow1966 » Sun Mar 15, 2020 5:05 pm

Hello

You can set the hostname as an example this is what I use :-

Code: Select all

import network, ntptime, utime, machine

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.config(dhcp_hostname="ResetTest")
sta_if.connect('ssid', 'password')
print("Connecting ")
while not sta_if.isconnected():
    print(".", end="")
    utime.sleep(1)
You can also set static ip by doing before the connect command :-

Can be seen on the document page https://docs.micropython.org/en/latest/ ... C.ifconfig

Code: Select all

sta_if.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
Regards Mike

arishy
Posts: 4
Joined: Sun Mar 15, 2020 4:32 am

Re: ESP32 connected local Wi-FI how to get it's IP using socket

Post by arishy » Sun Mar 15, 2020 6:02 pm

This is great Mike....you gave me quite a lot to work on.
If I understand you, you dealt with my issue by "ignoring" the socket approach.
BUT if I am already connected, is it possible to come up with a similar simple solution like the one offered above with proper python. i.e. use of socket ?????.

Or....just forget it and use your solution.....

arishy
Posts: 4
Joined: Sun Mar 15, 2020 4:32 am

Re: ESP32 connected local Wi-FI how to get it's IP using socket

Post by arishy » Mon Mar 16, 2020 2:33 am

It seems to me that I have the "wrong" mind set for the socket use.
If you need to connect to the OUTSIDE world then the sucket is your key. And it is pretty straight forward to use it that way.
BUT since my issue was purely INTERNAL i.e. within the WiFi network , there is really no need to use sockets for that and Mike solution is the way to go...

Your thoughts will be greatly appreciated......

Post Reply