i have a uni project where i needed to connect my arduino mega to the internet so i can control its behavior from a website, i bought a wemos d1 mini for that, i almost finishd the project , i can send http requests to my laravel app and vice versa , the problem is everyhting will stay local i will not host the app or use this project anywhere i will just use it to present it to my university, so my demo will be localy but the doctor need me to make a secure connection, so i wwas thinking about encrypting the data i am sending then decrypt it on both sides. but when i tried to implement this when i am encrypting the data it is not returned as a "String" which makes sense but when i try to add it to a json and send it as a payload in a post request , the body of the received request on the laravel app is empty something is going wrong , but without that encryption everything works fine (note: i did not try yet to encrypt the data that will be sent from the laravel app and received from the wemos which i'll need also)
here is the working code on the wemos (without encryption) i need your help to implement the encryption that will let me receive the body on my other app encrypted that i will add a specific token also to ensure that the request is comming from the wemos
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
const char* ssid = "****";
const char* password = "****";
// Laravel App Url
String serverName = "http://192.168.153.211:8000"; // always add the ip address of the pc
ESP8266WebServer server(80);
// Function prototype
String sendHttpRequest(String url, String payload);
void setup() {
Serial.begin(9600);
delay(5000);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nConnected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
server.on("/open-door", HTTP_POST, handlePost);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
if (input.startsWith("{\"wemo_listen_phone_number\":")) {
String payload = input.substring(0);
String response = sendHttpRequest(serverName+"/api/send-otp", payload);
}
if (input.startsWith("{\"wemo_listen_otp\":")) {
String payload = input.substring(0);
String response = sendHttpRequest(serverName+"/api/open-door", payload);
}
}
}
// Function definition
String sendHttpRequest(String url, String payload) {
String response = "";
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi Disconnected");
return response;
}
WiFiClient client;
HTTPClient http;
if (http.begin(client, url)) {
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);
if (httpCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("[HTTP] Unable to connect");
}
return response;
}
// handle encryption
void handlePost() {
if (server.hasArg("plain")) {
String message = server.arg("plain");
Serial.println("mega_listen_open_door");
server.send(200, "text/plain", "Received POST data: " + message + "\n");
} else {
server.send(400, "text/plain", "Bad Request: No POST data received");
}
}
i know it is not clean code but i am new to this stuff
THANKS