Notifications
Clear all

Howto NOT hard code ip addresses

47 Posts
4 Users
11 Likes
3,032 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

I am fairly sure this can be done but I don't have all the pieces. Here is ehat I have.

An esp32 running WiFi and mqtt. The mqtt wants either a domain name or ip address. At the moment I am using an ip address.

This is your standard 192.168.0.xxx local net. I know from past experience that the ip I have coded can change. I also know I can use ifconfig to set an ip at the Pi4 that is the mqtt broker. What would be nice if it can be done is to future proof it by somehow assigning a name like mypi4-1.ron.com. That way the mqtt code NEVER needs to change and by some sort of magic the real ip gets attached to the logical name mypi4-1.ron.com (ron.com is just an example, I have no idea what I can use for a name).

Almost forgot, I have 2 routers (1 not in use) a switch and a Pi4-4 that can all be used. I suspect they would be useful, I just don't know the interconnections.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Quote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

I know with Windows, you can set a static IP address in the appalet.  I'm assuming Linux will have the same ability.  Your router will honor it as it has not other option.  If it assigned that address to another computer... well... you're screwed!  You get a network address conflict and all hell might as well break loose.

The other option is to leave your Linux using DHCP and configure your router to assign it the same address no matter how much time has lapsed.  Usually you'll find an option that receives its MAC address which is constant and assigns it the the configured IP address.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ron bentley and DaveE reacted
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1695
 

Hi @zander Ron,   

(Whilst I was writing this, @Inq has briefly described the same suggestion as 'other option' above)

re:

This is your standard 192.168.0.xxx local net. I know from past experience that the ip I have coded can change.

I see you say "ip I have coded can change", but not why it changes, or more importantly, if there is some reason I haven't thought of, why it MUST change.

Hence, I am suggesting a method to avoid it 'accidentally' changing.

---------------

Of course, the Internet generally relies on DNS servers to provide the name to IP address mapping look up, but in principle at least, it is possible to configure (client) devices with fixed mappings .. thus if you have one web server (say) and four potential client PCs to browse the web server, then (I believe) you can type this mapping into each of the clients.

Sorry, but I don't bother to even remember how to do it, as I just never use it, but a simple Google would advise which file needs to be created, etc.

---------------------------

Sorry, I don't know if MQTT, or anything else is making extra demands, but my genearal KISS approach to local net IP addressses is to assign and fix the IP addresses, according to each device's MAC address.

I am assuming your ESP32 has a fixed MAC address, there is NO FUNDAMENTAL reason why it shouldn't always use the same IP adddress, and it is always connecting to the same router.

If this isn't possible in your case, or you have already discounted this method, apologies for adding noise to your inbox.

===========

I am assuming your router supports what I am suggesting .... I have had about 5 different ones over the last 20-ish years and they all have, so I don't think this is a problem.

===========

My simple, if initially tedious, approach is to assign addresses to all 'known' devices ... or at least those whose address I want to stay fixed, like my networked printer which would be a nightmare if it kept getting a different address.

ie

Use router dhcp function to assign addresses to all devices connecting on that 192.168.0 subnet.

      a/ Mentally divide the 254 possible xxx addresses into two groups say 1-127 and 128-254.

(NB 0 and 255 are 'magic' addresses and not available for this purpose.)

      b/ Tell the router to use addresses 128-254 for devices which do not have an 'MAC assigned' address

      c/ For each device you want a fixed IP address, (such as your ESP32), then at the router, manually assign an IP from the 1-127 range, and attach to its MAC address.

........

Personally, I try to identify all of my PCs, phones, etc and give them fixed addresses, but this is probably me being a bit 'over the top' ... you may only need to do it for your ESP32 - the rest of your devices will slot into the addresses 128 and above automatically.

.......

Hope this is helpful. Best wishes, Dave

 


   
ron bentley and Inq reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@inq I have assigned a static ip as I think I mentioned. I am unsure how that helps. In my esp32 sketch I want to use a symbolic name like "pi4-1.ronsniftynetwork.com" so now the sketch will always work even if the real ip changes. I think thats called a name server but I ws hoping to not build a server. Is it possible? I have looked in my router, there is nothing there. I haven't checked my big router though, maybe it has something. What should I look for?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@davee Thanks, that was helpful but the tiny mobile router has no ability to do that so I will need to add another question, How to connect two routers so that all devices can reach the internet and each other. I have no need for the esp32 to have any address although I am sure it does, it's only sending.

So if I can get the other router into the mix I will divide up the addresses as you suggest. I have 2 phones, 3 tablets, laptop, thermostat, solar controller, battery monitor, solar brain, inverter, and almost a dozen cameras that can all be random and change, but the home server pi4, development pi4, and now maybe a pi based router all need access by name or at a minimum fixed IP.

Your answer helped a lot.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @zander

@inq I have assigned a static ip as I think I mentioned. I am unsure how that helps. In my esp32 sketch I want to use a symbolic name like "pi4-1.ronsniftynetwork.com" so now the sketch will always work even if the real ip changes. I think thats called a name server but I ws hoping to not build a server. Is it possible? I have looked in my router, there is nothing there. I haven't checked my big router though, maybe it has something. What should I look for?

You didn't mention a static ip in this thread.  You might have in some other random thread, but my memory does not span all users' details about their own networks.  But...

Setting a static IP address on all servers internal to your network is standard procedure even in large corporations.  Certainly, you should set a MQTT Broker as a fixed, static address.  Like @davee said about a network printer... it'd be a pain otherwise!  

As far as your mobile router not being able to assign IP's to specific devices, are you sure?  Even my very common, $23 GL.iNet mobile router can do that kind of function.  It is under "More Settings/LAN IP/Static IP Address Binding".  I've never seen any router that can't do that function.

Now, if you do have this very special router, there is another way.  Using this method, you do not have to dink with MAC addresses.  Hopefully, your router will have this capability...

  1. Set its range (you mentioned 192.168.0.xxx).  You're good to go!
  2. There should be a router settings ability to restrict the "Start IP Address" and "End IP Address" that the DHCP of the router will hand-out to clients like your laptop, phones, tablets, etc.  Set these to be a subset of your full range.  Say...
    1. Start = 192.168.0.1
    2. End = 192.168.0.100
  3. Now simply set your servers (Linux, Windows) to use static IP addresses in the range above 100.  Say... your MQTT server is 192.168.0.101, etc.  All you need to do is make sure your servers all use unique IP's.

You're router will will still route traffic to/from this "101" MQTT server, but will never assign it to any client.

NOW to the topic at hand...

"Howto NOT hard code ip addresses" - Most all routers I've seen will handle DNS capabilities within your LAN.  So if the computer name of your server is MyMQTTBrokeryou should be able to use the computer name instead of a hard-coded IP address. 

I know I routinely give my ESP8266/InqPortal web server projects computer names, like:  InqlingJr ... and from any browser, I access it by:  http://inqlingjr/admin.html.  

The same thing can be done with a MQTT connection... instead of having the hardcoded IP address, "192.168.0.101", you should be able to put "MyMQTTBroker" and nothing else needs to change.

VBR,

Inq

 

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@inq The small router has neither a dns or mac to ip function BUT the big router does. I have the ethernet cable from the small router going to a switch but I will now plug that into the wan port of the big router swap ssid's and be back in business. I tried what you say about using the localhost name but no luck.That little router is very special so I will get it out of the loop by treating it as a modem then I think I know what to do. The idea of splitting the range in two parts for static and dynamic is the key. Thanks.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @zander

The small router has neither a dns or mac to ip function

🤣 Sounds like its ready to be a boat anchor!  

I've had really good luck with a couple of these.  I use them in our classes so the students can talk to their InqPortal projects from the computers/phones/tablets.  I've had over a dozen connected through them without issue.  They also has some pretty nice features to use a VPN and thus protect you when your out in public settings.  

Oops, they've gone up in price - now $28.  

"https://www.amazon.com/GL-iNET-GL-MT300N-V2-Repeater-300Mbps-Performance/dp/B073TSK26W"

https://www.amazon.com/GL-iNET-GL-MT300N-V2-Repeater-300Mbps-Performance/dp/B073TSK26W

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@inq I AM AN IDIOT!!!!!!!!!!

Yes, I have both the dhcp setting, and the MAC to IP settings. I expected to see one thingand saw something else and made a very stupid conclusion. 

I DO know enough that when @Inq insists I will have that functionality in the router I WILL go back and look harder. Of course it is there, I even played with the DHCP yesterday without fully understanding what I was doing. I think I do now. Tell me if the picture makes sense. Also from the picture, can I make any use of the 'Default Domain' and can I add a secondary DNS like a local RaspberryPi for 'local' name service? Second picture is the menu, is there anything else I should be paying attention to for my little local net, maybe future internet access?

Screen Shot 2022 07 26 at 09.06.48
Screen Shot 2022 07 26 at 09.11.48

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Inq reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@inq That looks like and sounds like what I am using. The reason I got mine was it is supposed to be able to connect to both wifi and phone. I was hoping for an auto fail over but have not got the phone part to work at all. I probably will never make use of it as I will switch to Starlink soon and have no need.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@davee Thanks, I totally missed the concept of splitting the dhcp range. Simple yet effective!

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @zander

Yes, I have both the dhcp setting, and the MAC to IP settings. I expected to see one thingand saw something else and made a very stupid conclusion. 

So you can do either method of making your "servers" have a constant IP address.

Version 1 - My very first post at the top describing "other option" and @davee 's more thorough post explained leaving all your computers to let your router make all the decisions.  It's the way I prefer.  All connections (severs and clients) all request to be assigned an IP address from DHCP and the router is configured to give the clients a random IP from a range and the servers (from their MAC addresses) are assigned a static IP address every time they ask for it.

Version 2 - Was my later post... if you didn't have MAC/IP assigning will do effectively the same thing.  BUT in that situation the server computer itself tells the router what static IP it wants.

Either way gives you the important part of making sure the computers you need to reach always are using the same address.

Posted by: @zander

Also from the picture, can I make any use of the 'Default Domain' and can I add a secondary DNS like a local RaspberryPi for 'local' name service? Second picture is the menu, is there anything else I should be paying attention to for my little local net, maybe future internet access?

I don't know what 'Default Domain' actually does.  My routers don't have that option and I Googled it and it says its an older feature on some routers.  

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @zander

@inq That looks like and sounds like what I am using. The reason I got mine was it is supposed to be able to connect to both wifi and phone. I was hoping for an auto fail over but have not got the phone part to work at all. I probably will never make use of it as I will switch to Starlink soon and have no need.

Yes, that sounds familiar.  The one I use in the house will:

  1. connect to another router to extend the range out further in the house 
  2. connect via Ethernet to another router to extend range
  3. connect via USB to a phone or hotspot to supply Ethernet and/or WiFi.

... and I'm waiting for Starlink so I can stop using my crappy Verizon cellphone for Internet!

I understand they will now let you use Starlink for your RV when you're traveling.  I wanted it for my boat, but read recently they want $10K for the hardware and $5K per month for the service!  Ouch!  My boat isn't work $10K.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7008
Topic starter  

@inq Slowly getting there. I set up the dhcp to 192.168.0.2 to 192.168.0.99. Those are the dynamic, my laptop for example is 192.168.0.11.

I thought the MAC to IP didn't work for 192.168.0.100 because it is outside the 2 to 99 so I used a dhcpcd.conf entry to assign the PI server the 192.168.0.100 address.

The problem is the laptop can't pig the PI and the PI can't ping the laptop.

I am going to remove the PI dhcpcd.conf entry and instead make a mac to ip assignment in the router. I like the central approach. If you see a problem with this let me know, I will report back.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1695
 

Hi Ron @zander and @inq,

  You may both be fully up and running now, but this discussion has prompted me to do some research, and I am writing another mini-sermon of what I found .. I hope it will be useful to someone, as I found it relatively hard to discover some of the story. As always, apologies for any new errors on my part ...

----------

  Thanks for Ron and Inq for the kind acknowledgements of my last effort in this thread.

  I am finding that one of the results of attempting to help someone, is that I am finding out more .. partly as a result of having to do some searching on the net, etc. for stuff I don't know, and partly as a result of others, (Inq in this case), filling in the gaps in my limited undertstanding. So, thanks @Inq for your helpful reply above, which caused me to go and check some things. Of course, this does mean I have spent a few hours diverted from what I intended, but that is how research often works!

--

And before getting into the 'new stuff', a minor correction to my previous note ... "into two groups say 1-127" should read "into two groups say 2-127". Address "1" is also 'magic', it is the address of the router itself!

------

Inq correctly says:

"Howto NOT hard code ip addresses" - Most all routers I've seen will handle DNS capabilities within your LAN.  So if the computer name of your server is MyMQTTBrokeryou should be able to use the computer name instead of a hard-coded IP address. 

The trouble I had, is on first read, it didn't ring any bells on how to achieve it. Perhaps, I haven't been looking in the right places, but I have never seen it in the router set up manuals, etc. The only mention of DNS I had found before, is the ability to set the DNS address used ... e.g. you might choose any one of Google, CloudFlare, your ISP, etc, to provide your DNS services. This can be useful in some circumstances, but not for finding servers on your local 192.168.xxx.yyy servers.

Large organisations may run their own 'full' DNS servers, which will resolve all local domain addresses, and pass external domain addresses to appropriate external suppliers, such as those mentioned above, but this is usually 'over the top' for a small network. 'Well known exceptions' to this general observation, include 'Pi-hole' and some open source software downloads for commercial home routers, but they all require significant effort, and probably cash in most cases.

However, it seems home routers do 'often' include a limited DNS service, allegedly based on dnsmasq or similar, although I don't recall any mention of it in the 'official' router manuals I have seen. For most home users, it probably only  used as a 'cache' service, so that multiple calls in a short space of time, to resolve the same external address, only need one call to the external DNS servers. So this should speed up some web browsing, etc.

Obviously, this 'cache' service would not automatically enable resolution of local (say 192.168.xxx.yyy) names to addresses, as it relies on the external servers to provide the necessary mapping. However, it seems that the cache can also be filled by the preset entries in the DHCP server.

i.e. if your local server has a MAC address 11 : 22 : 33 : 44 : 55 : 66, then you can use the router's DHCP service, to assign it a fixed IP address, say 192.168.0.77 AND a host name, gertrude-machine

(I had noticed 'host name' before, but only used it as an identifier for my own purposes... it hadn't occurred to me it had any useful connection ... I am the only one who is so stupid? 🙄 🙄)

This will then put an entry into the cache of gertrude-machine to 192.168.0.77

So you can can now use either ping 192.168.0.77 or ping gertrude-machine to see if the server is listening.

(I am assuming the same applies to using it for a web, database, MQTT, etc. server, but I have only tested it with ping so far!)

----------

A further twist then appeared ... the router was very 'picky' about the characters it would allow in the host name, not allowing say a full stop "." . I had seen this previously, but just put it down to some harmless software 'feature'. However, we are all used to web addresses like "Google.com", which clearly has a full stop.

Then, in another page of the router's set up webpage screens, I discovered a field that was blank, labelled "RT-AX56U's Domain Name" ... where "RT-AX56U" is my router's model type. This field is similarly not discussed in the manual, but I finally realised what it meant ... it sets the TLD Top Level Domain for the local network ... equivalent to say "com" in Google.com.

Thus if Gertrude, from the example above, wanted to call her local network, "glan", she could put "glan" in this box, and her server would be called gertrude-machine.glan for the purposes of ping, and presumably web, database, MQTT etc.

---------------------

To illustrate this, I have a few screen snips from my router below ... unless you have the same router, yours will be different, but perhaps they will help you find the relevent pages.

DHCP enabled, with auto assign to addresses above 100

image

 

image

A particular machine ... note that the "Client Name", "dave-Fans.lan" is NOT the "Host name" used by ping etc... it is just a title I typed in ... I haven't yet found out it if has any other significance. the "Host Name" is "dave-fans" in the right hand box.

image

("Fans" refers to the ridiculous LED covered spinning fans towards the front of the PC box ... not my fan club members, who could all meet in a telephone kiosk, with room to spare!)

----------------

The Domain Name for the router is set to deflan in the LAN - LAN IP page

image

Empirically, I found the choice seems fairly open ... obviously MUST avoid all of the TLDs in use such as com, net, org and countries like uk, fr, etc.

 In my Googling, I found the Domain Name should 'officially' be "home.arpa", which was apparently chosen as it is defined as never be chosen as a "real" TLS  ... the connection to Arpanet, the Internet's forefather reflects there are several xxx.arpa s , some of which predate DNS .. see https://en.wikipedia.org/wiki/.arpa for a few words on the subject

And another note suggested certain key names .. "Local" and "Localnet" were mentioned, with the suggestion that there are others, will not work as they have specific meanings which results in them being filtered ... so maybe choose a "TLD" that is not an obvious name and more than about 3 characters

------------

And to test operation, ping from a command terminal window on another machine on the same local network

image

----------------------------------

Hope this helps and best wishes, Dave


   
Inq reacted
ReplyQuote
Page 1 / 4