ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By seaurchin
#87277 I am noob trying to use ESPAsyncWebServer.h,

in a serving a rest api that returns wifi hotspots.

Code: Select allString* getWifiHotspots() {
//  String* arr = new String[];
  String arr[100];
  Serial.print("Scan start ... ");
  int n = WiFi.scanNetworks();
  for (int i = 0; i < n; i++)
  {
    arr[i] = WiFi.SSID(i).c_str();
  }
  return arr;
}


But when in the setup function I try to setup server resource like this:

Code: Select all     ap_server.on("/hotspots",HTTP_GET, [](AsyncWebServerRequest * request) {
        request->send_P(200, "text/plain", getWifiHotspots);
        });


I am getting this error

Code: Select all/libraries/ESPAsyncWebServer/src/ESPAsyncWebServer.h:244:10: note: void AsyncWebServerRequest::send_P(int, const String&, const char*, AwsTemplateProcessor)
     void send_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr);
          ^
User avatar
By seaurchin
#87278 The full code is here:

Code: Select all#include <Arduino.h>
#include <Hash.h>
#include "ESP8266WiFi.h"
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

const char* ap_ssid     = "access_point_webpage ";
const char* ap_password = "0123456789";

// Create AsyncWebServer object on port 8080
AsyncWebServer ap_server(80);


const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 1.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
  </style>
</head>
<body>
  <h2>Connect to a Wifi Networks</h2>
  <p>
    <form action="#">
  <label for="wifi">Choose a hotspot to connect:</label>
  <select name="wifi" id="wifi">
    <option value="hotspot">None</option>
  </select><br>
  <label for="wifi">Enter password:</label>
  <input type="text"></input>
  <br><br>
  <input type="button" value="Rescan">
  <input type="submit" value="Connect">
  </form>
  </p>
</body>

</script>
</html>)rawliteral";

String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(1.1);
  }
  else if(var == "HUMIDITY"){
    return String(1.2);
  }
  return String();
}


void setup() {
  Serial.begin(115200); 
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ap_ssid, ap_password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

  // Print ESP8266 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  ap_server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send_P(200, "text/html", index_html, processor);
 });
     ap_server.on("/hotspots",HTTP_GET, [](AsyncWebServerRequest * request) {
        request->send_P(200, "text/plain", getWifiHotspots);
        });
  // Start server
  ap_server.begin();
}

String* getWifiHotspots() {
//  String* arr = new String[];
  String arr[100];
  Serial.print("Scan start ... ");
  int n = WiFi.scanNetworks();
  for (int i = 0; i < n; i++)
  {
    arr[i] = (const char*) WiFi.SSID(i).c_str();
  }
  return arr;
}

void loop() {
  // put your main code here, to run repeatedly:

}
greatly appreciate if you can help me make it work ;)