mirror of
https://github.com/k3ng/k3ng_rotator_controller.git
synced 2025-03-13 15:46:26 +00:00
webserver
This commit is contained in:
parent
13430c06e9
commit
fa79e461c2
@ -1867,7 +1867,9 @@ struct config_t {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void
|
||||
wifiSetup(void);
|
||||
|
||||
/* ------------------ let's start doing some stuff now that we got the formalities out of the way --------------------*/
|
||||
|
||||
void setup() {
|
||||
@ -1888,6 +1890,8 @@ void setup() {
|
||||
|
||||
initialize_interrupts();
|
||||
|
||||
wifiSetup();
|
||||
|
||||
run_this_once();
|
||||
|
||||
|
||||
@ -1895,6 +1899,8 @@ void setup() {
|
||||
|
||||
/*-------------------------- here's where the magic happens --------------------------------*/
|
||||
|
||||
void wifiLoop(void);
|
||||
|
||||
void loop() {
|
||||
|
||||
#ifdef DEBUG_LOOP
|
||||
@ -2093,6 +2099,8 @@ void loop() {
|
||||
check_serial();
|
||||
#endif
|
||||
|
||||
wifiLoop();
|
||||
|
||||
} // loop
|
||||
|
||||
/* -------------------------------------- subroutines -----------------------------------------------
|
||||
|
146
k3ng_rotator_controller/wifi.cpp
Normal file
146
k3ng_rotator_controller/wifi.cpp
Normal file
@ -0,0 +1,146 @@
|
||||
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <LEAmDNS.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "ENRICO"
|
||||
#define STAPSK "cqdxcqdx"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
|
||||
extern float azimuth;
|
||||
extern float raw_azimuth;
|
||||
extern int analog_az;
|
||||
|
||||
void handleRoot() {
|
||||
|
||||
std::string reply = "Pico W rotator<br/>";
|
||||
|
||||
reply += "Azimuth: " + std::to_string(azimuth) + "<br/>";
|
||||
reply += "Raw Azimuth: " + std::to_string(raw_azimuth) + "<br/>";
|
||||
reply += "Analog Azimuth: " + std::to_string(analog_az) + "<br/>";
|
||||
|
||||
server.send(200, "text/html", reply.c_str());
|
||||
}
|
||||
|
||||
|
||||
void handleNotFound() {
|
||||
String message = "File Not Found\n\n";
|
||||
message += "URI: ";
|
||||
message += server.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
for (uint8_t i = 0; i < server.args(); i++) {
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
server.send(404, "text/plain", message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void wifiSetup(void) {
|
||||
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
if (MDNS.begin("picow")) {
|
||||
Serial.println("MDNS responder started");
|
||||
}
|
||||
|
||||
server.on("/", handleRoot);
|
||||
|
||||
server.on("/inline", []() {
|
||||
server.send(200, "text/plain", "this works as well");
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
// Hook examples
|
||||
|
||||
server.addHook([](const String & method, const String & url, WiFiClient * client, WebServer::ContentTypeFunction contentType) {
|
||||
(void)method; // GET, PUT, ...
|
||||
(void)url; // example: /root/myfile.html
|
||||
(void)client; // the webserver tcp client connection
|
||||
(void)contentType; // contentType(".html") => "text/html"
|
||||
Serial.println("A useless web hook has passed:");
|
||||
Serial.println(method);
|
||||
Serial.println(url);
|
||||
//Serial.println(contentType);
|
||||
return WebServer::CLIENT_REQUEST_CAN_CONTINUE;
|
||||
});
|
||||
|
||||
server.addHook([](const String&, const String & url, WiFiClient*, WebServer::ContentTypeFunction) {
|
||||
if (url.startsWith("/fail")) {
|
||||
Serial.printf("An always failing web hook has been triggered\n");
|
||||
return WebServer::CLIENT_MUST_STOP;
|
||||
}
|
||||
return WebServer::CLIENT_REQUEST_CAN_CONTINUE;
|
||||
});
|
||||
|
||||
server.addHook([](const String&, const String & url, WiFiClient * client, WebServer::ContentTypeFunction) {
|
||||
if (url.startsWith("/dump")) {
|
||||
Serial.printf("The dumper web hook is on the run\n");
|
||||
|
||||
// Here the request is not interpreted, so we cannot for sure
|
||||
// swallow the exact amount matching the full request+content,
|
||||
// hence the tcp connection cannot be handled anymore by the
|
||||
auto last = millis();
|
||||
while ((millis() - last) < 500) {
|
||||
char buf[32];
|
||||
size_t len = client->read((uint8_t*)buf, sizeof(buf));
|
||||
if (len > 0) {
|
||||
Serial.printf("(<%d> chars)", (int)len);
|
||||
Serial.write(buf, len);
|
||||
last = millis();
|
||||
}
|
||||
}
|
||||
// Two choices: return MUST STOP and webserver will close it
|
||||
// (we already have the example with '/fail' hook)
|
||||
// or IS GIVEN and webserver will forget it
|
||||
// trying with IS GIVEN and storing it on a dumb WiFiClient.
|
||||
// check the client connection: it should not immediately be closed
|
||||
// (make another '/dump' one to close the first)
|
||||
Serial.printf("\nTelling server to forget this connection\n");
|
||||
static WiFiClient forgetme = *client; // stop previous one if present and transfer client refcounter
|
||||
return WebServer::CLIENT_IS_GIVEN;
|
||||
}
|
||||
return WebServer::CLIENT_REQUEST_CAN_CONTINUE;
|
||||
});
|
||||
|
||||
// Hook examples
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
}
|
||||
|
||||
void wifiLoop(void) {
|
||||
server.handleClient();
|
||||
MDNS.update();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user