0
votes

I am working on a project that uses the ESP32 C3 dev board and runs a webserver with dns, and comunicates with the client via websocket. here is the arduino code (the part of it relevent to the webserver):

#include <math.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
#include <time.h>
#include "SPIFFS.h"
#include <FS.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#include <ESPmDNS.h>
#include <WebSocketsServer.h>
#include <DNSServer.h>
#include <NTPClient.h>
#include <Adafruit_NeoPixel.h>
#include <driver/adc.h>


WebServer server(8000); 
DNSServer dnsServer;
WebSocketsServer webSocket = WebSocketsServer(81);
WiFiServer server2(80);
IPAddress apIPDNS(8,8,4,4);

IPAddress testIP1(192,168,1,4);
IPAddress testIP2(192,168,1,5);

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

IPAddress apIP(192, 168, 4, 3);
IPAddress netMsk(255, 255, 255, 0); 

uint8_t* data;

void onWebSocketEvent(uint8_t num,
                      WStype_t type,
                      uint8_t * data1,
                      size_t length) {

  // Figure out the type of WebSocket event
  switch(type) {
    // Client has disconnected
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", num);
      break;

    // New client has connected
    case WStype_CONNECTED:
      {
        IPAddress ip = webSocket.remoteIP(num);
     //   Serial.printf("[%u] Connection from ", num);
     //   Serial.println(ip.toString());
      }
      break;

    // Echo text message back to client
    case WStype_TEXT:
          data = data1;
          parsedata();    
      break;
    case WStype_BIN:
    case WStype_ERROR:
    case WStype_FRAGMENT_TEXT_START:
    case WStype_FRAGMENT_BIN_START:
    case WStype_FRAGMENT:
    case WStype_FRAGMENT_FIN:
    default:
      break;
  }
}

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  time_now = millis();
     
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAPConfig(testIP1, testIP1, netMsk);
  WiFi.softAP(ssid, password);
  WiFi.hostname("esp32server");
  WiFi.begin(sta_ssid, sta_password);
  dnsServer.start(53, "*", WiFi.softAPIP());
  Serial.println("connecting to wifi1...");
  delay(6000);
   if(WiFi.status() == WL_CONNECTED) { current_connected_wifi = String(sta_ssid); current_connected_password = String(sta_password);Serial.println("connected to:" + current_connected_wifi);}
   else {WiFi.begin(sta_ssid2, sta_password2);delay(6000);Serial.println("connecting to wifi2...");
   
     if(WiFi.status() == WL_CONNECTED) {  current_connected_wifi = String(sta_ssid2); current_connected_password = String(sta_password2);Serial.println("connected to:" + current_connected_wifi);}
     else {WiFi.begin(sta_ssid3, sta_password3);delay(6000);
     Serial.println("connecting to wifi3...");
    
      if(WiFi.status() == WL_CONNECTED) {  current_connected_wifi = String(sta_ssid3); current_connected_password = String(sta_password3);Serial.println("connected to:" + current_connected_wifi);}
      else { Serial.println("NO INTERNET"); WiFi.disconnect();dnsServer.start(53, "*", WiFi.softAPIP());}
     }
   }
 
   SPIFFS.begin(); 
   MDNS.begin("espserver");

  server.on("/", handle_root);
  server.on("/Chart",HTTP_GET, handle_chart);
  server.on("/Style",HTTP_GET, handle_chartcss);
  server.on("/mark",HTTP_GET, handle_img);
  server.on("/nonet",HTTP_GET, handle_img2);
  server.on("/wifilogo",HTTP_GET, handle_img3);
  server.on("/adressip", handle_adressip);
  server.on("/maxcoff", handle_maxcoff);
  server.on("/request", handle_request);
  server.on("/mintime", handle_mintime);
  server.on("/maxtime", handle_maxtime);
  server.on("/dailychart", handle_dailychart);
  server.on("/hourlychart", handle_hourlychart);
   server.on("/monthlychart", handle_monthlychart);
  server.on("/cumulative", handle_cumulative);
  server.on("/monthly", handle_monthly);
  server.on("/SSIDnames", handle_SSIDnames);
  server.on("/getmaxenergy", handle_maxenergy);
  server.begin();
  webSocket.begin();
  webSocket.onEvent(onWebSocketEvent);   
  server2.begin();
  MDNS.addService("http", "tcp", 80);    
}

void loop() {
 WiFiClient client = server2.available(); 
 dnsServer.processNextRequest();
 server.handleClient();
  webSocket.loop();
  delay(20);
}

quick explenation:

when the ESP32 starts, it checks if it can connect to the internet with any of the 3 saved credentials from eeprom (sta_ssid, sta_password). If it manages to connect with any of them, the webserver can be accesed either when connecting to the wifi of the ESP or on the network that the ESP32 is connected to. When it is unable to find a a network it can only be accesed when connected to the wifi AP of the ESP32. The webserver is accesed from either 192.168.1.4:8000 or espserver.local:8000.

This works great regardless of internet access. When i connect to the webserver i send my html and javascript code to the client and then establish a websocket connection. When the esp doesnt have internet acces the websocket connection works fine, and packets get recieved within milieconds, worst case scenario a couple of them take 2 seconds to arrive.

HOWEVER, when the ESP is not connected to the internet, the packets get stuck on pending and take forever to arive (even 10 seconds), or dont arive at all. Its almost like the webserver is too busy, even tho it has far less to do compered to when it is connected to the internet.

My question is, do i have to use a special function to disconect the webserver from the internet (where WiFi.disconect() is not enough?) or how can I make the WebServer library work better to not lose any packets?

(i have tried using the asynch websocket library, however it does not work on my specific chipset (esp32 c3) so i have to make it work with this library)

1
You're not trying to use the web page through the ESP AP while it's scanning for an AP to connect to, are you? That's going to disrupt your existing connections quite significantly if the radio is hopping around all 13 channels, looking for an AP.Tarmo
no, the program only connects to an AP during setup (WiFi.begin) with a long delay. unless there is another function in there that also does that, which i am not awere of.Aleš Kramžar
Random thought: try switching WiFi power save mode off: docs.espressif.com/projects/esp-idf/en/latest/esp32/…Tarmo

1 Answers

0
votes

I figured it out. the problem was as some people hinted, that the wifi status was 1, which meant it was constantly trying to reconnect (and using cpu time). I used WiFi.disconnect(true); to disconnect the wifi if it didnt connect to any network during setup. that solved the problem.