Async Web Server RP2040 returning ERR_CONNECTION_REFUSED?

18 Views Asked by At

I'm developing a project on Raspberry Pi Pico W and I'm trying to setup a custom Wi-Fi Manager using AsyncWebServer_RP2040W library and AsyncTCP_RP2040W on Arduino Pico. The code for the async server are bellow:

#include <Arduino.h>
#include "AsyncTCP_RP2040W.h"
#include "AsyncWebServer_RP2040W.h"
#include <WiFi.h>
#include <LittleFS.h>

// Wi-Fi Manager definitions
const char *WIFI_USER = "WIFI_MANAGER";
const uint8_t WIFI_CHANNEL = 6;
const uint8_t WIFI_PORT = 80;

// Read File from LittleFS
String readFile(fs::FS &fs, const char * path) {
  Serial.printf("Reading file: %s\r\n", path);
  File file = fs.open(path, "r");

  if(!file || file.isDirectory()) {
    Serial.println("- failed to open file");
    return String();
  }
  
  String fileContent;
  
  while(file.available()) {
    fileContent = file.readStringUntil('\n');
    break;     
  }
  
  return fileContent;
}

// Write file to LittleFS
void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\r\n", path);
  File file = fs.open(path, "w");
  
  if(!file) {
    Serial.println("- failed to write file");
    return;
  }

  if(file.print(message)) {
    Serial.println("- writed file");
  }
  
  else {
    Serial.println("- failed to write file");
  }
}

void setup() {
  // Init serial, LittleFS and ON LED
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  LittleFS.begin();

  // Load values saved in LittleFS
  ssid = readFile(LittleFS, ssidPath);
  pass = readFile(LittleFS, passPath);

  Serial.println(ssid);
  Serial.println(pass);

  if(ssid == "") {
    AsyncWebServer server(WIFI_PORT);
    // Connect to Wi-Fi network with SSID and password
    Serial.println("Beggining AP...");
    WiFi.softAP(WIFI_USER, NULL, WIFI_CHANNEL);
    IPAddress IP = WiFi.softAPIP();
    Serial.print("AP IP: ");
    Serial.println(IP);

    // Web Server Root URL
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send(LittleFS, "/index.html", "text/html");
    });
    
    server.serveStatic("/", LittleFS, "/");
    
    server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
      int params = request->params();

      for(int i=0;i<params;i++) {
        AsyncWebParameter* p = request->getParam(i);
        
        if(p->isPost()) {

          // HTTP POST ssid value
          if (p->name() == PARAM_INPUT_1) {
            ssid = p->value().c_str();
            Serial.print("SSID: ");
            Serial.println(ssid);
            // Write file to save value
            writeFile(LittleFS, ssidPath, ssid.c_str());
          }
          
          // HTTP POST pass value
          if (p->name() == PARAM_INPUT_2) {
            pass = p->value().c_str();
            Serial.print("Password: ");
            Serial.println(pass);
            // Write file to save value
            writeFile(LittleFS, passPath, pass.c_str());
          }
        }
      }
      request->send(200, "text/plain", "Finished. The device will restart");
      vTaskDelay(pdMS_TO_TICKS(3000));
      rp2040.reboot();   // restart device
    });
    server.begin();
  }
}

The software needs to detect if there are a connection avaliable and if aren't, it opens an AP and set up a HTML page where the user can send the parameters for the Wi-Fi network and these params are saved in a .txt file using LittleFS, so the system reboots and on the next boot, it can read the file with the Wi-Fi credentials and connect to network. The code compiles and uploads to the Pico, but when I connect to the AP and type the IP in the browser, it gives a ERR_CONNECTION_REFUSED error. I tried to change various parameters without success, and earlier versions of the code opened the page, but now doesn't open at any cost. Can someone give a hint?

0

There are 0 best solutions below