Skip to content

Commit

Permalink
digital and analog input and output support, digital IO requires init…
Browse files Browse the repository at this point in the history
…ialization with pinMode, supported via http req, analog needs testing
  • Loading branch information
Lemon2311 committed Dec 26, 2023
1 parent b9f1c25 commit febc027
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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.");
Expand All @@ -126,15 +124,75 @@ 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<int>(value));
request->send(200, "text/plain", "DAC Pin nr." + String(pin) + " set to " + String(value)); });
}

void setHttpEndpoints()
{
setPinModeHttpEndpoint();
setDigitalOutputHttpEndpoint();
getDigitalInputHttpEndpoint();
getAnalogInputHttpEndpoint();
setAnalogOutputHttpEndpoint();
}

void setup()
Expand Down

0 comments on commit febc027

Please sign in to comment.