Unable to get data from webserver at certain networks
Posted: Thu Jan 20, 2022 10:01 am
Hi!
I have this code running on my NodeMCU:
When I connect both ESP and my PC in my main network, messages send by the WebServer are shown on the Serial Monitor. When I connect both ESP and PC to my smartphone hotspot (without internet connection), "handleSend" is never called, even though the WebServer can be accessed . How can I make sure to aways be able to communicate with the ESP through the server?
I have this code running on my NodeMCU:
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <strings_en.h>
#include <WiFiManager.h>
#define INTERVALO_WIFI 30000
bool controle_auto_rec = false;
bool shouldSaveConfig = false;
const int PIN_AP = 14;
ESP8266WebServer server(80);
char htmlResponse[1000];
unsigned int tempo_decorrido = 0;
void handleRoot() {
snprintf(htmlResponse, 1000,
"<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
<meta charset=\"utf-8\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<h1>Mensagem</h1>\
<input type='text' name='msg_to_send' id='msg_to_send' size=2 autofocus> mensagem \
<div>\
<br><button id=\"send_button\">Enviar</button>\
</div>\
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\
<script>\
var txt;\
$('#send_button').click(function(e){\
e.preventDefault();\
txt = $('#msg_to_send').val();\
$.get('/send?txt=' + txt, function(data){\
console.log(data);\
});\
});\
</script>\
</body>\
</html>");
server.send(200, "text/html", htmlResponse);
}
void handleSend() {
Serial.println("Handle send");
if (server.arg("txt")!= "")
Serial.println("Mensagem: " + server.arg("txt"));
}
void setup() {
Serial.begin(115200);
delay(10);
pinMode(PIN_AP, INPUT);
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.autoConnect("ESP_AP","12345678");
server.on ( "/", handleRoot );
server.on ("/send", handleSend);
server.begin();
Serial.println ( "HTTP server started" );
}
void loop() {
if(WiFi.getMode() == WIFI_STA && WiFi.status() == WL_CONNECTED && controle_auto_rec == false) {
Serial.println("Setar persistencia");
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
controle_auto_rec = true; }
if((WiFi.getMode() == WIFI_AP || WiFi.status() != WL_CONNECTED) && controle_auto_rec == true) {
controle_auto_rec = false;
Serial.println("Setar controle false"); }
if((millis() - tempo_decorrido >= INTERVALO_WIFI) && WiFi.status() != WL_CONNECTED && WiFi.getMode() == WIFI_STA) {
Serial.println("Reconectando");
WiFi.reconnect();
tempo_decorrido = millis(); }
if(digitalRead(PIN_AP) == HIGH) {
Serial.println("resetar");
WiFiManager wifiManager;
wifiManager.resetSettings();
delay(500);
wifiManager.resetSettings();
delay(500);
ESP.restart(); }
server.handleClient();
}
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entrou no modo de configuração");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void saveConfigCallback () {
Serial.println("Configuração salva");
Serial.println(WiFi.softAPIP());
}
When I connect both ESP and my PC in my main network, messages send by the WebServer are shown on the Serial Monitor. When I connect both ESP and PC to my smartphone hotspot (without internet connection), "handleSend" is never called, even though the WebServer can be accessed . How can I make sure to aways be able to communicate with the ESP through the server?