PsychichHTTP v2-dev
This commit is contained in:
@@ -7,22 +7,21 @@
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <LittleFS.h>
|
||||
#include <ArduinoJSON.h>
|
||||
#include "_secret.h"
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJSON.h>
|
||||
#include <LittleFS.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <PsychicHttpsServer.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#ifndef WIFI_SSID
|
||||
#error "You need to enter your wifi credentials. Rename secret.h to _secret.h and enter your credentials there."
|
||||
#endif
|
||||
|
||||
//Enter your WIFI credentials in secret.h
|
||||
const char *ssid = WIFI_SSID;
|
||||
const char *password = WIFI_PASS;
|
||||
// Enter your WIFI credentials in secret.h
|
||||
const char* ssid = WIFI_SSID;
|
||||
const char* password = WIFI_PASS;
|
||||
|
||||
PsychicHttpsServer server;
|
||||
PsychicWebSocketHandler websocketHandler;
|
||||
@@ -30,7 +29,7 @@ PsychicWebSocketHandler websocketHandler;
|
||||
String server_cert;
|
||||
String server_key;
|
||||
|
||||
const char *htmlContent = R"(
|
||||
const char* htmlContent = R"(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -162,52 +161,56 @@ void setup()
|
||||
|
||||
if (connectToWifi())
|
||||
{
|
||||
if(!LittleFS.begin())
|
||||
if (!LittleFS.begin())
|
||||
{
|
||||
Serial.println("LittleFS Mount Failed. Do Platform -> Build Filesystem Image and Platform -> Upload Filesystem Image from VSCode");
|
||||
return;
|
||||
}
|
||||
|
||||
File fp = LittleFS.open("/server.crt");
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
server_cert = fp.readString();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("server.pem not found, SSL not available");
|
||||
return;
|
||||
}
|
||||
fp.close();
|
||||
|
||||
File fp2 = LittleFS.open("/server.key");
|
||||
if (fp2) {
|
||||
if (fp2)
|
||||
{
|
||||
server_key = fp2.readString();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("server.key not found, SSL not available");
|
||||
return;
|
||||
}
|
||||
fp2.close();
|
||||
|
||||
//start our server
|
||||
server.listen(443, server_cert.c_str(), server_key.c_str());
|
||||
// start our server
|
||||
server.setCertificate(server_cert.c_str(), server_key.c_str());
|
||||
|
||||
//our index
|
||||
server.on("/", HTTP_GET, [](PsychicRequest *request)
|
||||
{
|
||||
return request->reply(200, "text/html", htmlContent);
|
||||
});
|
||||
// our index
|
||||
server.on("/", HTTP_GET, [](PsychicRequest* request)
|
||||
{ return response->send(200, "text/html", htmlContent); });
|
||||
|
||||
//serve static files from LittleFS/www on /
|
||||
// serve static files from LittleFS/www on /
|
||||
server.serveStatic("/", LittleFS, "/www/");
|
||||
|
||||
//a websocket echo server
|
||||
websocketHandler.onFrame([](PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
|
||||
request->reply(frame);
|
||||
return ESP_OK;
|
||||
});
|
||||
// a websocket echo server
|
||||
websocketHandler.onFrame([](PsychicWebSocketRequest* request, httpd_ws_frame* frame)
|
||||
{
|
||||
response->send(frame);
|
||||
return ESP_OK; });
|
||||
server.on("/ws", &websocketHandler);
|
||||
|
||||
//api - parameters passed in via query eg. /api/endpoint?foo=bar
|
||||
server.on("/api", HTTP_GET, [](PsychicRequest *request)
|
||||
{
|
||||
// api - parameters passed in via query eg. /api/endpoint?foo=bar
|
||||
server.on("/api", HTTP_GET, [](PsychicRequest* request)
|
||||
{
|
||||
//create a response object
|
||||
StaticJsonDocument<128> output;
|
||||
output["msg"] = "status";
|
||||
@@ -224,8 +227,7 @@ void setup()
|
||||
//serialize and return
|
||||
String jsonBuffer;
|
||||
serializeJson(output, jsonBuffer);
|
||||
return request->reply(200, "application/json", jsonBuffer.c_str());
|
||||
});
|
||||
return response->send(200, "application/json", jsonBuffer.c_str()); });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user