Hey team,
I have some code that reads a couple of temp sensors and displays on to a web server from the ESP32 then a couple of buttons which change the set temperature (a variable in my code) + or – 0.5 per button push. The code all works well but I would like to write the set temperature to the EEPROM but am worried about the amount of writes it will do due the set temp changing quite regularly by 20+ deg (so could be 40 writes to an EEPROM per temp change). I understand a way to do this is only writing to the EEPROM every few mins if the set temp has changed but that isnt fool proof either.
What I want to know is how do I get a text box instead of the buttons and how do i get it to change the set_temp variable in my code. Ignore the EEPROM stuff for now as thats easy enough.
Please note I am fairly new to programming of any sort and have pieced this code together through RNTs and some forum stuff. Any help would be greatly appreciated!
// Load Wi-Fi library
#include
//Load Temp Sensor Libraries
#include
#include
//Define temp probe pin
#define ONE_WIRE_BUS 15
// set up 1-wire probes
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress AIR_TEMP_SENSOR = {0x28, 0xFF, 0x16, 0x8D, 0x87, 0x16, 0x03, 0x50}; //Test sensor A
DeviceAddress VAT_TEMP_SENSOR = {0x28, 0xFF, 0x0A, 0x2E, 0x68, 0x14, 0x04, 0xA6}; //Test sensor B
//variables for temp sensors
float set_temp = 19.0;
float air_temp;
float vat_temp;
// Replace with your network credentials
const char* ssid = "AHRF455889";
const char* password = "zxcv1597";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
Serial.begin(115200);
// Set up temperature probes
sensors.setResolution(AIR_TEMP_SENSOR, 11); //resolution of 0.125deg cels,
sensors.setResolution(VAT_TEMP_SENSOR, 11); //takes approx 375ms
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
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()) { // loop while the client's connected
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();
//Get temps
sensors.requestTemperaturesByAddress(AIR_TEMP_SENSOR);
sensors.requestTemperaturesByAddress(VAT_TEMP_SENSOR);
vat_temp = sensors.getTempC(VAT_TEMP_SENSOR);
air_temp = sensors.getTempC(AIR_TEMP_SENSOR);
//Set Temp Changes By Button
if (header.indexOf("GET /set/up") >= 0) {
Serial.println("Increase Set Temperature by 0.5");
set_temp = set_temp + 0.5;
}
else if (header.indexOf("GET /set/down") >= 0) {
Serial.println("Decrease Set Temperature by 0.5");
set_temp = set_temp - 0.5;
}
// Display the HTML web page
client.println("");
client.println("");
client.println("");
//CSS Styles
client.println("html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; font-size: 15px}");
client.println(".button {background-color: lightgrey; color: black; padding: 10px 20px; text-decoration: none; font-size: 15px; margin: 2px; cursor: pointer; border-color: black; border-style: solid; border-radius: 10px;}");
client.println(".temp {background-color: #4CAF50; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".heat {background-color: orange; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".cool {background-color: lightblue; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".idle {background-color: lightgrey; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".error {background-color: red; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".relax {background-color: white; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println("");
// Web Page Heading
client.println("Laser Snake Temperature");
// Set Temp Line
client.println("Set Temperature =");
client.println("" );
client.println(set_temp);
client.println("℃");
client.println("+-
");
//Fermenter Temp Line
client.println("Fermenter Temperature =");
client.println("" );
client.println(vat_temp);
client.println("℃
");
//Fridge Temp Line
client.println("Fridge Temperature =");
client.println("" );
client.println(air_temp);
client.println("℃
");
// 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("");
}
}
Hi Matt, I hope you are doing well!
We don’t have a specific tutorial about getting data from a textbox.
I’ve found this issue: https://stackoverflow.com/questions/48475428/get-value-from-text-field-at-a-webserver-esp8266
I think it does what you are looking for. But it is for ESP8266. However, it should be compatible with the ESP32 with some modifications.
You can also modify this project (from the Learn ESP32 with Arduino IDE course) to send the values from an HTML input: https://github.com/RuiSantosdotme/ESP32-Course/blob/master/code/Servo/WiFi_Web_Server_Servo/WiFi_Web_Server_Servo.ino
I hope this helps. Regards,
Sara