Compatible Code

Hello Everyone,

I am getting closer to accomplishing my goal however I’ve come across the following code that is NOT compatible with the Arduino Uno WiFi Rev2.

  <div class="slidecontainer">
  <input type="range" min="0" max="1023" value="512" class="slider" id="myRange">
  <p>Value: <span id="demo"></span></p>
</div>

<script>
var slider = document.getElementById("myRange");

var output = document.getElementById("demo");

output.innerHTML = slider.value;

slider.onchange = function() {
  output.innerHTML = this.value;
  pwm_change(output.innerHTML);

}
function pwm_change(val) {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "setPWM?PWMval="+val, true);
     xhttp.send();
}
</script>

The slider code works fine however its the Arduino function code that does not.

void handle_pwm() {
 String  pwm_val = server.arg("PWMval"); // reading from slider on html page // THIS DOES NOT WORK

 Serial.print("slider val: ");
 Serial.println(pwm_val);
int pwm_value = map(pwm_val.toInt(), 0, 1023, 0, 180); 
Servo.write(pwm_value);

server.send(200,"text/plane","0"); // handling the webpage update // THIS DOES NOT WORK

}

server.arg and server.send are NOT Arduino Uno WiFi REV2 compatible…it gives a class WiFiserver has no member named ‘on’

Thanks for the help!

Snippets R Us!

Are you using any special library? This is the reference for the WiFi NINA Library that is the default one you get if you do a

#include <WiFiNINA.h>

Look at the server examples , you’ll have to parse the GET request

My COMPLETE code is below. The buttons which control the motor work: MotorFWD, MotorREV, MotorBRAKE. I would like to use a slider to control the motor in a more smooth way. I am controlling a small model railroad loop of track if anyone is wondering what my project is. Thanks for the help!

#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;    // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

int keyIndex = 0;             // your network key Index number (needed only for WEP)

int led = LED_BUILTIN; // LED_BUILTIN;
int status = WL_IDLE_STATUS;

#define RELAY_ONE 2
#define RELAY_TWO 4
#define MOTOR_A_ONE 12
#define MOTOR_A_TWO 9
#define MOTOR_A_ENGAGE 3
WiFiServer server(80);

void setup() {

//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.println("P&O Access Point Web Server");

pinMode(RELAY_ONE, OUTPUT);    // establishing Pin 2 as output, controls a relay
pinMode(RELAY_TWO, OUTPUT);    // establishing Pin 3 as output, controls a relay
// Setup Channel A
pinMode(MOTOR_A_ONE, OUTPUT);  // Initiates Motor Channel A pin
pinMode(MOTOR_A_TWO, OUTPUT);  // Initiates Brake Channel A pin 
 
pinMode(led, OUTPUT);  // set the LED pin mode

digitalWrite(RELAY_ONE, HIGH);
digitalWrite(RELAY_TWO, HIGH);

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication With WiFi Module Failed!");
// don't continue
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please Upgrade The Firmware");
}

// by default the local IP address of will be 192.168.4.1
// you can override it with the following:
WiFi.config(IPAddress(10, 1, 100, 45), (10, 1, 100, 1), (255, 255, 255, 0));

// print the network name (SSID);
Serial.print("Creating Access Point Named: ");
Serial.println(ssid);

// Create open network. Change this line if you want to create an WEP network:
status = WiFi.beginAP(ssid, pass);
if (status != WL_AP_LISTENING) {
Serial.println("Creating Access Point Failed");
// don't continue
while (true);
}

// wait 10 seconds for connection:
delay(10000);

// start the web server on port 80
server.begin();

// you're connected now, so print out the status
printWiFiStatus();
}

void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();

if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device Connected To AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device Disconnected From AP");
}
}
  
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 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();

// HEADER
client.println("<head>");
client.println("<title>P&O AP</title>");
client.println("</head>");
client.println();

// TITLE OF MAIN PAGE
client.println("<center><h1>P&O MAIN PAGE</h1></center>");
client.println();

// The content of the HTTP response follows the header:
client.println("<body>");
client.println("<center><table><tr><th>LED CONTROLS</th><th>TURNOUT CONTROLS</th><th>DC TRAIN CONTROLS</th></tr><tr><td><select onchange=\"location = this.value;\" aria-label=\"LEDS\"><option value=\"\" selected data-default>Select Below</option><optgroup label=\"LEDS\"></optgroup><option value=\"/H\">Led On</option><option value=\"/L\">Led Off</option><option value=\"/B\">Led Blink</option></select></td><td><select onchange=\"location = this.value;\" aria-label=\"TURNOUTS\"><option value=\"\" selected data-default>Select Below</option><optgroup label=\"TURNOUTS\"></optgroup><option value=\"/RON\">T0-1 N</option><option value=\"/ROFF\">T0-1 R</option></td><td><select onchange=\"location = this.value;\" aria-label=\"TRAINS\"><option value=\"\" selected data-default>Select Below</option><optgroup label=\"TRAIN 1\"></optgroup><option value=\"/MONFWD\">Train 1 FWD</option><option value=\"/MONREV\">Train 1 REV</option><option value=\"/MOFF\">Train 1 BRAKE</option></select></td></tr><tr><td><button><a href=\"/B\" style=\"text-decoration: none\">TEST BUTTON</a></button></td></tr></table></center>");

client.println("<style>");
client.println(".class-name{writing-mode: bt-lr;-webkit-appearance: slider-vertical;width: 8px;height: 175px;padding: 0 5px;}</style>");
client.println("<center><input type=\"range\" min=\"0\" max=\"255\" value=\"0\" class=\"class-name\" orient\"vertical\" id=\"myRange\">");
client.println("<p><b>SPEED:</b> <span id=\"demo\"></span></p></center>");
client.println("</style>");

client.println("<script>");
client.println("var slider = document.getElementById(\"myRange\");");
client.println("var output = document.getElementById(\"demo\");");
client.println("output.innerHTML = slider.value;");
client.println("slider.onchange = function() { output.innerHTML = this.value; pwm_change(output.innerHTML);");
client.println("}");

client.println("function pwm_change(val) { var xhttp = new XMLHttpRequest(); xhttp.open(\"GET\", \"setPWM?PWMval=\"+val, true); xhttp.send();");
client.println("}");
client.println("</script>");

client.println("</body>");

// 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
}

// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(led, HIGH);               // GET /H turns the LED on
} // END IF LED ON

if (currentLine.endsWith("GET /L")) {
digitalWrite(led, LOW);                // GET /L turns the LED off
} // END IF LED OFF


if (currentLine.endsWith("GET /MONFWD")) {
digitalWrite(MOTOR_A_ONE, HIGH);  // Establishes forward direction of Channel A
digitalWrite(MOTOR_A_TWO, HIGH);  // ENGAGE BRAKE
delay(1000);                      // DELAY BEFORE RELEASING BRAKE
digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
analogWrite(MOTOR_A_ENGAGE, 255); // Spins the motor forward on Channel A at full speed
} // END IF MOTOR A ON FWD

if (currentLine.endsWith("GET /MONREV")) {
digitalWrite(MOTOR_A_ONE, LOW);   // Establishes reverse direction of Channel A
digitalWrite(MOTOR_A_TWO, HIGH);  // ENGAGE BRAKE
delay(1000);                      // DELAY BEFORE RELEASING BRAKE
digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
analogWrite(MOTOR_A_ENGAGE, 255); // Spins the motor reverse on Channel A at full speed
} // END MOTOR A ON REV

if (currentLine.endsWith("GET /MOFF")) {
digitalWrite(MOTOR_A_ONE, LOW);  // Establishes forward direction of Channel A
digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
analogWrite(MOTOR_A_ENGAGE, 0); // Stops the motor on Channel A at full speed
} // END IF MOTOR A OFF


}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected");
}
}

void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print where to go in a browser:
Serial.print("To Connect To The Railroad, Open Web Browser To http://");
Serial.println(ip);

} // END VOID

so at the moment you check for different types of GET commands, for example this code

        if (currentLine.endsWith("GET /MOFF")) {
          digitalWrite(MOTOR_A_ONE, LOW);  // Establishes forward direction of Channel A
          digitalWrite(MOTOR_A_TWO, LOW);   // Disengage the Brake for Channel A
          analogWrite(MOTOR_A_ENGAGE, 0); // Stops the motor on Channel A at full speed
        } // END IF MOTOR A OFF

looks at what you received to see if it was a GET request with "GET /MOFF" at the end of the URL

What you want to do is check if you have received a "GET /setPWM?PWMval=xxxxx" but since there is the xxxxx value you don't know, you can't just compare with a currentLine.endsWith() as the [url=https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/endswith/]endsWith()[/url] method Tests whether or not a String ends with the characters passed as parameter.

What you need to do is find out if "GET /setPWM?PWMval=" is within the string and if so, extract the value that is after the = sign

--> look at the [

indexOf()

](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/) method