Page 1 of 1
ESP32 connected local Wi-FI how to get it's IP using socket
Posted: Sun Mar 15, 2020 4:40 am
by arishy
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
Re: ESP32 connected local Wi-FI how to get it's IP using socket
Posted: Sun Mar 15, 2020 4:46 pm
by arishy
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
Re: ESP32 connected local Wi-FI how to get it's IP using socket
Posted: Sun Mar 15, 2020 5:05 pm
by DJShadow1966
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
Re: ESP32 connected local Wi-FI how to get it's IP using socket
Posted: Sun Mar 15, 2020 6:02 pm
by arishy
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.....
Re: ESP32 connected local Wi-FI how to get it's IP using socket
Posted: Mon Mar 16, 2020 2:33 am
by arishy
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......