How to know the IP address
-
- Posts: 8
- Joined: Wed Jan 08, 2020 10:10 am
How to know the IP address
Hi,
I'm using MicroPython on a Wemos D1 Mini to power a little IoT device. When it is first powered, it will power-up its WiFi access point for the user to connect it to their home WiFi network.
I have some questions about what IP address the user should use both when connecting to the access point, and when the device has connected to their home WiFi. The questions are:
1. Has the IoT community settled on a paradigm for what the user should connect to when they first setup hardware? My thoughts were to ask them to connect to http://10.0.0.1.
2. Once the user inputs their home WiFi, the device will connect to it and obtain an IP address (hopefully). I can't know in advance what their IP address will be, so how should I instruct them to connect back to their device? I thought about using the hostname, e.g. (http://your-device.local) but setting the hostname didn't seem to allow the user to connect in that way.
Many thanks in advance for your help, it's appreciated!
Andy
I'm using MicroPython on a Wemos D1 Mini to power a little IoT device. When it is first powered, it will power-up its WiFi access point for the user to connect it to their home WiFi network.
I have some questions about what IP address the user should use both when connecting to the access point, and when the device has connected to their home WiFi. The questions are:
1. Has the IoT community settled on a paradigm for what the user should connect to when they first setup hardware? My thoughts were to ask them to connect to http://10.0.0.1.
2. Once the user inputs their home WiFi, the device will connect to it and obtain an IP address (hopefully). I can't know in advance what their IP address will be, so how should I instruct them to connect back to their device? I thought about using the hostname, e.g. (http://your-device.local) but setting the hostname didn't seem to allow the user to connect in that way.
Many thanks in advance for your help, it's appreciated!
Andy
Re: How to know the IP address
I'm also curious to know what other people suggest here...this appears to be a bit of an unsolved problem.
Some thoughts from my own experiences:
1. I'm assuming you're suggesting that the device is its own AP until configured. i.e. you connect with their phone to their AP and configure them, then they join your real AP. Note there are some issues with this (at least on Android, don't know about iOS) where because the device AP has no route to the internet the phone refuses to send _any_ traffic to it. (See viewtopic.php?f=16&t=6742&p=38383#p38383 for recent discussion)
I've used commercially available IoT gear that does this. Usually the app takes care of connecting to the AP transparently, however often with instructions about putting your phone into airplane mode then manually turning on WiFi.
But I guess your question is, what IP address should the device assign itself when in AP mode?
2. Setting the hostname will only work if the DHCP server is also your DNS server (i.e. you're not directly using your ISP's DNS server).
I think commercial IoT devices get around this with a server run by the manufacturer where either you bounce all traffic to the device via their servers, or at a minimum they just register themselves with the server then your app can ask for their IP. (i.e. they effectively build their own DNS). Many devices do the first thing because they need the phone app to work when your phone isn't at home (i.e. not on the same WiFi network).
Some thoughts from my own experiences:
1. I'm assuming you're suggesting that the device is its own AP until configured. i.e. you connect with their phone to their AP and configure them, then they join your real AP. Note there are some issues with this (at least on Android, don't know about iOS) where because the device AP has no route to the internet the phone refuses to send _any_ traffic to it. (See viewtopic.php?f=16&t=6742&p=38383#p38383 for recent discussion)
I've used commercially available IoT gear that does this. Usually the app takes care of connecting to the AP transparently, however often with instructions about putting your phone into airplane mode then manually turning on WiFi.
But I guess your question is, what IP address should the device assign itself when in AP mode?
2. Setting the hostname will only work if the DHCP server is also your DNS server (i.e. you're not directly using your ISP's DNS server).
I think commercial IoT devices get around this with a server run by the manufacturer where either you bounce all traffic to the device via their servers, or at a minimum they just register themselves with the server then your app can ask for their IP. (i.e. they effectively build their own DNS). Many devices do the first thing because they need the phone app to work when your phone isn't at home (i.e. not on the same WiFi network).
-
- Posts: 9
- Joined: Mon Jan 06, 2020 12:08 am
Re: How to know the IP address
If I unerstand your question correctly, this can be solved pretty easily.
Have a look at:
That should output the local ip address, which you can use to update some dynamic DNS record.
https://docs.micropython.org/en/latest/ ... N.ifconfig
For obtaining your external IP address there are loads of solutions.
Have a look at:
Code: Select all
print(WLAN.ifconfig()[2])
https://docs.micropython.org/en/latest/ ... N.ifconfig
For obtaining your external IP address there are loads of solutions.
-
- Posts: 8
- Joined: Wed Jan 08, 2020 10:10 am
Re: How to know the IP address
I am now going to use https://github.com/nickovs/slimDNS - this allows me to connect to my device with a friendly name, rather than trying to figure out what the IP address is (as long as the host's device/router supports mDNS).
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: How to know the IP address
These are really two separate questions.
The answer to 1) can be different. We still use a pre-configured SSID and PassPhrase, but keep the crappy WiFi router's DHCP in place. The device could listen on a UDP broadcast address. Your client application would first broadcast and receive zero or more UDP replies with addresses in return. The device could also have a unique device ID pre-configured to tell multiple of the same kind apart.
The answer to 2) is a lot more complicated. If WiFi is the only thing available, then powering up in AP mode and having another device (PC or phone or whatnot) connect to it to configure the SSID and PassPhrase is the only way, before the second solution above can take over. I have seen implementations that offer BlueTooth for the config phase. But somehow the device must get the WiFi credentials.
Regards, Jan
- How to control the IP address of a device in your own environment?
- How to create an initial WiFi config (SSID and PassPhrase) in an unknown environment?
The answer to 1) can be different. We still use a pre-configured SSID and PassPhrase, but keep the crappy WiFi router's DHCP in place. The device could listen on a UDP broadcast address. Your client application would first broadcast and receive zero or more UDP replies with addresses in return. The device could also have a unique device ID pre-configured to tell multiple of the same kind apart.
The answer to 2) is a lot more complicated. If WiFi is the only thing available, then powering up in AP mode and having another device (PC or phone or whatnot) connect to it to configure the SSID and PassPhrase is the only way, before the second solution above can take over. I have seen implementations that offer BlueTooth for the config phase. But somehow the device must get the WiFi credentials.
Regards, Jan
Re: How to know the IP address
A different try at answering... It's a messy problem, unfortunately. The industry answer (WPS) is insecure...
If your device starts an access point it should implement what is called a "captive portal". What that means is that any hostname the user tries to resolve ends up pointing to your device. Same thing as when you're in a coffee shop, hotel, airport and you always land on that stupid "accept the conditions of use" page. Android devices try to fetch a something.google.com page (I forget the details) and given the appropriate response cause a pop-up directing the user to the web browser to "sign in".
Once the user is on a web page of your device, you will want to show networks to join and provide a text box to enter a password. You then want to associate with the network specified by the user in STA+AP mode so your device can continue providing information to the user, such as which IP address your device got. Once the user has seen that you can turn off the AP after a little while (e.g. a minute or so).
It's a PITA to get this stuff worked out...
If your device starts an access point it should implement what is called a "captive portal". What that means is that any hostname the user tries to resolve ends up pointing to your device. Same thing as when you're in a coffee shop, hotel, airport and you always land on that stupid "accept the conditions of use" page. Android devices try to fetch a something.google.com page (I forget the details) and given the appropriate response cause a pop-up directing the user to the web browser to "sign in".
Once the user is on a web page of your device, you will want to show networks to join and provide a text box to enter a password. You then want to associate with the network specified by the user in STA+AP mode so your device can continue providing information to the user, such as which IP address your device got. Once the user has seen that you can turn off the AP after a little while (e.g. a minute or so).
It's a PITA to get this stuff worked out...
Re: How to know the IP address
I found this:
https://github.com/amora-labs/micropyth ... ive-portal
https://github.com/Matt4/micropython-ca ... work-setup
don't know if there are useful.
https://github.com/amora-labs/micropyth ... ive-portal
https://github.com/Matt4/micropython-ca ... work-setup
don't know if there are useful.
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: How to know the IP address
"Messy" and "expensive".
By the time we are done with the AP portion of the setup process we needed a small DHCP server (so the phone or PC gets an IP address), a rudimentary DNS server (to redirect traffic to us) and a small web server with SSL support (we are going to transmit the WiFi credentials in clear text over an unsecured WiFi).
That is a lot of code (resources) on such a small device. And all just to tell it an SSID and a PassPhrase.