Send javascript variable to Arduino

Hello, it's my first time posting here.
I need to get a javascript variable into an int variable, to then turn on an LED depending of the pin in the variable. I created a web server on Arduino WiFi Rev2 with the HTML code working, but I don't know how to convert the javascript variable p into an Arduino int. The Arduino code is attached, but you need to put your SSID and password.
This is the HTML code. You have to put the link + "?p=" and then a number between 2, 7 and 12, the positions of the LEDs. The code will then redirect to a page (google.com in this case). I hope you understood my problem.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script>
      function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
          vars[key] = value;
        });
        return vars;
      }
      function getPin(p) {
        alert(p);
        if(p == 2) {
          window.open("www.google.com", "_self");
        }
        else if(p == 7) {
          window.open("www.google.com", "_self");
        }
        else if(p == 12) {
            window.open("www.google.com", "_self");
        }

      }
    </script>
  </head>
  <body>
    <script type="text/javascript">
      window.onload = function() {
        var p = getUrlVars()["p"];
        getPin(p);
      }
    </script>
  </body>
</html>

WiFiWebServer.ino (4.87 KB)

Java documentation will tell you the number of bytes in that variable. When your Arduino reads that number of bytes, it has the variable. If this was my problem, I would use Serial.print() to look at what was sent and go from there.

Paul

I did some testing and figured out I can get the value from a character. Here's the code. Thank you for your response, Paul.

#include <SPI.h>
#include <WiFiNINA.h>

#include "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 status = WL_IDLE_STATUS;
int i;
int pin = 0;
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
  }
  
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  
  // 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 < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  //print out the status
  printWifiStatus();
}


void loop() {
  // 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(c == '?') {
          i = 4;
        }
        if(i == 1) {
          pin = int(c);
          pin-=48;
        }
        else if(!i) {
          if(c >= 48 && c <= 57) {
            pin = pin*10 + int(c) - 48;
          }
        }
        
        // 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();
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("  <head>");
          client.println("    <meta charset='utf-8'>");
          client.println("    <title></title>");
          client.println("    <script>");
          client.println("      function getUrlVars() {");
          client.println("        var vars = {};");
          client.println("        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {");
          client.println("          vars[key] = value;");
          client.println("        });");
          client.println("        return vars;");
          client.println("      }");
          client.println("      function getPin(p) {");
          client.println("        if(p == 2) {");
          client.println("          window.open('https://www.google.com', '_self');");
          client.println("        }");
          client.println("        else if(p == 7) {");
          client.println("          window.open('https://www.google.com', '_self');");
          client.println("        }");
          client.println("        else if(p == 12) {");
          client.println("            window.open('https://www.google.com', '_self');");
          client.println("        }");
          client.println("");
          client.println("      }");
          client.println("    </script>");
          client.println("  </head>");
          client.println("  <body>");
          client.println("    <script type='text/javascript'>");
          client.println("      window.onload = function() {");
          client.println("        var p = getUrlVars()['p'];");
          client.println("        getPin(p);");
          client.println("      }");
          client.println("    </script>");
          client.println("  </body>");
          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;
        }
      }
      i--;
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disconnected");
    Serial.println(pin);
    digitalWrite(pin, HIGH);
    delay(5000);
    digitalWrite(pin, LOW);
    pin = 0;
  }
}


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

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

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}