BojanJurca / Multitasking-Esp32-ping-class

Multitasking Esp32 ping class
Creative Commons Zero v1.0 Universal
1 stars 0 forks source link

Arduino Ethernet compatibility #1

Open gabrielbravov opened 3 months ago

gabrielbravov commented 3 months ago

Hello friend,

first of all, thanks for the work. I was searching a great icmp library with arduino compatibility a lot of time and your library work flawlessly with Arduino Framework.

¿how can i use it with Ethernet instead of wifi? using Arduino Ethernet Library and W5500 ic.

I tried this code in platformio, but it crashed and goes into reboot loop.

`//Ejemplo de https://github.com/BojanJurca/Multitasking-Esp32-ping-class

include

include "ESP32_ping.hpp"

include // Ethernet library

IPAddress local_IP(192, 168, 10, 126); // Set your Static IP address IPAddress gateway(192, 168, 10, 1); // Set your Gateway IP address IPAddress subnet(255, 255, 255, 0); //subnetmask

// Enter a MAC address and IP address for your controller below. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE }; EthernetClient clienteth; //Generamos objeto de la clase Ethernet

void setup () { Serial.begin (9600); while (!Serial) delay (10); delay (1000);

// WiFi.begin ("yourwifi","yourpw"); // use your WiFi credentials

// Serial.print ("\nConnecting "); // while (WiFi.status () != WL_CONNECTED) { // delay (500); // Serial.print ("."); // } // Serial.print ("\nConnected, STAtion IP: "); // Serial.print (WiFi.localIP ()); // Serial.print(", route's IP: "); // Serial.println (WiFi.gatewayIP ());

Ethernet.init(5); // SS pin for spiV Ethernet.begin(mac, local_IP); // start the Ethernet connection delay(1000); // give the Ethernet shield a second to initialize Serial.print("Direccion IP: "); Serial.println(Ethernet.localIP());

Serial.println("#######################");
Serial.println("Ejemplo 1: Pingueo simple al gateway");

// example 1: simple router pinging Serial.println (); esp32_ping simplePing;

simplePing.ping ("github.com");

// simplePing.ping (WiFi.gatewayIP ()); if (simplePing.error() != ERR_OK) { Serial.printf ("Error %i\n", simplePing.error ()); } else { Serial.printf ("Ping statistics for %s:\n" " Packets: Sent = %i, Received = %i, Lost = %i", simplePing.target ().toString ().c_str(), simplePing.sent (), simplePing.received (), simplePing.lost ()); if (simplePing.sent ()) { Serial.printf (" (%.2f%% loss)\nRound trip:\n" " Min = %.3fms, Max = %.3fms, Avg = %.3fms, Stdev = %.3fms\n", (float) simplePing.lost () / (float) simplePing.sent () * 100, simplePing.min_time (), simplePing.max_time (), simplePing.mean_time (), sqrt (simplePing.var_time () / simplePing.received ())); } else { Serial.printf ("\n"); } }

Serial.println("#######################");
Serial.println("Ejemplo 2: ping and capture intermentidate results by overloading onReceive function");
// example 2: ping and capture intermentidate results by overloading onReceive function
Serial.println ();
class capture_ping : public esp32_ping {
    void onReceive (int bytes) {
        if (elapsed_time ())
            Serial.printf ("Reply from %s: bytes = %i time = %.3fms\n", target ().toString().c_str(), size (), elapsed_time ());
        else
            Serial.printf ("Reply from %s: time-out\n", target ().toString ().c_str ());
    }
};
capture_ping capturePing;

capturePing.ping ("github.com");
if (capturePing.error () != ERR_OK) {
    Serial.printf ("%i\n", capturePing.error ());
} else {
    Serial.printf ("Ping statistics for %s:\n"
                  "    Packets: Sent = %i, Received = %i, Lost = %i", capturePing.target ().toString ().c_str (), capturePing.sent (), capturePing.received (), capturePing.lost ());
    if (capturePing.sent()) {
        Serial.printf (" (%.2f%% loss)\nRound trip:\n"
                      "   Min = %.3fms, Max = %.3fms, Avg = %.3fms, Stdev = %.3fms\n", (float) capturePing.lost () / (float) capturePing.sent () * 100, capturePing.min_time (), capturePing.max_time (), capturePing.mean_time (), sqrt (capturePing.var_time () / capturePing.received ()));
    } else {
        Serial.printf ("\n");
    }
}

Serial.println("#######################");
Serial.println("Ejemplo 3: simultaneous pinging from different tasks");
// example 3: simultaneous pinging from different tasks
Serial.println ();
capture_ping githubPing;

            xTaskCreate ([] (void *param)  {
                delay (3500);
                capture_ping routerPing;
                // routerPing.ping (WiFi.gatewayIP (), 10);
                    routerPing.ping ("192.168.10.1", 10);
                if (routerPing.error () != ERR_OK) {
                    Serial.printf ("%i\n", routerPing.error ());
                } else {
                    Serial.printf ("Ping statistics for %s:\n"
                                  "    Packets: Sent = %i, Received = %i, Lost = %i", routerPing.target ().toString ().c_str (), routerPing.sent (), routerPing.received (), routerPing.lost ());
                    if (routerPing.sent()) {
                        Serial.printf (" (%.2f%% loss)\nRound trip:\n"
                                      "   Min = %.3fms, Max = %.3fms, Avg = %.3fms, Stdev = %.3fms\n", (float) routerPing.lost () / (float) routerPing.sent () * 100, routerPing.min_time (), routerPing.max_time (), routerPing.mean_time (), sqrt (routerPing.var_time () / routerPing.received ()));
                    } else {
                        Serial.printf ("\n");
                    }
                }
                vTaskDelete (NULL);
            }, 
            "ping_task", 4068, NULL, 1, NULL);

githubPing.ping ("github.com", 10);
if (githubPing.error () != ERR_OK) {
    Serial.printf ("%i\n", githubPing.error ());
} else {
    Serial.printf ("Ping statistics for %s:\n"
                  "    Packets: Sent = %i, Received = %i, Lost = %i", githubPing.target ().toString ().c_str (), githubPing.sent (), githubPing.received (), githubPing.lost ());
    if (githubPing.sent()) {
        Serial.printf (" (%.2f%% loss)\nRound trip:\n"
                      "   Min = %.3fms, Max = %.3fms, Avg = %.3fms, Stdev = %.3fms\n", (float) githubPing.lost () / (float) githubPing.sent () * 100, githubPing.min_time (), githubPing.max_time (), githubPing.mean_time (), sqrt (githubPing.var_time () / githubPing.received ()));
    } else {
        Serial.printf ("\n");
    }
}

}

void loop () {

} `

Serial output:

17:18:35.182 > ����q-����A�Direccion IP: 192.168.10.126 17:18:38.016 > ####################### 17:18:38.042 > Ejemplo 1: Pingueo simple al gateway 17:18:38.082 > 17:18:38.084 > 17:18:38.086 > assert failed: tcpip_send_msg_wait_sem IDF/components/lwip/lwip/src/api/tcpip.c:455 (Invalid mbox) 17:18:38.190 > 17:18:38.192 > 17:18:38.194 > Backtrace: 0x40083571:0x3ffb1f30 0x4008827d:0x3ffb1f50 0x4008d1d5:0x3ffb1f70 0x400e0a4e:0x3ffb20a0 0x400ec015:0x3ffb20d0 0x400dfca1:0x3ffb2110 0x400d24f3:0x3ffb2150 0x400d27b1:0x3ffb2170 0x400d64ea:0x3ffb2290 17:18:38.413 > 17:18:38.415 > 17:18:38.417 > 17:18:38.419 > 17:18:38.421 > ELF file SHA256: 1bfdffd2f84a0cc6 17:18:38.458 > 17:18:38.460 > Rebooting... 17:18:38.475 > ,� �)b��C5� * Terminal will be reused by tasks, press any key to close it.

This it's the platformio.ini content: `; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html

[env:esp32dev] platform = espressif32 board = esp32dev framework = arduino

monitor_speed = 9600 monitor_filters = time

lib_deps =

RECOMMENDED

Accept new functionality in a backwards compatible manner and patches

arduino-libraries/Ethernet @ ^2.0.2`

BojanJurca commented 3 months ago

Thank you for your comments and observations. I never tested the class neither with ethernet nor with Platform.io.

I'll take a look at it in the following days. I might still have an ESP32 board with an ethernet socket somewhere ...