Skip to content

Commit

Permalink
updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Jan 30, 2025
1 parent 6142d3d commit 1814a04
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 59 deletions.
73 changes: 14 additions & 59 deletions examples/Test_tar_packer/Test_tar_packer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,9 @@

// Uncomment `USE_WEBSERVER` to download the generated files when using SPIFFS/LittleFS
// Comment out `USE_WEBSERVER` if you get a "sketch too big" compilation error
#define USE_WEBSERVER
// Comment out `USE_ETHERNET` to use WiFi instead of Ethernet
// #define USE_ETHERNET

#if defined USE_WEBSERVER
#if !defined USE_ETHERNET
#include <WiFi.h>
#else
#include <ETH.h>

static bool eth_connected = false;

// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START: Serial.println("ETH Started"); ETH.setHostname("esp32-ethernet"); break;
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
case ARDUINO_EVENT_ETH_GOT_IP: Serial.println("ETH Got IP"); Serial.println(ETH); eth_connected = true; break;
case ARDUINO_EVENT_ETH_LOST_IP: Serial.println("ETH Lost IP"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_DISCONNECTED: Serial.println("ETH Disconnected"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_STOP: Serial.println("ETH Stopped"); eth_connected = false; break;
default: break;
}
}

#endif

#include <WebServer.h>
#include <ESPmDNS.h>
WebServer server(80);
#if defined ESP32 || defined ESP8266
#define USE_WEBSERVER
#include "./network.h"
#endif


Expand Down Expand Up @@ -101,7 +74,7 @@ void testTarGzPacker()
Serial.printf("Wrote %d bytes to %s\n", ret, targz_path);
}

void testTarGzPackerStream()
bool testTarGzPackerStream()
{
// targz_path: name of the .tar.gz compressed file
// dst_path: optional path prefix in tar archive
Expand All @@ -110,10 +83,12 @@ void testTarGzPackerStream()
Serial.printf("TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path); Free heap: %lu bytes\n", HEAP_AVAILABLE() );
auto gz = tarGzFS.open(targz_path, "w");
if(!gz)
return;
return false;
TarPacker::setProgressCallBack( LZPacker::defaultProgressCallback );
auto ret = TarGzPacker::compress(&tarGzFS, dirEntities, &gz, dst_path);
gz.close();
Serial.printf("Wrote %d bytes to %s\n", ret, targz_path);
return ret>0;
}


Expand All @@ -133,46 +108,26 @@ void setup()
removeTempFiles(); // cleanup previous examples
Serial.println("Gathering directory entitites");
TarPacker::collectDirEntities(&dirEntities, &tarGzFS, src_path, 3); // collect dir and files at %{src_path}
LZPacker::setProgressCallBack( LZPacker::defaultProgressCallback );
// LZPacker::setProgressCallBack( LZPacker::defaultProgressCallback );

Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() );

testTarPacker();
testTarPackerStream();
testTarGzPacker();
// testTarPacker();
// testTarPackerStream();
// testTarGzPacker();
testTarGzPackerStream();

Serial.printf("Free heap: %lu bytes\n", HEAP_AVAILABLE() );

Serial.println();

#if defined USE_WEBSERVER
#if !defined USE_ETHERNET // start WiFI
WiFi.mode(WIFI_STA);
WiFi.begin();
Serial.printf("Connect to WiFi...\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.printf(".");
}
Serial.printf("connected.\n");
Serial.printf("open <http://%s> or <http://%s>\n", WiFi.getHostname(), WiFi.localIP().toString().c_str());
#else // start ethernet
Network.onEvent(onEvent); // Will call onEvent() from another thread.
ETH.begin();
while (!eth_connected) {
delay(500);
printf(".");
}
#endif
// start webserver, serve filesystem at root
server.serveStatic("/", tarGzFS, "/", nullptr);
server.begin();
setupNetwork();
#endif
}

void loop()
{
#if defined USE_WEBSERVER
server.handleClient();
handleNetwork();
#endif
}
86 changes: 86 additions & 0 deletions examples/Test_tar_packer/network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

#if defined USE_WEBSERVER
// Comment out `USE_ETHERNET` to use WiFi instead of Ethernet
// #define USE_ETHERNET

#if !defined USE_ETHERNET
#if defined ESP32
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
WebServer server(80);
#elif defined ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
//Call to ESpressif SDK
extern "C" {
#include <user_interface.h>
}
uint8_t mac[6] {0x24, 0x0A, 0xC4, 0x11, 0xCF, 0x30};
#else
#error "Please include your wifi/webserver library from here"
#endif
#else
#include <ETH.h>

static bool eth_connected = false;

// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START: Serial.println("ETH Started"); ETH.setHostname("esp32-ethernet"); break;
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
case ARDUINO_EVENT_ETH_GOT_IP: Serial.println("ETH Got IP"); Serial.println(ETH); eth_connected = true; break;
case ARDUINO_EVENT_ETH_LOST_IP: Serial.println("ETH Lost IP"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_DISCONNECTED: Serial.println("ETH Disconnected"); eth_connected = false; break;
case ARDUINO_EVENT_ETH_STOP: Serial.println("ETH Stopped"); eth_connected = false; break;
default: break;
}
}

#endif


void setupNetwork()
{
#if !defined USE_ETHERNET // start WiFI
#if defined ESP8266
wifi_set_macaddr(0, const_cast<uint8*>(mac)); // prevent ESP8266 from randomizing the mac address
#endif
Serial.println("Setting WiFi mode to WIFI_STA");
WiFi.mode(WIFI_STA);
Serial.println("Starting WiFi");
WiFi.begin(/*WIFI_SSID, WIFI_PASS*/);
Serial.printf("Connect to WiFi...\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.printf(".");
}
Serial.printf("connected.\n");
Serial.printf("open <http://%s>\n", WiFi.localIP().toString().c_str());
#else // start ethernet
Network.onEvent(onEvent); // Will call onEvent() from another thread.
ETH.begin();
while (!eth_connected) {
delay(500);
printf(".");
}
#endif
// start webserver, serve filesystem at root
log_d("Serving static");
server.serveStatic("/", tarGzFS, "/", nullptr);
log_d("Server begin");
server.begin();
}


void handleNetwork()
{
server.handleClient();
}



#endif
Binary file removed examples/Test_tar_packer/test.tar.gz
Binary file not shown.

0 comments on commit 1814a04

Please sign in to comment.