Inconsistent connectivity via WiFi manager in ESP8266 - esp8266

I am facing a peculiar problem. The below code snippet connects to new WiFi network. No hard-coded ssid or password in the program. I am using AsyncWifiManager and AsyncWebServer modules. When I am connecting to my home WiFi router providing ssid and password in autoconnect portal, NodeMCU is getting connected and the server is working fine. But whenever I am changing the WiFi, connecting to my mobile phone hotspot, then server is not running, though I am getting local IP address in Serial Monitor.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncWiFiManager.h>
#include <FS.h>
#include <Wire.h>
AsyncWiFiManager wifiManager(&server,&dns);
// To clean previous settings. Use one time, then comment
// wifiManager.resetSettings();
// set custom static ip for portal
IPAddress staticIP(192,168,0,20); //ESP static ip
IPAddress gateway(192,168,0,1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255,255,255,0); //Subnet mask
wifiManager.setSTAStaticIPConfig(staticIP, gateway, subnet);
// Open WiFi Setup portal
wifiManager.autoConnect();
Serial.println("Connecting to WiFi..");
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
WiFi.begin();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
I am programming the NodeMCU board via Arduino IDE.

As your code uses fix parameters for IP/subnet/gateway you have to setup your different hotspots accordingly or you have the following options to choose from as you connect your ESP8266 server to different hotspots:
always being in the same network/subnetwork (all hotspots are part of a local network) you can use fix IP-addresses for all devices irrelevant of the hotspot you connect to
If you do not want to use above (=fix IP for all devices): Set the DHCP server to always give the same IP addresses to the NodeMCUs based on their MAC addresses.
You use mDNS and give the ESP8266 a fixed name and call via http://nyPreferredESP.local (allthough the IP varies) and use on your Android phone something like this APP
If you want to work with changing gateways (Devices not on the same net/subnet, access over the internet): This would require something a little more robust. Use a Dynamic DNS service along with a domain name. The Dynamic DNS will update your DNS records in almost real time if your gateway address changes. Example here
The complexity of the solution results from the factor always in the same network/subnet and a fixed gateway or everything (except MAC-address and name of the device are fix) and the rest may be variable. Read some basics about setting up a local network here

Related

Avoid-Hard-Coding-WiFi-Credentials-on-Your-ESP8266 without using Wifimanage

Is it possible? ESP8266 connect to router wifi via iOS application?
I want to make an application control something by ESP8266. My workflow bellow:
Open the app, on the device connected to wifi router.
Connect to ESP8266 and give it SSID and PASSWORD info from current Wifi.
ESP8266 receive this infos and auto connect to Wifi router.
Thank for your advices first.
I tried this way but I get user experience. Because user has to do many steps.
- Using WiFiManager, make ESP8266 as wifi router.
- Go to Wifi setting from device to connect to ESP8266 Wifi.
- Give Wifi infos (SSID, PASSWORD) by go to 192.168.4.1 from web browser.
Just note here to avoid this way
Please give me your advices.
Thank you
First of all, I don't think you made ESP8266 as a Wi-Fi router. I think you mean it goes into AP mode to prompt the user for the credentials.
I have worked around this by using the EEPROM library. You can easily find code example here
So basically:
Pre hard code credentials (can be dummy data for first run) to the EEPROM.
When ESP8266 boots up, load those credentials and try to connect to
the Wi-Fi.
If it can't connect, prompt the user to input credentials.
ESP8266 tries to reconnect. If it can connect, then write
credentials to EEPROM.
Repeat from step 2 every time it boots up.
Now instead of having user input credentials every single time, it will try to connect first. If it fails, then user will have to do extra work.
Let me know if I can help, I have work extensively on this problem and have working code.
WiFiManager requires you to:
Connect to ESP8266's AP
Go to 192.168.4.1 (or any other set address) - on iOS it should appear automatically
Click on Wifi scan
Choose the network you want your ESP8266 to connect to
Provide password if any
Click Save button
This is the minimal thing a user has to do. He/she has to connect to ESP8266's AP and provide WiFi credentials somehow. The only thing that you can change is how the user provides that credentials. If you already make your user download your iOS app, then maybe it's okay to add possibility to configure ESP8266 within your app.
In order to configure ESP8266 with WiFiManager on board your app would have to:
Scan for WiFi networks and connect to ESP8266's AP (eg. by searching
for particular SSIDs)
Send GET request to http://192.168.4.1/wifisave?s=NETWORK_SSID&p=NETWORK_PASSWORD
However, please bear in mind that it's pointless otherwise. If your user isn't required to download any app before configuring ESP8266, then don't require him/her to do it. Web applications are a way to go in these days. This approach won't require a user to download another app just to type in the WiFi credentials. WiFiManager should appear automatically after user connects to the device's AP.
I found the way to do that:
This is code on iOS application:
https://github.com/EspressifApp/EsptouchForIOS
This is code on ESP8266 run on Arduino:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
void setup() {
int cnt = 0;
//Allocate baud 115200
Serial.begin(115200);
//Mode wifi is station
WiFi.mode(WIFI_STA);
//Waiting for connect
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(cnt++ >= 10){
WiFi.beginSmartConfig();
while(1){
delay(1000);
//Check the connect and print inform if success
if(WiFi.smartConfigDone()){
Serial.println("SmartConfig Success");
break;
}
}
}
}
Serial.println("");
Serial.println("");
WiFi.printDiag(Serial);
// Allocate server
Udp.begin(49999);
Serial.println("Server started");
// Print IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Receive the package from ESPTouch
Udp.parsePacket();
//Print IP of ESP8266
while(Udp.available()){
Serial.println(Udp.remoteIP());
Udp.flush();
delay(5);
}
}

Router as Access Point + Repeater

Is there a way to use a wireless router as some sort of Access Point + Repeater?
I have a LTE Box with a SIM Card that has a build-in router which is pretty shitty.
I now want to connect a proper router to this LTE Box (via WIFI, since the LTE Box has no LAN Port) to get internet access and then connect only to the router (via LAN Port, since I don't trust in WIFI).
The answer depends on your W-Lan Router.
Some devices got a function called WDS -> Wifi Distributed Service.
You need to specify the signal and the login data for the wlan router who should distribute the original signal.

Force iOS device app to talk through the local WIFI network

I'm building an application that will run in a museum with a local area wifi network without internet access, for some strange reason I'm not able to fully "join" this network with an iOS device. Enabling internet access on this network solves the problem...
The network should provide only a web server and a DNS server, the access point has a DHCP server, android devices can connect to the network without problems.
When I try to join the network with the device it remains in a "spinning wheel" status, the DHCP server log on the debian server says it has assigned an address to the iOS device, and if I check for the wifi address with an application (like iSys o SBSettings) I see the WIFI DHCP assigned address.
But when my app (or safari) tries to connect to the web server the request is routed through the 3G connection and not completed.
In my app I'm using the standard "Reachability" framework from Apple to check the reachability of a provided host name through the wifi connection and I get 0 on the SCNetworkReachabilityFlags mask....
I'm quite sure the problem is due to the fact iOS (5.1 in my case) tries to check the reachability for some "standard" host in the network, before routing traffic through the WIFI connection.
Anyone knows what an iOS device do to "validate" a WIFI network? I can add hostnames or simple dummy services to the server machine if this can help me connect the device to a LOCAL-only network :)
It seems that iOS doesn't like to join networks without a gateway, also if the network is local you have to setup a correct gateway address.
Setting the gateway as the server itself did the trick and the device started to route TCP/IP over my local area wifi network.

Getting IP addresses of the PC's available on wifi network in android

I am able to establish connection between android and PC via Wi-fi. But this is done by hard coding the the IP address of the PC (server) in the android program. But I wanted to get IP addresses of the PC's available on the Wi-fi network programmatically. So please let me know how to scan for PC's on the network and get their respective IP address.
can you not multicast a UDP packet on the network which the server listens for and responds to with a packet containing the ip address of the server in order to set up the connection?
You should be able to find help on that topic, with some options here here and here

How to make a program that sends data through the WiFi router

I got a WiFi router connected to my PC.
What I want is to send from another device some data to my PC through the Wi-Fi adapter.
Program on the device is developed using EVC++. The one on PC - on VC++.
EDIT 1:
PC has an IP address.
Another device s IP is set at program execution. I mean WiFi IP address. And then connection to WiFi router is proceeded.
EDIT 2:
What if it uses an ethernet and wifi together?
How should I make a connection through WiFi?
WiFI is no different to usual Ethernel network.
So you just usually find out IP of other computer (in config/ask user/DNS), establish usual TCP/IP connection and send data on.

Resources