0
votes

So I have an ESP device that is connected to my WiFi network. It runs a web server and now I want to make requests to this webserver from my android app. So I tried a couple of things and none got satisfied.

Things I tried and why not suitable for me.


Setting up mDNS:
Can't use it since android doesn't really do mDNS it fails in the lookup.

Setting a Static IP:
This approach will fail if the routers' gateway is changed. (at least that's what I believe).
(Can anyone elaborate on this ^. AFAIK if you want to make a static IP you need to match the given gateway with your router's gateway, So if I configured esp board for my router, it might not work with someone else's router.)

Setting a WebSocket connection:
To do this also need the hostname which is IP again.

Asking IP from user:
Since this is going to be a product I can't ask the user to provide the IP address in the mobile application. It leads to a lack of user experience.


I still hope that there should be another way of handling this case which I'm not aware of. I'm actually dying to get this done and I need you guys to help me with this.

2
Since this is going t be a product maybe you should hire a consultant who understands networking. - romkey
Maybe this answer on Arduino will help. - ocrdu
I think I can get this done using both ap mode and sta mode together. I will try to come up with a solution. There should be a better way of handling this. - Charitha Goonewardena

2 Answers

0
votes

It sounds like you are over complicating stuff here.

Step 1. Connect esp board to your wifi (i guess you have figured this out) step 2. check what lan address your board has connected to, for this is usually just use

    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

step 3. start your wifiserver at port 80

WiFiServer(80);

step 4. in your android app, or any other platform(i recommend using browser in computer while simply testing if it works or not) type url: local_ip_address_that_you_got_above/mygetrequest?value1=1

step 5. Listen for incoming clients and intercept the get value that you will recieve in the header in fashion of "GET /mygetrequest?value1=1"

step 6. done :)

If you want to skip the process of implementing all of it from scratch you can get full source code of my example server described above at: https://spacerival.com/lounge/2614/arduino-server but i do highly suggest you to implement it on your own and not just copy paste that source code, since you wont learn much at all to be fair

0
votes

So after trying all day long, I came with a solution. This is a bit of a workaround as I was lost. So What I did was I created a Soft AP with a small web server that returns the LocalIP. So my esp is working on both AP and STA modes right now.

How it works is simple

  1. ESP runs with a small webserver with /getLocalIP endpoint.
  2. ESP opens a SoftAP for the user to connect. The user connects with the SoftAP.
  3. The user will be asked to enter credentials for the desired WiFi.
  4. After connecting to the desired WiFi mobile app will request a for 192.168.4.1/getLocalIP (AP's IP Address).
  5. Since ESP is already connected to the WiFi this endpoint will send the WiFi.localIP() to the app.
  6. So now the app has the LocalIP.