1

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!

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Nov 8, 2016 at 19:46

2 Answers 2

1

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.

mkr1000 WiFi Chat Server

answered Nov 12, 2016 at 0:37
0

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.

answered Aug 24, 2020 at 14:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.