PsychichHTTP v2-dev
This commit is contained in:
@@ -7,26 +7,30 @@
|
||||
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 <ESPmDNS.h>
|
||||
#include <LittleFS.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#ifndef WIFI_SSID
|
||||
#error "You need to enter your wifi credentials. Copy 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;
|
||||
|
||||
// hostname for mdns (psychic.local)
|
||||
const char* local_hostname = "psychic";
|
||||
|
||||
PsychicHttpServer server;
|
||||
PsychicWebSocketHandler websocketHandler;
|
||||
PsychicEventSource eventSource;
|
||||
|
||||
const char *htmlContent = R"(
|
||||
const char* htmlContent = R"(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -92,8 +96,8 @@ bool connectToWifi()
|
||||
Serial.print("[WiFi] Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.setSleep(false);
|
||||
WiFi.useStaticBuffers(true);
|
||||
// WiFi.setSleep(false);
|
||||
// WiFi.useStaticBuffers(true);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
@@ -102,10 +106,8 @@ bool connectToWifi()
|
||||
int numberOfTries = 20;
|
||||
|
||||
// Wait for the WiFi event
|
||||
while (true)
|
||||
{
|
||||
switch (WiFi.status())
|
||||
{
|
||||
while (true) {
|
||||
switch (WiFi.status()) {
|
||||
case WL_NO_SSID_AVAIL:
|
||||
Serial.println("[WiFi] SSID not found");
|
||||
break;
|
||||
@@ -135,15 +137,12 @@ bool connectToWifi()
|
||||
}
|
||||
delay(tryDelay);
|
||||
|
||||
if (numberOfTries <= 0)
|
||||
{
|
||||
if (numberOfTries <= 0) {
|
||||
Serial.print("[WiFi] Failed to connect to WiFi!");
|
||||
// Use disconnect function to force stop trying to connect
|
||||
WiFi.disconnect();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
numberOfTries--;
|
||||
}
|
||||
}
|
||||
@@ -157,47 +156,42 @@ void setup()
|
||||
delay(10);
|
||||
Serial.println("PsychicHTTP Benchmark");
|
||||
|
||||
if (connectToWifi())
|
||||
{
|
||||
if(!LittleFS.begin())
|
||||
{
|
||||
if (connectToWifi()) {
|
||||
// set up our esp32 to listen on the local_hostname.local domain
|
||||
if (!MDNS.begin(local_hostname)) {
|
||||
Serial.println("Error starting mDNS");
|
||||
return;
|
||||
}
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
|
||||
if (!LittleFS.begin()) {
|
||||
Serial.println("LittleFS Mount Failed. Do Platform -> Build Filesystem Image and Platform -> Upload Filesystem Image from VSCode");
|
||||
return;
|
||||
}
|
||||
|
||||
//start our server
|
||||
server.listen(80);
|
||||
// our index
|
||||
server.on("/", HTTP_GET, [](PsychicRequest* request, PsychicResponse* response) { return response->send(200, "text/html", htmlContent); });
|
||||
|
||||
//our index
|
||||
server.on("/", HTTP_GET, [](PsychicRequest *request)
|
||||
{
|
||||
return request->reply(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.onOpen([](PsychicWebSocketClient *client) {
|
||||
client->sendMessage("Hello!");
|
||||
});
|
||||
websocketHandler.onFrame([](PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
|
||||
request->reply(frame);
|
||||
return ESP_OK;
|
||||
// a websocket echo server
|
||||
websocketHandler.onOpen([](PsychicWebSocketClient* client) {
|
||||
// client->sendMessage("Hello!");
|
||||
});
|
||||
websocketHandler.onFrame([](PsychicWebSocketRequest* request, httpd_ws_frame* frame) {
|
||||
response->send(frame);
|
||||
return ESP_OK; });
|
||||
server.on("/ws", &websocketHandler);
|
||||
|
||||
//EventSource server
|
||||
eventSource.onOpen([](PsychicEventSourceClient *client) {
|
||||
client->send("Hello", NULL, millis(), 1000);
|
||||
});
|
||||
// EventSource server
|
||||
eventSource.onOpen([](PsychicEventSourceClient* client) { client->send("Hello", NULL, millis(), 1000); });
|
||||
server.on("/events", &eventSource);
|
||||
|
||||
//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, PsychicResponse* response) {
|
||||
//create a response object
|
||||
StaticJsonDocument<128> output;
|
||||
JsonDocument output;
|
||||
output["msg"] = "status";
|
||||
output["status"] = "success";
|
||||
output["millis"] = millis();
|
||||
@@ -212,16 +206,16 @@ 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()); });
|
||||
|
||||
server.begin();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long last;
|
||||
void loop()
|
||||
{
|
||||
if (millis() - last > 1000)
|
||||
{
|
||||
if (millis() - last > 1000) {
|
||||
Serial.printf("Free Heap: %d\n", esp_get_free_heap_size());
|
||||
last = millis();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user