In using the command: client.print(data); there is a dissimilar display result between the produced output in a UNO WiFi Rev2 sketch and an ESP32 sketch. Is this because each
one references a different library file?
For instance:
client.print(" Control/Alarm Parameters");
Produces a screen display with the text indexed to the right X spaces in the UNO sketch.
This same line of code in the ESP32 sketch does not index the text to the right and ignores the spacing. Is this because the referenced WiFi libraries are different for each microcontroller.
Is there an area on the Arduino.cc site that addresses the various syntax requirements for using client.print and client.println? Especially as far generating line feeds and newlines.
I did reference: WiFiNINA - client.print() - Arduino Reference but it only had a cursory explanation of the command. To be clear this is in regard to text manipulation on a web page and not the serial monitor.
HTML likes to eat leading spaces, but I don't know whether that is relevant.
If you provide a complete minimal sketch that demonstrates the issue, you might get better assistance.
The WiFiNINA library inherits this from the standard Print class. Unfortunately, Arduino never produced a universal Print class reference, even though the same information applies to the many libraries that use it. So you end up with varying quantities and qualities and vintages of information about these functions from one library reference to another. Probably the closest thing to a universal Print class reference is the one on the "Serial" reference page:
Other than the demo sketch, none of the information on that page is specific to communication over a serial port.
#include <WiFi.h>
#define ONBOARD_LED 2
char wifissid[] = "xxxxxxxxxxxx"; // network SSID (name)
char wifipass[] = "xxxxxxxxxxx"; // WPA network password
boolean lastConnected = false;
char serverName[] = "api.pushingbox.com";
WiFiClient client;
WiFiServer server(80);
int status = WL_IDLE_STATUS;
void setup() {
pinMode(ONBOARD_LED,OUTPUT);
Serial.println("Begin loop top..");
// initialize serial:
Serial.begin(115200);
WiFi.begin(wifissid, wifipass);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
server.begin();
// you're connected now, so print out the status:
Serial.print("WiFi Status: ");
Serial.println(WiFi.status()); // added 2/4 testing
}
void loop() {
digitalWrite(ONBOARD_LED,HIGH); // Blink for run indicator
delay(100);
digitalWrite(ONBOARD_LED,LOW);
delay(500);
// ******************** WiFiWebServer Code***********
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available())
{
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
{
client.print(" Test input line 1");
client.println();
client.print(" Test input line 2");
client.println();
client.print(" Test input line 3");
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(2000);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
What you are seeing is the expected output from this HTML:
<!DOCTYPE HTML>
<html>
Test input line 1
Test input line 2
Test input line 3
<br />
</html>
I am not able to reproduce your results. I just tried the sketch on my Uno WiFi Rev2, and I get the expected output:
Test input line 1 Test input line 2 Test input line 3
So this makes me think you are running some different code on your Uno WiFi Rev2.
Here is my sketch:
I changed the #include directive to WiFiNINA.h and fixed the bug where you were printing to Serial before calling Serial.begin (seems to break Serial output on the Uno WiFi Rev2). But it is identical as far as the HTML it produces.
#include <WiFiNINA.h>
#define ONBOARD_LED LED_BUILTIN
char wifissid[] = "xxxxxxxxxxxx"; // network SSID (name)
char wifipass[] = "xxxxxxxxxxxx"; // WPA network password
WiFiClient client;
WiFiServer server(80);
int status = WL_IDLE_STATUS;
void setup() {
pinMode(ONBOARD_LED, OUTPUT);
Serial.begin(115200);
Serial.println("Begin loop top..");
// initialize serial:
WiFi.begin(wifissid, wifipass);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
server.begin();
// you're connected now, so print out the status:
Serial.print("WiFi Status: ");
Serial.println(WiFi.status()); // added 2/4 testing
}
void loop() {
digitalWrite(ONBOARD_LED, HIGH); // Blink for run indicator
delay(100);
digitalWrite(ONBOARD_LED, LOW);
delay(500);
// ******************** WiFiWebServer Code***********
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available())
{
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print(" Test input line 1");
client.println();
client.print(" Test input line 2");
client.println();
client.print(" Test input line 3");
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(2000);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
To be clear, you are saying on the UNO the text is horizontally placed and not in a column? Because my UNO sketch is the opposite. They both use client.print() & client.println() identically. It beats me. I gotta come up with a different approach (ie control characters) or some other way to do the formatting.
From the UNO sketch:
When I connect my browser to the IP address of the Uno WiFi Rev2 running the sketch I shared above, I get this exact text in the browser:
Test input line 1 Test input line 2 Test input line 3
Please try uploading my sketch to your Uno WiFi Rev2 and then post the text you get in the browser once you connect to the IP address it shows in Serial Monitor.
No, the results are not consistent. lol..
My original WiFi Rev2 sketch works properly , ie vertically, with the same formatting commands.
But I do now have a solution for the ESP32 printing a continuous line:
client.print(" Test input line 1");
client.println("<br />");
client.println("<br />");
client.print(" Test input line 2");
client.println("<br />");
client.println("<br />");
client.print(" Test input line 3");
your main problem is that your generated output is not a valid HTML5.
if you generate valid HTML I bet the result of wifi.h and wifinina.h will be consistant.
As a general hint: always validate your generated HTML, for example use this: https://validator.w3.org/
You are welcome. I'm glad when I can be of some assistance.
Fun fact: the forum actually has a similar treatment of leading spaces. This markup:
Test input line 1
Test input line 2
Test input line 3
Is rendered like this:
Test input line 1
Test input line 2
Test input line 3
This is one of the reasons why using code blocks is important.
By specification, the Markdown markup language used by the forum would also eat "soft line breaks" the same way as HTML does all line breaks, so we would expect this:
Test input line 1 Test input line 2 Test input line 3
But that behavior would be too annoying for the use case of writing forum posts, so line breaks in markup are respected.
Ok, here is what I have discovered about my issue. After commenting out the
section of code as shown in the code listing things starting acting normally. I realized
that my UNO WiFi Rev2 sketch omitted this piece of code and was present in the ESP32
sketch. Not sure why exactly but I suspect it is related to the "Content-Type: text/html"
definition line. Or I could be all 'wet' about that assumption...lol
void loop() {
digitalWrite(ONBOARD_LED,HIGH); // Blink for run indicator
delay(100);
digitalWrite(ONBOARD_LED,LOW);
delay(500);
// ******************** WiFiWebServer Code***********
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available())
{
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
/* client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("</html>");
*/
client.println();
client.print(" Test input line 1");
client.println();
client.print(" Test input line 2");
client.println();
client.print(" Test input line 3");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(2000);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}