WIP: adding HTTP server
parent
6a5b65d8c5
commit
a96d053502
Binary file not shown.
|
@ -24,6 +24,8 @@ const char* unique_softap_ssid()
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
bool connected = false;
|
||||||
|
|
||||||
Serial.begin(SERIAL_BAUD);
|
Serial.begin(SERIAL_BAUD);
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
|
@ -42,15 +44,20 @@ void setup()
|
||||||
Serial.printf_P( PSTR("Created Soft AP for setup: %s\r\n"), softApSSID);
|
Serial.printf_P( PSTR("Created Soft AP for setup: %s\r\n"), softApSSID);
|
||||||
} else {
|
} else {
|
||||||
Serial.println( F("Connected"));
|
Serial.println( F("Connected"));
|
||||||
|
connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup Web Server
|
// Setup Web Server
|
||||||
// TODO: only if not connected or explicitly enabled
|
|
||||||
uweather_web_init();
|
uweather_web_init();
|
||||||
|
// if disabled and connected, shutdown web server
|
||||||
|
if (connected) { //TODO: if connected and disabled
|
||||||
|
uweather_web_shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
uweather_web_handle();
|
uweather_web_handle();
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
45
src/web.cpp
45
src/web.cpp
|
@ -1,8 +1,19 @@
|
||||||
#include "web.h"
|
#include "web.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
#define WEB_CONTENT_BUFFER 512
|
#define WEB_CONTENT_BUFFER 512
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How long to wait after the OK to shutdown before no longer
|
||||||
|
* accepting requests
|
||||||
|
*/
|
||||||
|
#define WEB_SHUTDOWN_TIME 300000
|
||||||
|
|
||||||
|
bool web_is_shutdown = false;
|
||||||
|
unsigned long web_shutdown_at = 0; // if non-zero, this is when requests will no longer be responsed to
|
||||||
|
|
||||||
|
|
||||||
uint8_t _content_buffer[WEB_CONTENT_BUFFER] = {};
|
uint8_t _content_buffer[WEB_CONTENT_BUFFER] = {};
|
||||||
Buffer content(_content_buffer, WEB_CONTENT_BUFFER);
|
Buffer content(_content_buffer, WEB_CONTENT_BUFFER);
|
||||||
|
|
||||||
|
@ -28,7 +39,24 @@ void handle_wifi_scan(HttpRequest& req, HttpResponse& res)
|
||||||
// Handle WiFi Connect/Disconnect/Status
|
// Handle WiFi Connect/Disconnect/Status
|
||||||
void handle_wifi_state(HttpRequest& req, HttpResponse& res)
|
void handle_wifi_state(HttpRequest& req, HttpResponse& res)
|
||||||
{
|
{
|
||||||
|
res.code = 200; // assume OK unless otherwise specified
|
||||||
|
if (req.method[0] == 'P') { // PUT | POST
|
||||||
|
// Attempt to connect to connection from request
|
||||||
res.code = 501;
|
res.code = 501;
|
||||||
|
} else if (req.method[0] == 'D') { // DELETE
|
||||||
|
// Disconnect, start softAP, disable shutdown
|
||||||
|
res.code = 501;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.headers.set("Content-Type","application/json"); //TODO: flash string
|
||||||
|
if (WiFi.isConnected()) {
|
||||||
|
String ssid = WiFi.SSID();
|
||||||
|
String ipv4 = WiFi.localIP().toString();
|
||||||
|
content.printf_P( PSTR("{\"connected\": true, \"ssid\": \"%s\", \"ipv4\": \"%s\", \"rssi\": %d}"), ssid.c_str(), ipv4.c_str(), WiFi.RSSI());
|
||||||
|
} else {
|
||||||
|
content.print( F("{\"connected\": false}"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Route routes[] = {
|
Route routes[] = {
|
||||||
|
@ -43,15 +71,28 @@ void uweather_web_init(void)
|
||||||
webKernel.begin();
|
webKernel.begin();
|
||||||
webKernel.setInitHandler(web_init);
|
webKernel.setInitHandler(web_init);
|
||||||
webKernel.setTerminateHandler(web_terminate);
|
webKernel.setTerminateHandler(web_terminate);
|
||||||
Serial.println("Web Kernel initialized");
|
Serial.println(F("Web Kernel initialized"));
|
||||||
|
web_shutdown_at = 0;
|
||||||
|
web_is_shutdown = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uweather_web_handle(void)
|
void uweather_web_handle(void)
|
||||||
{
|
{
|
||||||
|
if (web_is_shutdown) return;
|
||||||
|
if (web_shutdown_at) {
|
||||||
|
unsigned long now = millis();
|
||||||
|
if (now > web_shutdown_at) {
|
||||||
|
web_is_shutdown = true;
|
||||||
|
Serial.println(F("Web Kernel is shutdown"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
webKernel.handleClients();
|
webKernel.handleClients();
|
||||||
}
|
}
|
||||||
|
|
||||||
void uweather_web_shutdown(void)
|
void uweather_web_shutdown(void)
|
||||||
{
|
{
|
||||||
Serial.println("NOTICE! web shutdown now implemented");
|
unsigned long now = millis();
|
||||||
|
web_shutdown_at = now + WEB_SHUTDOWN_TIME;
|
||||||
|
Serial.printf_P(PSTR("Web Kernel will stop responding in %d milliseconds\r\n"), web_shutdown_at);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue