Hallo mal wieder,
bin überm Bau einer Lüfterreglung. Eigentlich wollte ich einen Wemos D1 dazu nutzen. Leider fangen die Lüfter mit PWM an zu pfeifen. Beim EP32 DEV ist das nicht der Fall. Nun versuche ich verzweifelt das Programm umzustellen auf ESP32. Über ein Input Feld möchte ich die tempMax ändern können und da klemmt es. Der neue Wert wird nicht übernommen bzw. es wird eine 0 reingeschrieben. Ich denke an es liegt hier dran:
Zeile 186: String text1 = ("TEXT1");
tempMax = text1.toInt();
Das Programm ist teilweise aus Beispielen übernommen.
Wär nett wenn jemand einen Tipp hätte. Hier mal der Sketch.
Bitte nicht zu sehr über meine Fehler schimpfen
[code]
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
////////////////////////////////////////////////////////
// WiFi connect
////////////////////////////////////////////////////////
//Enter your SSID and PASSWORD
const char* ssid = "xxxx";
const char* password = "xxxx";
// Output Status
String luefterState = "auto";
// definition of PWM pins:
const int luefter = 16; // 16 corresponds to GPIO16, sets fan1(luefter) to GPIO16
////////////////////////////////////////////////////////
// Temperatur Sensor
////////////////////////////////////////////////////////
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
////////////////////////////////////////////////////////
// PWM Werte setzen
////////////////////////////////////////////////////////
// setting PWM properties
const int freq1 = 200; //frequency fan1(luefter)
const int resolution = 8; //bit-resolution
const int pwmChannel1 = 0; //inner PWM-Channels
//FanSpeed Variablen
int fanSpeed = 0; //basic fanspeed(set to 0)
int tempMin = 22; //definition of minimum temperature
int tempMax = 28; //definition of maximum temperature
void setup() {
Serial.begin(115200); //baud rate
Serial.println("Ready");
// configure PWM functionalitites
ledcSetup(pwmChannel1, freq1, resolution);
// channelvariable for GPIO-control
ledcAttachPin(luefter, pwmChannel1);
// temperaturesensor start
sensors.begin();
// initializing Output variables
pinMode(luefter, OUTPUT);
// Set outputs to LOW
pinMode(luefter, LOW);
// connection to Network, for example WLAN
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Output of the connection options
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// start web server
server.begin();
}
void loop(void) {
// read temperature sensor
sensors.requestTemperatures(); //Request to temperature sensor
float temperatureC = sensors.getTempCByIndex(0); //sets temperatureC to input value
String StemperatureC; //StemperatureC for Webserver, (actually Value)
StemperatureC = String(temperatureC); //cast temperature variable too String Casten for output an the Webserver
//Umwandeln tempMax in string
int tempm = tempMax;
String Stempm;
Stempm = String(tempm);
Serial.print(temperatureC);
Serial.println("ºC");
delay(1000);
//------------------------------------------------------------------------
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns Luefter status auto -> on -> off -> auto...
if (header.indexOf("GET /1/on") >= 0) { //fan1, Url-extension
Serial.println("L\x81fter 1 on"); //print "fan1 on" to output
luefterState = "on"; //sets fan1state "on"
}
else if (header.indexOf("GET /1/off") >= 0) { //fan1
Serial.println("L\x81fter 1 off");
luefterState = "off";
} else if (header.indexOf("GET /1/auto") >= 0) { //fan1
Serial.println("L\x81fter 1 auto");
luefterState = "auto";
}
// Display the HTML web page
//String SendHTML(int tempMax);
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" http-equiv=\"refresh\" content=\"2\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; background-color:#c9f128; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button3 {background-color: #D8D800;border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>");
// Web Page Heading
client.println("<body><h1>Test ESP32</h1>");
// current temperature
client.println("<h1>Temperatur: " + StemperatureC + "°C</h1>");
// Display current state, and ON/OFF/AUTO buttons for fan1(luefter)
client.println("<p>Lüfter1 - Status " + luefterState + "</p>");
// If the luefterState is off, it displays the auto button
if (luefterState == "off") {
client.println("<p><a href=\"/1/auto\"><button class=\"button button3\">AUTO</button></a></p>");
luefterState = "off";
}
// If the luefterState is auto, it displays the on button
else if (luefterState == "auto") {
client.println("<p><a href=\"/1/on\"><button class=\"button \">ON</button></a></p>");
luefterState = "auto";
}
// If the luefterState is on, it displays the off button
else {
client.println("<p><a href=\"/1/off\"><button class=\"button button2\">OFF</button></a></p>");
luefterState = "on";
}
client.print("<h1>TemperaturMax: " + Stempm + "°C</h1>");
//client.print("<form action='/buton' method='POST'>");
client.print("<form action=\"/get\">"); //added a form to take a text input and send a get request.
client.print("<p>TempMax ändern</p>");
client.print( "<input type='text1' name='TEXT1' value='");
client.print(tempMax);
client.print("'size='6'>");
client.print("<input type='submit' name='SUBMIT' value='senden'>");
String text1 = ("TEXT1");
tempMax = text1.toInt();
Serial.println(tempMax);
client.print("</form><br>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
//-------------------------------------------------
// fan1(Luefter 1) state requesting
if (luefterState == "auto") // auto = automatical PWM regulation
{
if (temperatureC < tempMin)
{
//Temperature below tempMin fan1 off
fanSpeed = 0;
Serial.println("L1 = auto, Temp < " + String(tempMin) + ", fanSpeed= " + String(fanSpeed)); //print in serial interface the temperature,the current terms and the fan speed
ledcWrite(pwmChannel1, fanSpeed);
}
else if (temperatureC >= tempMin && temperatureC < tempMax)
{
//Temperature from tempMin fan1 20% -80%
//0...255 = 256 values because of 8-bit
// 32 means 20% of 256 values and 80% means 216
//fanSpeed = map(tempC, tempMin, tempMax, 32, 255);
fanSpeed = map(temperatureC, tempMin, tempMax, 32,216);
Serial.println("L1 = auto, Temp > " + String(tempMin) + " + Temp < " + String(tempMax) + ", fanSpeed= " + String(fanSpeed)); //print in serial interface the temperature,the current terms and the fan speed
ledcWrite(pwmChannel1, fanSpeed);
}
else if (temperatureC >= tempMax)
{
//Temperature from temMax fan1 100%
fanSpeed = 255; //fanSpeed 100% means 255
Serial.println("L1 = auto, Temp > " + String(tempMax) + ", fanSpeed= " + String(fanSpeed)); //print in serial interface the temperature,the current terms and the fan speed
ledcWrite(pwmChannel1, fanSpeed);
}
}
else if (luefterState == "off") // no terms -> fan 1 off
{
fanSpeed = 0;
Serial.println("L1 = off , fanSpeed= " + String(fanSpeed));
ledcWrite(pwmChannel1, fanSpeed);
}
else if (luefterState == "on") // no terms -> fan 1 on
{
fanSpeed = 255;
Serial.println("L1 = on , fanSpeed= " + String(fanSpeed));
ledcWrite(pwmChannel1, fanSpeed);
}
}
[/code]