How to know the IP address

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
andydrizen
Posts: 8
Joined: Wed Jan 08, 2020 10:10 am

How to know the IP address

Post by andydrizen » Wed Jan 08, 2020 10:18 am

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to know the IP address

Post by jimmo » Wed Jan 08, 2020 11:55 am

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).

JohnieBraaf
Posts: 9
Joined: Mon Jan 06, 2020 12:08 am

Re: How to know the IP address

Post by JohnieBraaf » Thu Jan 09, 2020 4:12 pm

If I unerstand your question correctly, this can be solved pretty easily.

Have a look at:

Code: Select all

print(WLAN.ifconfig()[2])
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.

andydrizen
Posts: 8
Joined: Wed Jan 08, 2020 10:10 am

Re: How to know the IP address

Post by andydrizen » Fri Jan 10, 2020 8:50 pm

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).

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: How to know the IP address

Post by MostlyHarmless » Fri Jan 10, 2020 10:19 pm

These are really two separate questions.
  1. How to control the IP address of a device in your own environment?
  2. How to create an initial WiFi config (SSID and PassPhrase) in an unknown environment?
The answer to 1) can be straight forward. Don't use the DHCP functionality of a consumer grade router. Turn it OFF. Then run your own DHCP and (forwarding) DNS on some other machine. I am using a PowerEdge 2950, that is running anyway. But a Raspberry Pi can do the job if that is all you want to leave on 24/7. Any type of Linux server will do.

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

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: How to know the IP address

Post by tve » Sat Jan 11, 2020 5:58 am

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...


User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: How to know the IP address

Post by MostlyHarmless » Sat Jan 11, 2020 1:14 pm

tve wrote:
Sat Jan 11, 2020 5:58 am
A different try at answering... It's a messy problem, unfortunately. The industry answer (WPS) is insecure...
"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.


Post Reply