Search code examples
pythonsocketspython-requestswifirouter

Python: connecting to the IP of a device connected on a wifi repeater (WiFi NAT Router)


I have a working python script that retrieve data from a wifi device (a NodeMCU*) by connecting to its IP address:

import requests
response = requests.get('http://192.168.1.12/data.json')
data = response.json()
print(data)

This script works because my computer and my device are both connected to the same wifi SSID (let's called it the main SSID).

But my wifi signal is not strong enough for my device (because it is outdoor), so I have set up a wifi repeater** that connects to the main SSID and create an Access Point. I have then connected my device to this wifi repeater Access Point.

I used the new IP address of my device (10.24.1.2*** ) in the script above but this only works when I disconnect my computer from the main SSID and connect it to the wifi repeater Access Point. But I do not want to do that : my computer should remain on the main SSID and my device should be on the wifi repeater Access Point.

So my question is: How can I retrieve the data.json from this device which is connected to the wifi repeater, while my computer remains on the main SSID?

I looked into port-forward which the wifi repeater could do but my device doesn't seem to have a port: if I connect to http://10.24.1.2/data.json I see a bunch of data but if I connect to http://10.24.1.2/data.json:3 (or any number) I get "Not found".

But I can set up an API on my device with a specific server, path and port (though need to do some research to know how to use it)

enter image description here


*) The device is a lufdaten sensor, built from a nodemcu ) My wifi repeater is another nodemcu programmed with this https://github.com/martin-ger/esp_wifi_repeater
*
) I retrieve the new IP of my device via a TCP/telnet connection to my wifi repeater IP address with PuTTY


Solution

  • Your "repeater" does not extend the existing network as most WiFi repeaters do but is instead a WiFi router which creates a new network with a different address space. Your main WiFi router ("main SSID") is then just a single device inside this new network, which means it will use NAT to provide access from any devices inside "main SSID" to the "repeater SSID".

    But since this kind of NAT dynamically maps multiple IP to a single IP it can work by design only in one direction: connection from inside the "main network" (192.168.1/24) to the "repeater network" (10.24.1/24) can be created but no connection from the repeater network to the main network. That's actually the same reason why devices inside a home network are not directly reachable from the internet.

    To make this work you would need to create an explicit port forwarding if your router for "main SSID" can do this. Alternatively you could use a repeater which does not create a new network but which just extends the existing network. No idea if your existing device can do this since it is explicitly documented as "a WiFi NAT router".