I am attempting to writing a simple serial loop-back test for an esp32 arduino web site. The code function is to send one of two characters from the esp32 hosted web site, out the esp32 Serial2 output port. Physically Serial2 output port is directly connected to the Serial2 input port, creating a loop-back situation. The code reads the Serial2-input port and relays the character to the esp32 web site, and the character is displayed on the web site.
Simple enough and it works except, the loop-back character displays its numerical ASCII value and not the character. I need to print the character. The essential line of code is shown below.
client.println("<p>You sent a -> " + Mychar + "</p>");
NOTE: This code is based upon a Web site turning on and off a led. The led functionality was kept to ease debugging.
Seems it should be straight forward to cast the ASCII code and display a character, but I’ve tried a lot of things all to no avail. How does one cast the ASCII value into a character in HTML display?
Thanks in advance.
// Based on: https://www.electronics-lab.com/project/esp32-webserver-tutorial/
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "MySSID";
const char* password = "MYPASSWRD";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request String header;
String header;
// Declare the Serial2 pins & vars
#define RXD2 16
#define TXD2 17
char GrnXmt = 'U'; // char sent when top button
char RedXmt = 'A'; // char sent when bottom button
String Mychar = " "; // The loop back character
// Declare the pins to which the LEDs are connected
int greenled = 26;
int redled = 27;
String greenstate = "queued";// state of green LED
String redstate = "queued";// state of red LED
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
// Set the pinmode of the pins to which the LEDs are connected and turn them low to prevent flunctuations
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
digitalWrite(greenled, LOW);
digitalWrite(redled, LOW);
//connect to access point
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
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());// this will display the Ip address which should be entered into your browser
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
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();
// turns the GPIOs sent and queued
if (header.indexOf("GET /green/sent") >= 0) {
Serial.println("green sent");
greenstate = "sent";
digitalWrite(greenled, HIGH);
Serial.print("@ green Serial2.available() == ");
Serial.println(Serial2.available());
Serial2.write(GrnXmt);
} else if (header.indexOf("GET /green/queued") >= 0) {
Serial.println("green queued");
greenstate = "queued";
digitalWrite(greenled, LOW);
Mychar = ' ';
} else if (header.indexOf("GET /red/sent") >= 0) {
Serial.println("red sent");
redstate = "sent";
digitalWrite(redled, HIGH);
Serial.print("@ red Serial2.available() == ");
Serial.println(Serial2.available());
Serial2.write(RedXmt);
} else if (header.indexOf("GET /red/queued") >= 0) {
Serial.println("red queued");
redstate = "queued";
digitalWrite(redled, LOW);
Mychar = ' ';
}
Serial.print("Serial2.available() == ");
Serial.println(Serial2.available());
// if(Serial2.available()){
size_t len = Serial2.available();
uint8_t sbuf[len];
delay(100);
Mychar = Serial2.read();
Serial.print(" Mychar == ");
Serial.println(Mychar);
// }
// 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/queued buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>My Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>Send 'U' to Serial2 port - State -> " + greenstate + "</p>");
// If the green LED is queued, it displays the ON button
if (greenstate == "queued") {
client.println("<p><a href=\"/green/sent\"><button class=\"button\">SEND</button></a></p>");
} else {
client.println("<p><a href=\"/green/queued\"><button class=\"button button2\">RESET</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>Send 'A' to Serial2 port - State -> " + redstate + "</p>");
// If the red LED is queued, it displays the ON button
if (redstate == "queued") {
client.println("<p><a href=\"/red/sent\"><button class=\"button\">SEND</button></a></p>");
} else {
client.println("<p><a href=\"/red/queued\"><button class=\"button button2\">RESET</button></a></p>");
}
client.println("</body></html>");
// Print out the responce
client.println(); // blank line - astectically pleasing
client.println("<p>You sent a -> " + Mychar + "</p>");
// 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("");
}
}