Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esp8266 WebServer crashes when I use PracticalCrypto library! why? #1

Open
whatever2010 opened this issue Mar 9, 2024 · 0 comments

Comments

@whatever2010
Copy link

whatever2010 commented Mar 9, 2024

So here is my code, Its the simple_server example from ESPAsyncWebServer.h and encryption example from PracticalCrypto.h combined.

The webserver works fine and I can see the / page, but the moment I open the /get page, where the code for encryption exmple is, ESP8266 crashes. I tried removing while (1) yield(); but the problem is persistent.

Is PracticalCrypto too much for ESP8266? So why is this happening?

#include <Arduino.h>

#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>

# elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif

#include <ESPAsyncWebServer.h>
#include <Arduino.h>
#include <PracticalCrypto.h>

PracticalCrypto crypto;
AsyncWebServer server(80);

const char *ssid = "123456789";
const char *password = "123456789";

const char *PARAM_MESSAGE = "message";

void notFound(AsyncWebServerRequest *request)
{
    request->send(404, "text/plain", "Not found");
}

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED)
    {
        Serial.printf("WiFi Failed!\n");
        return;
    }

    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());

    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        request->send(200, "text/plain", "Hello, world");
	});

   	// Send a GET request to<IP>/get?message=<message>
    server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        String message;
        if (request->hasParam(PARAM_MESSAGE))
        {
            message = request->getParam(PARAM_MESSAGE)->value();
        }
        else
        {
            message = "No message sent";
        }

        String key = crypto.generateKey();
        crypto.setKey(key);

       	// let's make sure the key was set.
       	// if the key is empty, it's likely your key doesn't have the right length
        key = crypto.getKey();
        Serial.printf("\nEncryption key: %s\n", key.c_str());

        String plaintext = "hello world!";
        Serial.printf("Plaintext: '%s'\n", plaintext.c_str());

        String ciphertext = crypto.encrypt(plaintext);

        if (ciphertext.length() == 0)
        {
            Serial.printf("Encryption failed (status %d)\n", crypto.lastStatus());
            while (1) yield();
        }

        Serial.printf("Ciphertext: '%s'\n", ciphertext.c_str());
        request->send(200, "text/plain", "Hello, GET: " + message);
	});

   	// Send a POST request to<IP>/post with a form field message set to < message>
    server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request)
    {
        String message;
        if (request->hasParam(PARAM_MESSAGE, true))
        {
            message = request->getParam(PARAM_MESSAGE, true)->value();
        }
        else
        {
            message = "No message sent";
        }

        request->send(200, "text/plain", "Hello, POST: " + message);
	});

    server.onNotFound(notFound);

    server.begin();
}

void loop() {}

Here last prints before crash

23:02:10.050 -> Encryption key: E*o24<7c%J3Ss1Qk=7*_.c/Y3wIi']Er sjaSE8KyxX)*XqROj)X#$7+\tvhv Q/
23:02:10.050 -> Plaintext: 'hello world!'
23:02:10.050 -> 
23:02:10.050 -> User exception (panic/abort/assert)
23:02:10.050 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
23:02:10.050 -> 
23:02:10.050 -> Panic core_esp8266_main.cpp:191 __yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant