web kernel with some stubs

master
Kenneth Barbour 2020-01-01 10:41:39 -05:00
parent a2f1cad56f
commit 6a5b65d8c5
7 changed files with 91 additions and 7 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.pio
*.swp

View File

@ -13,6 +13,8 @@ platform = espressif8266
board = esp12e
framework = arduino
upload_speed = 921600
build_flags =
!bin/version.sh
build_flags =
!bin/version.sh
extra_scripts = pre:bin/firmware_rename.py
lib_deps = https://github.com/kenbarbour/HttpServer

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,7 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "version.h"
#include "web.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -43,13 +44,13 @@ void setup()
Serial.println( F("Connected"));
}
while (true) {
Serial.print(" . ");
delay(1000);
}
// Setup Web Server
// TODO: only if not connected or explicitly enabled
uweather_web_init();
}
void loop()
{
uweather_web_handle();
}

57
src/web.cpp 100644
View File

@ -0,0 +1,57 @@
#include "web.h"
#include <Arduino.h>
#define WEB_CONTENT_BUFFER 512
uint8_t _content_buffer[WEB_CONTENT_BUFFER] = {};
Buffer content(_content_buffer, WEB_CONTENT_BUFFER);
// Initialize Connection
void web_init(HttpRequest& req, HttpResponse& res)
{
content.clear(); // clear content buffer
res.content = &content; // response content buffer
}
// Terminate Connection
void web_terminate(const HttpRequest& req, const HttpResponse& res)
{
Serial.printf("[%s] %s %d\r\n", req.getMethod(), req.getUrl(), res.code);
}
// Handle WiFi Scan
void handle_wifi_scan(HttpRequest& req, HttpResponse& res)
{
res.code = 501;
}
// Handle WiFi Connect/Disconnect/Status
void handle_wifi_state(HttpRequest& req, HttpResponse& res)
{
res.code = 501;
}
Route routes[] = {
{ GET, "/wifi/scan", handle_wifi_scan},
{ GET | POST | DELETE, "/wifi", handle_wifi_state}
};
WebKernel webKernel(80, routes, sizeof(routes)/sizeof(routes[0]));
void uweather_web_init(void)
{
webKernel.begin();
webKernel.setInitHandler(web_init);
webKernel.setTerminateHandler(web_terminate);
Serial.println("Web Kernel initialized");
}
void uweather_web_handle(void)
{
webKernel.handleClients();
}
void uweather_web_shutdown(void)
{
Serial.println("NOTICE! web shutdown now implemented");
}

23
src/web.h 100644
View File

@ -0,0 +1,23 @@
#ifndef _UWEATHER_WEB_H_
#define _UWEATHER_WEB_H_
#include <WebKernel.h>
/**
* Initalizes and stards the web kernel during boot
* or after a shutdown
*/
void uweather_web_init(void);
/**
* Handle web tasks such as handling requests.
* Call this periodically
*/
void uweather_web_handle(void);
/**
* Start the web shutdown sequence
*/
void uweather_web_shutdown(void);
#endif /** _UWEATHER_WEB_H_ include guard */