- receiving the request
- responding with a simple 404
- see the browser display the 404
- waiting for the espconn_sent_callback()
- doing a espconn_disconnect() outside of the callback,
- doing a espconn_delete()
#include <user_interface.h>
#include <espconn.h>
#define SSID "Your SSID"
#define PW "Your Password"
#define HOST "wp"
void setup()
{
Serial.begin(115200);
while(!Serial) { Serial.print("."); delay(100); }
delay(2000);
// This is just cook-book Station connection stuff.
setupWiFi();
// Setup and start web server listener
static espconn listen;
os_memset(&listen, 0, sizeof(espconn));
listen.type = ESPCONN_TCP;
listen.state = ESPCONN_NONE;
listen.proto.tcp = new esp_tcp();
listen.proto.tcp->local_port = 80;
espconn_regist_connectcb(&listen, onConnection);
// This appears to be an error handler. Haven't seen it hit yet.
espconn_regist_reconcb(&listen, onError);
printIfErr(espconn_accept(&listen), "listen");
Serial.println("Ready");
}
espconn* dis = NULL;
u32 last;
void loop()
{
// The only thing we do in the loop is do the final disconnect since
// we can't do it in the onSent() callback.
if (dis)
{
u32 now = micros();
Serial.printf("disconnect = %u us\n", now - last);
last = now;
s8 rtn;
if ((rtn = espconn_disconnect(dis)))
{
Serial.printf("ERR - disconnect = %d\n", rtn);
if ((rtn = espconn_abort(dis)))
Serial.printf("ERR - abort = %d\n", rtn);
}
if ((rtn = espconn_delete(dis)))
Serial.printf("ERR - delete = %d\n", rtn);
dis = NULL;
}
}
void onConnection(void* c)
{
espconn* conn = reinterpret_cast<espconn*>(c);
// Incoming connection
last = micros();
Serial.printf("\nonConnection\n");
espconn_regist_recvcb(conn, onReceive);
espconn_regist_sentcb(conn, onSent);
espconn_regist_disconcb(conn, onDisconnect);
espconn_set_opt(conn, ESPCONN_REUSEADDR | ESPCONN_NODELAY);
// We'll use our own buffer instead of their 2920 buffer
espconn_clear_opt(conn, ESPCONN_COPY);
}
void onError(void* conn, s8 err)
{
Serial.printf("ERR=%d\n", err);
}
void onReceive(void* c, char* buf, u16 length)
{
espconn* conn = reinterpret_cast<espconn*>(c);
u32 now = micros();
Serial.printf("onReceive = %u us\n", now - last);
last = now;
static char buffer[536];
// Print out the request just to make sure its what we think it is.
memcpy(buffer, buf, length);
*(buffer + length) = 0;
// Serial.print(buffer);
// We don't care what's in there since we'll send back a 404 anyway.
sprintf(buffer, "HTTP/1.1 404 Not Found\r\nServer: InqPortal/5.0\r\n",
"Content-type: text/html\r\nPragma: no-cache\r\n\r\n");
printIfErr(espconn_send(conn, (u8*)buffer, strlen(buffer)), "send");
now = micros();
Serial.printf("sent = %u us\n", now - last);
last = now;
}
void onSent(void* c)
{
u32 now = micros();
Serial.printf("onSent = %u us\n", now - last);
last = now;
// Manual says we can't call disconnect in callback. We'll use
// cheap way of just letting the loop() do it.
dis = reinterpret_cast<espconn*>(c);
;
}
void onDisconnect(void* c)
{
Serial.printf("onDisconnect \n");
}
void printIfErr(s8 esp, const char* msg)
{
if (esp)
Serial.printf("ERR - %s = %d\n", msg, esp);
}
void setupWiFi()
{
Serial.print("Connecting to your router");
wifi_set_opmode(STATION_MODE);
station_config sc;
os_memset(&sc, 0, sizeof(station_config));
sc.bssid_set = 0;
os_memcpy(&sc.ssid, SSID, 32);
os_memcpy(&sc.password, PW, 64);
wifi_station_set_config_current(&sc);
wifi_station_set_hostname(HOST);
wifi_station_connect();
while (wifi_station_get_connect_status() != STATION_GOT_IP)
{
Serial.print(".");
delay(1000);
}
ip_info info;
wifi_get_ip_info(STATION_IF, &info);
Serial.printf("\nBrowse to (http://%s/index.html) or (http://" IPSTR
"/index.html)\n",
HOST, IP2STR(&(info.ip)));
}
w/ GUI Admin Client, Drag & Drop File Manager, OTA Built-In, Access Point Manager,
Performance Metrics, Web Socket Comms, App API, All running on ESP8266...
Even usable on ESP-01S --- Please check it out!
https://inqonthat.com/inqportal-the-three-line-promise/
https://InqOnThat.com/inqportal