zmiana ustawień przez stronę

robi7805
Posts: 20
Joined: Sat Dec 02, 2023 12:00 pm

Post

witam kod wgrywa się normalnie wchodząc na esp przez adres ip wyświetla możliwość zmiany ustawień lecz nie zapisuje ich po zatwierdzeniu czy ktoś pomoże zmienić w kodzie żeby taka funkcja działała?
oto kod:

#include <ModbusMaster.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <ESPAsyncWebSrv.h>

#define PZEM_COUNT 3
#define PZEM_RX_PIN_1 16
#define PZEM_RX_PIN_2 17
#define PZEM_RX_PIN_3 18

#define PZEM_TX_PIN_1 19
#define PZEM_TX_PIN_2 21
#define PZEM_TX_PIN_3 22

#define PWM_PIN_1 5
#define PWM_PIN_2 6
#define PWM_PIN_3 7

ModbusMaster nodes[PZEM_COUNT];
OneWire oneWire(4); // ONE_WIRE_BUS
DallasTemperature sensors(&oneWire);

float voltageMin = 248.0;
float voltageMax = 252.0;
float temperatureLimit = 85.0;

// Konfiguracja WiFi
const char* ssid = "Twój_ssid";
const char* password = "Twoje_hasło";
IPAddress staticIP(192, 168, 1, 166); // Ustaw statyczne IP
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

AsyncWebServer server(80);

void handleRoot(AsyncWebServerRequest *request) {
String html = "<html><body>";
html += "<h1>ESP32 Control Panel</h1>";
html += "<p>Current Temperature: " + String(readTemperature()) + " &deg;C</p>";
html += "<p>Settings:</p>";
html += "<form action='/set-parameters' method='post'>";
html += "Voltage Min: <input type='text' name='voltageMin' value='" + String(voltageMin) + "'><br>";
html += "Voltage Max: <input type='text' name='voltageMax' value='" + String(voltageMax) + "'><br>";
html += "Temperature Limit: <input type='text' name='temperatureLimit' value='" + String(temperatureLimit) + "'><br>";
html += "<input type='submit' value='Submit'>";
html += "</form>";
html += "</body></html>";
request->send(200, "text/html", html);
}

void handleSetParameters(AsyncWebServerRequest *request) {
if (request->hasParam("voltageMin") && request->hasParam("voltageMax") && request->hasParam("temperatureLimit")) {
voltageMin = request->getParam("voltageMin")->value().toFloat();
voltageMax = request->getParam("voltageMax")->value().toFloat();
temperatureLimit = request->getParam("temperatureLimit")->value().toFloat();
}
request->redirect("/");
}

void setup() {
Serial.begin(9600);
setupWiFi();

for (int i = 0; i < PZEM_COUNT; ++i) {
Serial2.begin(9600, SERIAL_8N1, PZEM_RX_PIN_1, PZEM_TX_PIN_1);
pinMode(PWM_PIN_1, OUTPUT);
nodes.begin(i + 1, Serial2);
}

sensors.begin();

server.on("/", HTTP_GET, handleRoot);
server.on("/set-parameters", HTTP_POST, handleSetParameters);

server.begin();
}

void loop() {
float maxVoltage = readMaxVoltage(); // Odczytaj najwyższe napięcie spośród wszystkich modułów
float temperature = readTemperature();

if (temperature > temperatureLimit) {
for (int i = 0; i < PZEM_COUNT; ++i) {
analogWrite(PWM_PIN_1, 0); // Ustaw PWM na 0% dla wszystkich modułów
}
} else {
for (int i = 0; i < PZEM_COUNT; ++i) {
float voltage = readVoltage(i);
int pwmValue = mapFloat(voltage, voltageMin, maxVoltage, 0, 255);
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(PWM_PIN_1, pwmValue);
}
}

delay(1000); // Czekaj 1 sekundę przed kolejnym odczytem
}

float readVoltage(int index) {
uint8_t result;
uint16_t data[6];

result = nodes[index].readInputRegisters(0x0000, 2);

if (result == nodes[index].ku8MBSuccess) {
uint32_t voltageRaw = (uint32_t)data[0] << 16 | data[1];
float voltage = voltageRaw / 10.0;
Serial.print("PZEM-004T ");
Serial.print(index + 1);
Serial.print(" - Voltage: ");
Serial.print(voltage);
Serial.println(" V");
return voltage;
} else {
Serial.print("Failed to read voltage from PZEM-004T ");
Serial.println(index + 1);
return 0.0;
}
}

float readMaxVoltage() {
float maxVoltage = 0.0;

for (int i = 0; i < PZEM_COUNT; ++i) {
float voltage = readVoltage(i);
maxVoltage = max(maxVoltage, voltage);
}

return maxVoltage;
}

float readTemperature() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
return temperature;
}

float mapFloat(float x, float inMin, float inMax, float outMin, float outMax) {
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}

void setupWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);

if (WiFi.config(staticIP, gateway, subnet)) {
Serial.println("Stałe IP zostały pomyślnie skonfigurowane");
} else {
Serial.println("Błąd konfiguracji stałego IP");
}

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}

Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
robi7805
Posts: 20
Joined: Sat Dec 02, 2023 12:00 pm

Post

Chcąc zmienić ustawienia muszę wgrać odnowa kod do esp
User avatar
klew
Posts: 13907
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

Jeśli chcesz jakieś parametry zapamiętać w pamięci trwałej, to na ESP trzeba je zapisać w jakiejś postaci do pamięci flash.
Jest dużo sposobów na to. Można użyć emulacji klasy Eeprom z Arduino, albo zapisać do pliku na spifs, littlefs, partycji fat itp.

Natomiast jeśli chcesz zrobić program pod Suplę, to najlepiej użyć gotowych komponentów
Najlepsze suple dla Twojego domu :mrgreen:

Return to “Pomoc”