Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt committed Nov 14, 2024
1 parent a125eb3 commit a9a4b6c
Show file tree
Hide file tree
Showing 11 changed files with 991 additions and 95 deletions.
6 changes: 6 additions & 0 deletions examples/csvLogger/.vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"configuration": "JTAGAdapter=default,PSRAM=disabled,FlashMode=qio,FlashSize=4M,LoopCore=1,EventsCore=1,USBMode=hwcdc,CDCOnBoot=default,MSCOnBoot=default,DFUOnBoot=default,UploadMode=default,PartitionScheme=default,CPUFreq=240,UploadSpeed=921600,DebugLevel=none,EraseFlash=none",
"board": "esp32:esp32:esp32s3",
"port": "COM3",
"sketch": "csvLogger.ino"
}
404 changes: 404 additions & 0 deletions examples/csvLogger/.vscode/c_cpp_properties.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/csvLogger/csvLogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void getUpdatedtime(const uint32_t timeout) {


//////////////////////////// Append a row to csv file ///////////////////////////////////
bool appenRow() {
bool appendRow() {
getUpdatedtime(10);

char filename[32];
Expand Down Expand Up @@ -181,6 +181,6 @@ void loop() {
static uint32_t updateTime;
if (millis() - updateTime > 30000) {
updateTime = millis();
appenRow();
appendRow();
}
}
5 changes: 5 additions & 0 deletions examples/simpleServer/.vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configuration": "USBMode=hwcdc,JTAGAdapter=default,CDCOnBoot=default,PartitionScheme=default,CPUFreq=160,FlashMode=qio,FlashFreq=80,UploadSpeed=921600,DebugLevel=none,EraseFlash=none",
"board": "esp32:esp32:nologo_esp32c3_super_mini",
"sketch": "simpleServer.ino"
}
451 changes: 451 additions & 0 deletions examples/simpleServer/.vscode/c_cpp_properties.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions examples/simpleServer/simpleServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

FSWebServer myWebServer(FILESYSTEM, 80);

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#undef LED_BUILTIN
#define LED_BUILTIN 8

// In order to set SSID and password open the /setup webserver page
// const char* ssid;
Expand Down Expand Up @@ -67,7 +66,7 @@ void setup(){
startFilesystem();

// Try to connect to stored SSID, start AP if fails after timeout
myWebServer.setAP("ESP_AP", "123456789");
myWebServer.setAP("ESP32C3", "123456789");
IPAddress myIP = myWebServer.startWiFi(15000);
Serial.println("\n");

Expand Down
4 changes: 2 additions & 2 deletions src/SerialLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ extern "C"
{
#endif

#define __FILE_NAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define _LOG_FORMAT(letter, format) "\n["#letter"][%s:%u] %s():\t" format, __FILE_NAME__, __LINE__, __FUNCTION__
#define __FILE__NAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define _LOG_FORMAT(letter, format) "\n["#letter"][%s:%u] %s():\t" format, __FILE__NAME__, __LINE__, __FUNCTION__

#if LOG_LEVEL == 0
#define log_error(format, ...)
Expand Down
20 changes: 12 additions & 8 deletions src/SetupConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ class SetupConfigurator
void addDropdownList(const char *label, const char** array, size_t size) {

// If key is present in json, we don't need to create it.
if ((*m_doc).containsKey(label))
return;
#if ARDUINOJSON_VERSION_MAJOR > 6
JsonObject obj = (*m_doc)[label].to<JsonObject>();
#else
JsonObject obj = (*m_doc).createNestedObject(label);
#endif

#if ARDUINOJSON_VERSION_MAJOR > 6
JsonObject obj = (*m_doc)[label].to<JsonObject>();
#else
JsonObject obj = (*m_doc).createNestedObject(label);
#endif
if (obj.isNull())
return;

obj["selected"] = array[0]; // first element selected as default
#if ARDUINOJSON_VERSION_MAJOR > 6
Expand Down Expand Up @@ -252,8 +252,12 @@ class SetupConfigurator
key += numOptions ;

// If key is present and value is the same, we don't need to create/update it.
if (m_doc->containsKey(key.c_str()))
JsonVariant foundObject = (*m_doc)[key];
if (foundObject.isNull())
return;

// if (m_doc->containsKey(key.c_str()))
// return;

// if min, max, step != from default, treat this as object in order to set other properties
if (d_min != MIN_F || d_max != MAX_F || step != 1.0) {
Expand Down
173 changes: 100 additions & 73 deletions src/esp-fs-webserver.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
#include "esp-fs-webserver.h"
#include "detail/RequestHandlersImpl.h"

// Override default handleClient() method to increase connection speed
#if defined(ESP32)
void FSWebServer::handleClient()
{
if (_currentStatus == HC_NONE) {
_currentClient = _server.available();
if (!_currentClient) {
if (_nullDelay) {
delay(1);
}
return;
}
log_v("New client: client.localIP()=%s", _currentClient.localIP().toString().c_str());
_currentStatus = HC_WAIT_READ;
_statusChange = millis();
}

bool keepCurrentClient = false;

if (_currentClient.connected()) {
switch (_currentStatus) {
// No-op to avoid C++ compiler warning
case HC_NONE:
break;

// Wait for data from client to become available
case HC_WAIT_READ:
if (_currentClient.available()) {
if (_parseRequest(_currentClient)) {
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
_contentLength = CONTENT_LENGTH_NOT_SET;
_handleRequest();
}
}
else {
if (millis() - _statusChange <= 100) {
keepCurrentClient = true;
}
yield();
}
break;

// Wait for client to close the connection
case HC_WAIT_CLOSE:
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
keepCurrentClient = true;
yield();
}
break;
}
}

if (!keepCurrentClient) {
_currentClient = WiFiClient();
_currentStatus = HC_NONE;
_currentUpload.reset();
}
}
#endif

// // Override default handleClient() method to increase connection speed
// #if defined(ESP32)
// void FSWebServer::handleClient()
// {
// if (_currentStatus == HC_NONE) {
// _currentClient = _server.available();
// if (!_currentClient) {
// if (_nullDelay) {
// delay(1);
// }
// return;
// }
// log_v("New client: client.localIP()=%s", _currentClient.localIP().toString().c_str());
// _currentStatus = HC_WAIT_READ;
// _statusChange = millis();
// }

// bool keepCurrentClient = false;

// if (_currentClient.connected()) {
// switch (_currentStatus) {
// // No-op to avoid C++ compiler warning
// case HC_NONE:
// break;

// // Wait for data from client to become available
// case HC_WAIT_READ:
// if (_currentClient.available()) {
// if (_parseRequest(_currentClient)) {
// _currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
// _contentLength = CONTENT_LENGTH_NOT_SET;
// _handleRequest();
// }
// }
// else {
// if (millis() - _statusChange <= 100) {
// keepCurrentClient = true;
// }
// yield();
// }
// break;

// // Wait for client to close the connection
// case HC_WAIT_CLOSE:
// if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
// keepCurrentClient = true;
// yield();
// }
// break;
// }
// }

// if (!keepCurrentClient) {
// _currentClient = WiFiClient();
// _currentStatus = HC_NONE;
// _currentUpload.reset();
// }
// }
// #endif

#define MDNS_INSTANCE "esp-fs-webserver"

void FSWebServer::setHostname(const char* host) {
m_host = host;
Expand Down Expand Up @@ -131,7 +132,7 @@ void FSWebServer::run()

if (m_websocket != nullptr)
m_websocket->loop();

#if defined(ESP8266)
MDNS.update();
#endif
Expand Down Expand Up @@ -272,7 +273,6 @@ IPAddress FSWebServer::startWiFi(uint32_t timeout, bool apFlag, CallbackF fn)
if (strlen(_ssid)) {
m_apmode = false;


WiFi.begin(_ssid, _pass);
uint32_t startTime = millis();
log_info("Connecting to '%s'", _ssid);
Expand All @@ -294,17 +294,44 @@ IPAddress FSWebServer::startWiFi(uint32_t timeout, bool apFlag, CallbackF fn)
}
MDNS.addService("http", "tcp", m_port);
#else
// Initialize mDNS
ESP_ERROR_CHECK( mdns_init() );
// Set mDNS hostname (required if you want to advertise services)
ESP_ERROR_CHECK( mdns_hostname_set(m_host.c_str()) );
ESP_LOGI("", "mdns hostname set to: [%s]", m_host.c_str());
// Set default mDNS instance name
ESP_ERROR_CHECK( mdns_instance_name_set("EXAMPLE_MDNS_INSTANCE") );
// Structure with TXT records
mdns_txt_item_t serviceTxtData[1] = {{"board", "esp32"}};
// Initialize service
ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 1) );
WiFi.setHostname(m_host.c_str());
// // Initialize mDNS
// ESP_ERROR_CHECK( mdns_init() );
// // Set mDNS hostname (required if you want to advertise services)
// ESP_ERROR_CHECK( mdns_hostname_set(m_host.c_str()) );
// log_info("mdns hostname set to: [%s]", m_host.c_str());
// // Set default mDNS instance name
// ESP_ERROR_CHECK( mdns_instance_name_set("MDNS_INSTANCE") );
// // Structure with TXT records
// mdns_txt_item_t serviceTxtData[1] = {{"board", "esp32"}};
// // Initialize service
// ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 1) );

//initialize mDNS service
esp_err_t err = mdns_init();
if (err) {
log_error("MDNS Init failed: %d\n", err);
}

//set hostname
mdns_hostname_set(m_host.c_str());
log_info("mdns hostname set to: [%s]", m_host.c_str());
//set default instance
mdns_instance_name_set(MDNS_INSTANCE);
//add our services
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
mdns_service_add(NULL, "_arduino", "_tcp", 3232, NULL, 0);
mdns_service_instance_name_set("_http", "_tcp", MDNS_INSTANCE);
mdns_service_subtype_add_for_host(MDNS_INSTANCE, "_http", "_tcp", NULL, "_server");

mdns_txt_item_t serviceTxtData[3] = {
{"board","{esp32}"},
{"u","user"},
{"p","password"}
};
//set txt data for service (will free and replace current data)
mdns_service_txt_set("_http", "_tcp", serviceTxtData, 3);

#endif

if ((WiFi.status() == WL_CONNECTED) || apFlag)
Expand Down
10 changes: 5 additions & 5 deletions src/esp-fs-webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#include "esp_task_wdt.h"
#include "sys/stat.h"
#include <WiFi.h>
#include <ESPmDNS.h>
#include <mdns.h>
#include <HTTPUpdateServer.h>
#include <WebServer.h>
using WebServerClass = WebServer;
Expand Down Expand Up @@ -280,10 +280,10 @@ class FSWebServer : public WebServerClass
IPAddress m_captiveIp = IPAddress(192, 168, 4, 1);
ServerWebSocket* m_websocket;

#if defined(ESP32)
// Override default handleClient() method to increase connection speed
void handleClient() override;
#endif
// #if defined(ESP32)
// // Override default handleClient() method to increase connection speed
// void handleClient() override;
// #endif

// Default handler for all URIs not defined above, use it to read files from filesystem
bool captivePortal();
Expand Down
2 changes: 1 addition & 1 deletion src/websocket/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {

if(client->tcp) {
if(client->tcp->connected()) {
client->tcp->flush();
client->tcp->clear();
client->tcp->stop();
}
event = true;
Expand Down

0 comments on commit a9a4b6c

Please sign in to comment.