Statistics: Posted by Inquisitor — Sun Nov 07, 2021 3:37 am
Code:
extern "C"
{
#include "user_interface.h"
#include "espconn.h"
}
#define SSID "InqMakers-Guest"
#define PW "Inquisitive"
#define HOST "wp"
const char* PAGE = "<!DOCTYPE html><html><head><title>Native Server Test</title> <meta name='viewport' content='width=device-width, initial-scale=1'><script>window.addEventListener('load',()=>{ setTimeout(()=>{location.reload();},500);},false);</script></head><body> <h1>Welcome to my Native Server Test</h1></body></html>";
void setup()
{
Serial.begin(115200);
while(!Serial) { Serial.print("."); delay(100); }
delay(2000);
// This is just cook-book Station connection stuff.
Serial.printf("\n\nSDK version: %s\n", system_get_sdk_version());
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;
u32 cnt = 0;
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("dis=%uus ", 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("del=%d ", rtn);
dis = NULL;
}
}
void onConnection(void* c)
{
espconn* conn = reinterpret_cast<espconn*>(c);
// Incoming connection
last = micros();
Serial.printf("\nconn(%u), ", cnt++);
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 | ESPCONN_KEEPALIVE);
}
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("rcv=%uus ", now - last);
last = now;
static char buffer[1024];
// 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'll send back a page that keeps reloading,
// so we can check for errors and/or memory leaks.
sprintf(buffer, "HTTP/1.1 200 OK\r\n"
"Server: InqPortal/5.0\r\n"
"Content-Length: %d\r\n"
"Content-type: text/html\r\n"
"Pragma: no-cache\r\n\r\n%s", strlen(PAGE), PAGE);
printIfErr(espconn_sent(conn, (u8*)buffer, strlen(buffer)), "send");
now = micros();
Serial.printf("snd=%uus ", now - last);
last = now;
}
void onSent(void* c)
{
espconn* conn = reinterpret_cast<espconn*>(c);
u32 now = micros();
Serial.printf("snt=%uus ", 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 = conn;
}
void onDisconnect(void* c)
{
u32 now = micros();
Serial.printf("dis(%p)=%uus mem=%u",
c, now - last, system_get_free_heap_size());
last = now;
}
void printIfErr(s8 esp, const char* msg)
{
if (esp)
Serial.printf("ERR - %s = %d\n", msg, esp);
}
void setupWiFi()
{
Serial.print("\nConnecting 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)));
}
Statistics: Posted by Inquisitor — Thu Nov 04, 2021 10:23 pm
Code:
#include <user_interface.h>
#include <espconn.h>
#define SSID "InqMakers-Guest"
#define PW "Inquisitive"
#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)));
}
Statistics: Posted by Inquisitor — Mon Nov 01, 2021 10:12 pm