Here is code I have written:
#include <stdio.h>
#include <ESP8266WebServer.h>
#define MAX_WIFI_INIT_RETRY 50
#define WIFI_RETRY_DELAY 500
const char* wifi_ssid = "ssid";
const char* wifi_passwd = "password";
ESP8266WebServer http_rest_server(80);
const int numParams = 20;
String params [numParams];
String toString(String my_array[], int size)
{
String string;
for (int i = 0; i < size; i++) {
string = string + ", " + my_array[i];
}
return string;
}
int init_wifi() {
int retries = 0;
Serial.println("Connecting to WiFi AP..........");
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_passwd);
// check the status of WiFi connection to be WL_CONNECTED
while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) {
retries++;
delay(WIFI_RETRY_DELAY);
Serial.print("#");
}
return WiFi.status(); // return the WiFi connection status
}
void readParams(String (& params) [numParams])
{
int mn = WiFi.scanNetworks();
for (int i = 0; i < mn; i++)
{
// Serial.println(WiFi.SSID(i).c_str()); // prints name of all hotspots in serial monitor.
params [i] = WiFi.SSID(i).c_str();
}
}
void listHotspots() {
String results = toString(params, sizeof(params));
Serial.println(results);
http_rest_server.send(200, "text/plain", results);
}
void config_rest_server_routing() {
http_rest_server.on("/", HTTP_GET, []() {
http_rest_server.send(200, "text/html",
"Welcome to the ESP8266 REST Web Server");
});
http_rest_server.on("/hotspots", HTTP_GET, listHotspots);
}
void setup(void) {
Serial.begin(115200);
init_led_resource();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
readParams (params);
if (init_wifi() == WL_CONNECTED) {
Serial.print("Connetted to ");
Serial.print(wifi_ssid);
Serial.print("--- IP: ");
Serial.println(WiFi.localIP());
}
else {
Serial.print("Error connecting to: ");
Serial.println(wifi_ssid);
}
config_rest_server_routing();
http_rest_server.begin();
Serial.println("HTTP REST Server Started");
}
void loop(void) {
http_rest_server.handleClient();
}
when I access the root url: i.e. 192.168.0.101/ It opens the root page fine, but when I try to open the 192.168.0.101/hotspots It reboots the ESP8266 until the page stops to make a new attempts and I get nothing in result.
In the serial monitor I am seeing this exception when I try to visit the ip/hotspot page .
Exception (9):
epc1=0x4020148a epc2=0x00000000 epc3=0x00000000 excvaddr=0x4d2d5456 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffde0 end: 3fffffc0 offset: 01a0
3fffff80: 3fffdad0 3ffee553 3ffee5a4 40201347
3fffff90: 402086b8 6505a8c0 feefeffe feefeffe
3fffffa0: 3fffdad0 00000000 3ffee5e0 40206614
3fffffb0: feefeffe feefeffe 3ffe8510 40100479
<<<stack<<<
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld
Connecting to WiFi AP..........
######Connetted to Hometelecom1N5E--- IP: 192.168.0.101
HTTP REST Server Started
is it something wrong with the toString function ?