-->
Page 1 of 1

Basic OTA when different network

PostPosted: Fri Mar 26, 2021 9:00 am
by SentinelAeon
Hello,

I am using a basic OTA code to upload code upgrades to my ESP8266 over network. It is working as intended but now i have a new challenge.

Right now my ESP8266 is running on network #1, my home network. ESP8266 connects to my network over wifi and runs OTA and also a basic HTML server. Now i have to move this ESP8266 to network #2, which means i have to code into it a new SSID name and new SSID password. The IP will remain the same. So my question is, will this work:

1) I upload code to ESP8266 over network #1 as i do now (both ESP8266 and PC connected to network #1). In this upload, i change SSID name and password from network #1 to network #2. Obviously this means i can no longer access my ESP8266 OTA since ESP8266 is not connected to network #1 anymore.

2) Now i change location and move my ESP8266 near network #2 - so now ESP8266 connects to network #2 and also my pc. I connect my pc also to network #2. Can i upload my code over OTA now, as i did before ?

I hope you understand what i mean. Its quite simple really. ESP8266 is inside a box, which means connecting it with USB cable is messy - right now ESP8266 is connected to 5V through microusb, also opening the box risks unplugging some wires. So that is why i use Basic OTA to upload small code corrections to ESP8266. But i am doing that at home on my home network. Now i need to take this ESP8266 box to another place where they have a different network. So my plan was to simply input new ssid name and password into ESP8266 at home and then take this ESP8266 box to another place and hopefuly it will connect to new network. What i fear is that changing the network will somehow prevent me from using arduino IDE to upload code to it. Right now in my program i have an ip adress of ESP8266 that i can choose, next to port selection.

Re: Basic OTA when different network

PostPosted: Fri Mar 26, 2021 4:59 pm
by btidey
Theoretically it should work but as soon as you change the ssid and password then of course it will only connect to a network with those credentials. So there is a risk that if you make a mistake or it won't connect to the new network for some reason then your only means of escape is via usb.

A much better solution is to embed wifi management in the code. This means that when a connection is not possible then the code runs an AP/server which may be browsed to select a network and allow entry of the ssid and password. This provides a solution for both initial setup, handling local network changes, or for relocating the device to a new location.

Adding wifi management is actually very simple using a library like wifiManager https://github.com/tzapu/WiFiManager (Arduino environment).

Personally all my projects include wifi management plus OTA

Re: Basic OTA when different network

PostPosted: Fri Mar 26, 2021 5:39 pm
by SentinelAeon
Thank you for your answer. Let me get this straight, because you might have just solved the issue i have been thinking about for years - how to make my devices independent of the network.

So .. i can make a "blank" device with no selected ssid. So when turned on, it will detect no ssid entered and start acting like wifi AP. Then you connect to this wifi AP and write in SSID and Password and then the device will connect to that wifi ? Is there a way to permanently write that SSID and password into ESP8266, so that even if i power it off and on, it will still hold that SSID and password ?

Re: Basic OTA when different network

PostPosted: Sat Mar 27, 2021 6:44 am
by btidey
Yes. That is the purpose of WifiManager and similar libraries.

You include the library in your sketch and add a couple of calls in setup. These include a username / password if required to allow access to the AP network setup. When first run the call to WifiManager will fail to connect to a local network so it will automatically start the access point. You then use a wifi client device (e.g. phone to connect to the wifi network set up by wifiManager and browse to the wifiManager setup (default 192.168.4.1). It brings up a response allowing you to select from available wifi networks and enter the password for that network. It will then connect and reboot. The ESP8266 by default remembers the last ssid and password so this time the wifiManager connects to the selected network. This happens on all subsequent reboots until the device fails to connect (network password change or new location) when the set up procedure is automatically invoked again.

Check the link for details on the calls needed. I normally use a time out on the wifiManager connect which allows it to alternate trying the normal connect and the AP set up. This allows better handling of the case where there may just be a short term local network issue