From e6eb58b85b46499b306efe216d832537a7d09460 Mon Sep 17 00:00:00 2001 From: Lemon2311 Date: Mon, 25 Dec 2023 22:50:12 +0200 Subject: [PATCH 1/5] better structure, readability and error handling for existing functionality --- src/main.cpp | 58 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4af2b77..da56b3a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,46 +21,58 @@ void initWifi() Serial.println(WiFi.localIP()); } -void setHttpEndpoints() +void helloWorld() { server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request) - { + { Serial.println("Hello"); - request->send(200, "text/plain", "Hello from ESP"); - }); + request->send(200, "text/plain", "Hello from ESP"); }); +} +void setDigitalOutputHttpEndpoint() +{ // New endpoint for controlling pins using query parameters server.on("/output", HTTP_GET, [](AsyncWebServerRequest *request) { - if (request->hasParam("pin") && request->hasParam("state")) + if (!request->hasParam("pin") || !request->hasParam("state")) { - String pinNumber = request->getParam("pin")->value(); - String state = request->getParam("state")->value(); + request->send(400, "text/plain", "Missing 'pin' or 'state' parameter."); + return; + } - int pin = pinNumber.toInt(); + String pinNumber = request->getParam("pin")->value(); + String state = request->getParam("state")->value(); - if (state == "high") - { - digitalWrite(pin, HIGH); - request->send(200, "text/plain", "Pin set to HIGH"); - } - else if (state == "low") - { - digitalWrite(pin, LOW); - request->send(200, "text/plain", "Pin set to LOW"); - } - else - { - request->send(400, "text/plain", "Invalid state. Use 'high' or 'low'."); - } + if (!pinNumber.toInt()) + { + request->send(400, "text/plain", "Invalid 'pin' parameter. Please use a valid integer."); + return; + } + + int pin = pinNumber.toInt(); + + if (state == "high") + { + digitalWrite(pin, HIGH); + request->send(200, "text/plain", "Pin set to HIGH"); + } + else if (state == "low") + { + digitalWrite(pin, LOW); + request->send(200, "text/plain", "Pin set to LOW"); } else { - request->send(400, "text/plain", "Missing pin or state parameter."); + request->send(400, "text/plain", "Invalid 'state' parameter. Use 'high' or 'low'."); } }); } +void setHttpEndpoints() +{ + setDigitalOutputHttpEndpoint(); +} + void setup() { pinMode(13, OUTPUT); From 36e62abc9e5b65ecb78c679e8240fab83bc013b3 Mon Sep 17 00:00:00 2001 From: Lemon2311 Date: Mon, 25 Dec 2023 23:19:46 +0200 Subject: [PATCH 2/5] added http endpoint for initializeingDigitalIO pins --- src/main.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index da56b3a..e9826db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ void helloWorld() void setDigitalOutputHttpEndpoint() { // New endpoint for controlling pins using query parameters - server.on("/output", HTTP_GET, [](AsyncWebServerRequest *request) + server.on("/digitalOutput", HTTP_POST, [](AsyncWebServerRequest *request) { if (!request->hasParam("pin") || !request->hasParam("state")) { @@ -68,14 +68,53 @@ void setDigitalOutputHttpEndpoint() }); } +void setPinModeHttpEndpoint() +{ + // New endpoint for initializing digital pins using query parameters + server.on("/initializeDigitalPin", HTTP_POST, [](AsyncWebServerRequest *request) + { + if (!request->hasParam("pin") || !request->hasParam("mode")) + { + request->send(400, "text/plain", "Missing 'pin' or 'mode' parameter."); + return; + } + + String pinNumber = request->getParam("pin")->value(); + String mode = request->getParam("mode")->value(); + + if (!pinNumber.toInt()) + { + request->send(400, "text/plain", "Invalid 'pin' parameter. Please use a valid integer."); + return; + } + + int pin = pinNumber.toInt(); + + if (mode == "input") + { + pinMode(pin, INPUT); + request->send(200, "text/plain", "Digital pin initialized as INPUT"); + } + else if (mode == "output") + { + pinMode(pin, OUTPUT); + request->send(200, "text/plain", "Digital pin initialized as OUTPUT"); + } + else + { + request->send(400, "text/plain", "Invalid 'mode' parameter. Use 'input' or 'output'."); + } + }); +} + void setHttpEndpoints() { + setPinModeHttpEndpoint(); setDigitalOutputHttpEndpoint(); } void setup() { - pinMode(13, OUTPUT); Serial.begin(115200); initWifi(); setHttpEndpoints(); From 4559a4f557d36d6fbe4aef0f79a2dc6445c26306 Mon Sep 17 00:00:00 2001 From: Lemon2311 Date: Mon, 25 Dec 2023 23:31:45 +0200 Subject: [PATCH 3/5] added http endpoint for initializeingDigitalIO pins --- .gitignore | 1 + src/main.cpp | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 89cc49c..0ee2098 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +WiFiCredentials.h diff --git a/src/main.cpp b/src/main.cpp index e9826db..09fa223 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,6 @@ #include #include - -const char *ssid = "DIGI-y6cQ"; -const char *password = "D2E2PyEhkT"; +#include "WiFiCredentials.h" AsyncWebServer server(80); From b9f1c252887bff4694920e050debafac05a615a2 Mon Sep 17 00:00:00 2001 From: Lemon2311 Date: Tue, 26 Dec 2023 00:03:57 +0200 Subject: [PATCH 4/5] added http endpoint for getting digitalInput, needs in hand hardware testing --- src/main.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 09fa223..f651c05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,12 +52,12 @@ void setDigitalOutputHttpEndpoint() if (state == "high") { digitalWrite(pin, HIGH); - request->send(200, "text/plain", "Pin set to HIGH"); + request->send(200, "text/plain", "Pin nr."+String(pin)+" set to HIGH"); } else if (state == "low") { digitalWrite(pin, LOW); - request->send(200, "text/plain", "Pin set to LOW"); + request->send(200, "text/plain", "Pin nr."+String(pin)+" set to LOW"); } else { @@ -91,12 +91,12 @@ void setPinModeHttpEndpoint() if (mode == "input") { pinMode(pin, INPUT); - request->send(200, "text/plain", "Digital pin initialized as INPUT"); + request->send(200, "text/plain", "Digital pin nr."+String(pin)+" initialized as INPUT"); } else if (mode == "output") { pinMode(pin, OUTPUT); - request->send(200, "text/plain", "Digital pin initialized as OUTPUT"); + request->send(200, "text/plain", "Digital pin nr."+String(pin)+" initialized as OUTPUT"); } else { @@ -105,10 +105,36 @@ void setPinModeHttpEndpoint() }); } +void getDigitalInputHttpEndpoint()//needs testing with hardware +{ + server.on("/digitalInput", HTTP_GET, [](AsyncWebServerRequest *request) + { + if (!request->hasParam("pin")) + { + request->send(400, "text/plain", "Missing 'pin' parameter."); + return; + } + + String pinNumber = request->getParam("pin")->value(); + + if (!pinNumber.toInt()) + { + request->send(400, "text/plain", "Invalid 'pin' parameter. Please use a valid integer."); + return; + } + + int pin = pinNumber.toInt(); + int pinState = digitalRead(pin); + + request->send(200, "text/plain", pinState == HIGH ? "HIGH" : "LOW"); + }); +} + void setHttpEndpoints() { setPinModeHttpEndpoint(); setDigitalOutputHttpEndpoint(); + getDigitalInputHttpEndpoint(); } void setup() From febc027a4f0337ce3523af360c7680ef79cc0ba7 Mon Sep 17 00:00:00 2001 From: Lemon2311 Date: Tue, 26 Dec 2023 02:18:15 +0200 Subject: [PATCH 5/5] digital and analog input and output support, digital IO requires initialization with pinMode, supported via http req, analog needs testing --- src/main.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f651c05..dead5fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,7 @@ void setDigitalOutputHttpEndpoint() { // New endpoint for controlling pins using query parameters server.on("/digitalOutput", HTTP_POST, [](AsyncWebServerRequest *request) - { + { if (!request->hasParam("pin") || !request->hasParam("state")) { request->send(400, "text/plain", "Missing 'pin' or 'state' parameter."); @@ -62,15 +62,14 @@ void setDigitalOutputHttpEndpoint() else { request->send(400, "text/plain", "Invalid 'state' parameter. Use 'high' or 'low'."); - } - }); + } }); } void setPinModeHttpEndpoint() { // New endpoint for initializing digital pins using query parameters server.on("/initializeDigitalPin", HTTP_POST, [](AsyncWebServerRequest *request) - { + { if (!request->hasParam("pin") || !request->hasParam("mode")) { request->send(400, "text/plain", "Missing 'pin' or 'mode' parameter."); @@ -101,14 +100,13 @@ void setPinModeHttpEndpoint() else { request->send(400, "text/plain", "Invalid 'mode' parameter. Use 'input' or 'output'."); - } - }); + } }); } -void getDigitalInputHttpEndpoint()//needs testing with hardware +void getDigitalInputHttpEndpoint() // needs testing with hardware { - server.on("/digitalInput", HTTP_GET, [](AsyncWebServerRequest *request) - { + server.on("/digitalInput", HTTP_GET, [](AsyncWebServerRequest *request) + { if (!request->hasParam("pin")) { request->send(400, "text/plain", "Missing 'pin' parameter."); @@ -126,8 +124,66 @@ void getDigitalInputHttpEndpoint()//needs testing with hardware int pin = pinNumber.toInt(); int pinState = digitalRead(pin); - request->send(200, "text/plain", pinState == HIGH ? "HIGH" : "LOW"); - }); + request->send(200, "text/plain", pinState == HIGH ? "HIGH" : "LOW"); }); +} + +void getAnalogInputHttpEndpoint() // needs testing with hardware +{ + server.on("/analogInput", HTTP_GET, [](AsyncWebServerRequest *request) + { + if (!request->hasParam("pin")) { + request->send(400, "text/plain", "Missing 'pin' parameter."); + return; + } + + String pinNumber = request->getParam("pin")->value(); + String type = request->hasParam("type") ? request->getParam("type")->value() : "value"; + String precisionParam = request->hasParam("precision") ? request->getParam("precision")->value() : "2"; + int pin = pinNumber.toInt(); + int precision = precisionParam.toInt(); + + //set the resolution of analogRead return values + analogReadResolution(precision); + // Read the analog value + int value = analogRead(pin); + + if (type == "voltage") { + float maxAdcValue = pow(2, precision) - 1; // Calculate max ADC value + float voltage = (value / maxAdcValue) * 3.3; // Convert to voltage + String voltageStr = String(voltage, precision); + request->send(200, "text/plain", voltageStr + "V"); + } else { + request->send(200, "text/plain", String(value)); + } }); +} + +void setAnalogOutputHttpEndpoint() // needs testing with hardware +{ + server.on("/analogOutput", HTTP_POST, [](AsyncWebServerRequest *request) + { + if (!request->hasParam("pin") || !request->hasParam("value")) { + request->send(400, "text/plain", "Missing 'pin' or 'value' parameter."); + return; + } + + String pinNumber = request->getParam("pin")->value(); + String valueString = request->getParam("value")->value(); + String type = request->hasParam("type") ? request->getParam("type")->value() : "value"; + + int pin = pinNumber.toInt(); + float value = valueString.toFloat(); + + if (type == "voltage") { + value = (value / 3.3) * 4095.0; // Convert from voltage to DAC value (12-bit resolution) + } + + if (value < 0 || value > 255) { + request->send(400, "text/plain", "Invalid 'value' parameter. Use a value between 0 and 255."); + return; + } + + dacWrite(pin, static_cast(value)); + request->send(200, "text/plain", "DAC Pin nr." + String(pin) + " set to " + String(value)); }); } void setHttpEndpoints() @@ -135,6 +191,8 @@ void setHttpEndpoints() setPinModeHttpEndpoint(); setDigitalOutputHttpEndpoint(); getDigitalInputHttpEndpoint(); + getAnalogInputHttpEndpoint(); + setAnalogOutputHttpEndpoint(); } void setup()