• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Need help with ESP32 webserver code please

Q&A Forum › Need help with ESP32 webserver code please
0 Vote Up Vote Down
David Huisman asked 5 years ago
1 Answers
0 Vote Up Vote Down
David Huisman answered 5 years ago

I am trying to write a little app to set a value in my device from the slider on the web page using ESP32.
The required functions are:
Press “READ” button, ESP32 sends request to host via serial port and sets slider position and “Range” value accordingly.
Press WRITE button and ESP32 Sends slider value to host via serial port.
Changes in slider value update variable in ESP32 that can be used to send to host.
The host gets the codes “R” to get value and “S” to write value in host but the actual values are not correct, I get 0 all the time and slider value after read defaults to “9”.
I cannot see how to attach a file, so text for my code is below:
#include <WiFi.h>
#include <stdio.h>
#include <stdlib.h>
// Replace with your network credentials
const char* ssid = “BodyGuard”;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Decode HTTP GET value
String valueString;
int pos1 = 0;
int pos2 = 0;
int receivedValue =0;
int initialValue =0;
void SetRange()
{
Serial2.println(‘S’+ valueString);
//Serial2.write(valueString.toInt());
}
void GetRange()
{
Serial2.write(‘R’);
delay(100);
if(Serial2.available()>0){
receivedValue = Serial2.read();
Serial.println(receivedValue);
}
}
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid);
IPAddress IP = WiFi.softAPIP();
Serial.print(“AP IP Address: “);
Serial.println(IP);
server.begin();
Serial2.begin(115200);
}
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();
// Display the HTML web page
client.println(“<!DOCTYPE html><html>”);
client.println(“<head><meta name=\”viewport\” content=\”width=device-width, initial-scale=1\”>”);
client.println(“<link rel=\”icon\” href=\”data:,\”>”);
// CSS to style the on/off buttons
client.println(“<style>body { text-align: center; font-family: \”Trebuchet MS\”, Arial; margin-left:auto; margin-right:auto;}”);
client.println(“.slider { width: 300px; }</style>”);
client.println(“<script src=\”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\”></script>”);

// Web Page
client.println(“</head><body><h1>BodyGuard Portable Detector</h1>”);
client.println(“<p>Range : <span id=\”servoPos\”></span></p>”);
client.println(“<input type=\”range\” min=\”1\” max=\”16\” class=\”slider\” id=\”servoSlider\” onchange=\”servo(this.value)\” value=\””+valueString+”\”/>”);

client.println(“<script>var slider = document.getElementById(\”servoSlider\”);”);
client.println(“var servoP = document.getElementById(\”servoPos\”); servoP.innerHTML = slider.value;”);
client.println(“slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }”);
client.println(“$.ajaxSetup({timeout:1000}); function servo(pos) { “);
client.println(“$.get(\”/?value=\” + pos + \”&\”); {Connection: close};}</script>”);
client.println(“<p><a href=\”/read\”><button class=\”button\”>READ</button></a></p>”);
client.println(“<p><a href=\”/write\”><button class=\”button\”>WRITE</button></a></p>”);

client.println(“</body></html>”);
//GET /?value=180& HTTP/1.1 if slider change, puts value into valueString
if(header.indexOf(“GET /?value=”)>=0) {
pos1 = header.indexOf(‘=’);
pos2 = header.indexOf(‘&’);
valueString = header.substring(pos1+1, pos2);
//Serial2.print(‘S’+valueString);
//Serial.println(valueString);
}
else if(header.indexOf(“GET /read”) >= 0)
{
GetRange();
}
else if(header.indexOf(“GET /write”) >= 0)
{
SetRange();
}
// 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(“”);
}
}

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.