2

what is the best way to init wifi communication between two devices without knowing IP of second device, let's say Raspberry Pi and ESP8266?

I want to build simple home automation system for my purpose. There will be Raspberry Pi, as heart of system and ESP8266 as controlled devices. What is best way to get them communicate? I think, the hardest think is how to get their IP address. Then it will be easy to communicate via sockets.

In next text, by RPi I mean Raspberry Pi and program in RPi will be in python (but using call of terminal commands when needed).

My idea is this: On ESP8266 will be running program, which will create WiFi Acces Point at start (on first run in network). Name (SSID) of that AP will be specific (let's say starting with ESP-AUTOMATION prefix), so Raspberry Pi will know, when new ESP device will be turned on (Raspberry Pi will repeatedly search for accesible networks). When new ESP appear, RPi will save own IP to variable and connect to newly searched network (let's say ESP-AUTOMATION-abcd). Then will RPi send to AP (ESP) it's (RPi's) IP address and local network SSID and password. Then it will disconnect, and connect back to local network (and gain it's "old" IP, which it previously saved to variable). Then ESP connect to local network with static IP and after succesfull connect, it will send to RPi it's IP address via socket (because IP addr of RPi it will already known, because RPi have send it when it served as AP). Then RPi will save that IP address. And because IP addresses (of RPi and also ESP) will be configured as static, it shouldn't change over time.

Is this good idea? Or is something of that unreal (I am at beginning of that all)?

PS: I really want completely autonomous system, so for me is not solution something like connect ESP to PC and print it's IP to serial and then it write to RPi...

Thanks for every advice/opinion.

4
  • 1
    That seems very overcomplicated to me. While not knowing, how exactly you would implement this on the ESP and Pi, you could let the Pi broadcast a special message over the network, which identifies it as your automation application server. All clients in the network will get this special message. Non-automation devices won't know, what to do with this message, thus they will ignore it. The ESP can wait for this message and when it arrives, it can save the IP, that the message came from (source IP). Then the ESP can contact the Pi by IP.
    – chrisl
    Dec 1 '20 at 22:58
  • @chrisl Oh, seems very good and "simple" at the same time (y). Probably I will do it this way. THANKS!
    – Petr Marek
    Dec 2 '20 at 8:04
  • 1
    One mechanism you could use for this is called "multicast DNS (mDNS)". This is supported by both the Raspberry Pi and the ESP8266. See also resolving Raspberry Pi address through mDNS with NodeMCU
    – StarCat
    Dec 2 '20 at 8:58
  • you give the rpi a fixed IP, and on boot have the ESPs send that IP a check-in http request, which will reveal their IP to the pi.
    – dandavis
    Dec 2 '20 at 20:17
1

One is a server and the other is a client. The server do not need to know IP of the client, but the client need to know IP address of the server. See more in communication between two Arduino

4
  • But when server (RPi) will need to control clients? (For example switch lamp...)
    – Petr Marek
    Dec 2 '20 at 8:05
  • @RozonaZoro The server will automatically get to know the client IP, when the first request from the client comes in. It would just have to save it. But the question seems to focus on the step before that. When the client doesn't know the IP of the server, how could it acquire the IP automatically?
    – chrisl
    Dec 2 '20 at 8:07
  • Yes, the biggest problem is your last question. Probably I will solve it with sending broadcast message from RPi, as have suggested @chrisl.
    – Petr Marek
    Dec 2 '20 at 9:44
  • 1
    It can be possible if the server support mDNS (Multicast DNS) Dec 3 '20 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.