Hello everyone, I am new to Arduino, and I am facing some issues regarding my Ethernet configuration. I am met with a "Failed to configure Ethernet with static IP" on the serial monitor. I would really appreciate any kind of help. I will provide my entire code/sketch below. Thank you in advance!
#include <ETH.h>
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
#include <WiFi.h>
#include <vector>
// GPIO pin definitions for relays
const int RELAY_POSITIVE = 26; // Positive relay GPIO
const int RELAY_NEGATIVE = 27; // Negative relay GPIO
AsyncWebServer server(80);
WiFiClient client; // Ethernet client for SCPI commands
// Static IP configuration
IPAddress ip(192, 168, 1, 100); // Static IP address
IPAddress gateway(192, 168, 1, 1); // Gateway IP (adjust as needed)
IPAddress subnet(255, 255, 255, 0); // Subnet mask
// Structure to hold parsed command data
struct ControlCommand {
String type; // Command type (e.g., "UI", "I", "DELAY", "CCD", "RUN", "STANDBY")
float value; // Value associated with the command
};
std::vector<ControlCommand> commands; // List of commands
// Function declarations
void setupRelays();
void setPolarity(int direction);
void sendSCPICommand(String command);
void handleSCPIError(int errorCode);
void sendCurrentCommand(float current);
void sendVoltageCommand(float voltage);
std::vector<ControlCommand> parseFile(const char* filepath);
void executeSequence(std::vector<ControlCommand> commands);
void handleFileUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final);
void logMessage(const String& message);
void setup() {
Serial.begin(115200);
// Initialize Ethernet with static IP
ETH.begin();
// Optionally, set Ethernet pins if needed
// Ethernet.init(5); // Uncomment and set the correct GPIO pin if necessary
if (!ETH.config(ip, gateway, subnet)) {
Serial.println("Failed to configure Ethernet with static IP");
return;
} else {
Serial.println("Ethernet configured successfully.");
}
// Wait for Ethernet link
while (!ETH.linkUp()) {
delay(1000);
Serial.println("Waiting for Ethernet...");
}
Serial.println("Connected to Ethernet with IP: " + ETH.localIP().toString());
setupRelays();
// Serve index.html
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/index.html", "text/html");
});
// Handle file uploads
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "File Uploaded Successfully");
}, handleFileUpload);
// Start sequence endpoint
server.on("/start", HTTP_GET, [](AsyncWebServerRequest *request) {
commands = parseFile("/control.txt");
executeSequence(commands);
request->send(200, "text/plain", "Sequence started.");
});
server.begin();
}
void loop() {
// Event-driven architecture; no operations in loop
}
// Relay setup
void setupRelays() {
pinMode(RELAY_POSITIVE, OUTPUT);
pinMode(RELAY_NEGATIVE, OUTPUT);
digitalWrite(RELAY_POSITIVE, LOW);
digitalWrite(RELAY_NEGATIVE, LOW);
}
// File upload handler
void handleFileUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
Serial.printf("UploadStart: %s\n", filename.c_str());
request->_tempFile = LittleFS.open("/control.txt", "w");
}
if (len) {
request->_tempFile.write(data, len);
}
if (final) {
Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index + len);
request->_tempFile.close();
}
}
// Parse command file
std::vector<ControlCommand> parseFile(const char* filepath) {
std::vector<ControlCommand> commands;
File file = LittleFS.open(filepath, "r");
if (!file) {
Serial.println("Failed to open file for reading");
return commands;
}
while (file.available()) {
String line = file.readStringUntil('\n');
line.trim();
if (line.startsWith("UI")) {
float value = line.substring(3).toFloat();
commands.push_back({"UI", value});
} else if (line.startsWith("I")) {
float value = line.substring(2).toFloat();
commands.push_back({"I", value});
} else if (line.startsWith("DELAY")) {
float delayTime = line.substring(6).toFloat();
commands.push_back({"DELAY", delayTime});
} else if (line.startsWith("CCD")) {
int direction = line.substring(4) == "+" ? 1 : -1;
commands.push_back({"CCD", (float)direction});
} else if (line.startsWith("RUN")) {
commands.push_back({"RUN", 0});
} else if (line.startsWith("STANDBY")) {
commands.push_back({"STANDBY", 0});
}
}
file.close();
return commands;
}
// Set polarity
void setPolarity(int direction) {
if (direction == 1) {
digitalWrite(RELAY_POSITIVE, HIGH);
digitalWrite(RELAY_NEGATIVE, LOW);
} else if (direction == -1) {
digitalWrite(RELAY_POSITIVE, LOW);
digitalWrite(RELAY_NEGATIVE, HIGH);
}
}
// Send SCPI command
void sendSCPICommand(String command) {
if (client.connect("169.254.0.0", 24)) {
client.println(command);
delay(100);
String response = client.readStringUntil('\n');
if (response.startsWith("-")) { // Error response
int errorCode = response.toInt();
handleSCPIError(errorCode);
}
client.stop();
} else {
Serial.println("Failed to connect to the power supply.");
}
}
// Handle SCPI errors
void handleSCPIError(int errorCode) {
switch (errorCode) {
case 0:
Serial.println("No error.");
break;
case -100:
Serial.println("Command error: Command unknown.");
break;
case -102:
Serial.println("Syntax error: Command syntax wrong.");
break;
case -108:
Serial.println("Parameter not allowed.");
break;
case -200:
Serial.println("Execution error: Command could not be executed.");
break;
case -201:
Serial.println("Invalid while in local mode.");
break;
case -220:
Serial.println("Parameter error: Wrong parameter used.");
break;
case -221:
Serial.println("Settings conflict.");
break;
case -222:
Serial.println("Data out of range.");
break;
case -223:
Serial.println("Too much data.");
break;
case -224:
Serial.println("Illegal parameter value.");
break;
case -225:
Serial.println("Out of memory.");
break;
case -999:
Serial.println("Safety OVP triggered. Please power cycle the device.");
break;
default:
Serial.println("Unknown error: " + String(errorCode));
}
}
// Execute sequence
void executeSequence(std::vector<ControlCommand> commands) {
for (ControlCommand cmd : commands) {
if (cmd.type == "UI") {
sendVoltageCommand(cmd.value);
} else if (cmd.type == "I") {
sendCurrentCommand(cmd.value);
} else if (cmd.type == "DELAY") {
delay((int)(cmd.value * 1000));
} else if (cmd.type == "CCD") {
setPolarity((int)cmd.value);
} else if (cmd.type == "RUN") {
sendSCPICommand("OUTP 1");
} else if (cmd.type == "STANDBY") {
sendSCPICommand("OUTP 0");
}
}
}
// Send current command
void sendCurrentCommand(float current) {
String command = "ISET " + String(current, 2);
sendSCPICommand(command);
}
// Send voltage command
void sendVoltageCommand(float voltage) {
String command = "VSET " + String(voltage, 2);
sendSCPICommand(command);
}
// Log messages for debugging
void logMessage(const String& message) {
Serial.println(message);
}