I am trying to create a project in my free time just for a bit of fun. It consists of two MKR1000s (one acting as a web server connected to one laptop, and the other acting as a web client connected to a different laptop - so I can view the Serial Monitor).
One MKR1000 has a potentiometer attached to it (therefore I can change the value of it), and sends the potentiometer value to the serial monitor every 4 seconds.
THE AIM: Once the web server sets up the w WifFi network, and the web client connects to that WiFi network, I want to send the potentiometer value across to the web client every time it gets updated. However, I am clueless on how to achieve this.
This is my web server void loop code (the rest is similar to the https://www.arduino.cc/en/Tutorial/Wifi101WiFiWebServer):
void loop() {
WiFiClient client = server.available();
// listen for incoming clients
if (client) {
// if you get a client,
Serial.println("new client");
// print a message out 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
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/showValue\">here</a> showValue of the potentiometer <br>");
// 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') {
currentLine += c;
// add it to the end of the currentLine
}
if (currentLine.endsWith("GET /showValue")) {
Serial.println(sensorValue);
delay(2000);
}
}
}
client.stop();
}
}
And this is my web client code I use this https://www.arduino.cc/en/Tutorial/Wifi101WiFiWebClient - I am not sure how I should modify this in order to receive the potentiometer value across from the web server MKR1000. Any tips or hints would be hugely appreciated!
I am sorry if this is a simple question, however I am just starting with the Arduino idea, and I'm new to coding!
2 Answers 2
At the bottom of the page for the client code link you provided, there's a link to a Chat Server example. If you study the code I think you'll see how to send and receive the potentiometer reading. Hint: Look in the code where it echoes what it receives.
You don't have to use a webserver/webclient for this; a raw socket would also work. If you do want to use "web technology":
You can either have the webclient poll the webserver periodically for a new value (ask for a new value every few seconds, and interpret the response), or have the webserver initiate the communication every time a new value is available.
For the latter, you could use Server-Sent Events (SSE) or a Websocket, both of which are very google-able.
I have made examples for both, but these examples are fairly large and have extra functionality. Still, they may give you some pointers and/or code to reuse.
These examples are here:
https://github.com/ocrdu/arduino-webinterface-sse
and here:
https://github.com/ocrdu/arduino-webinterface-websocket-ap
Note that the client side in the examples is a webpage in a webbrowser, but the principle is the same.
Explore related questions
See similar questions with these tags.