Hi everyone,
I am fairly new to WebServer communication and I am trying to build an Arduino WebServer that outputs a value each time it is queried by a client running on python. I am running this on an Arduino Uno R3 with an Ethernet Shield 2 (with W5500). Currently, I am aiming for a number to be sent to the client (currently just 1.0), every time a message is sent to the server, the received message is then output to a file. I have been able to successfully open a connection with my client, but it remains open so instead of getting something like this
1.0
1.0
1.0
1.0
I obtain this:
1
.0
1.0
1
.0
1
How can I set the connection up such that the WebServer only sends the 1.0 once each time the client queries it?
Here is my current Arduino code:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13};
IPAddress ip(198, 162, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
Serial.begin(9600); //Starting serial monitor
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected())
if (client.available())
{
Serial.println("new client");
// give the web browser time to receive the data
delay(1);
client.print(1.0);
delay(2000);
Serial.println();
// close the connection:
//client.stop();
}
}
}
ยดยดยด