Hi,
I'm working on Arduino WiFi MKR 1010, I want to get a value from a table on my webpage to toogle a led (pin 6).
Can someone help me please ?
this is my code:
#include <SPI.h>
#include <WiFiNINA.h>
#include <Servo.h>
char ssid[] = "******";
char pass[] = "******";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
//special characters
char quote = '"';
char slash = '/';
Servo myservo;
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(6, OUTPUT); // set the LED pin mode
pinMode(7, OUTPUT);
myservo.attach(9);
myservo.write(45);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWiFiStatus(); // you're connected now, so print out the status
}
void loop() {
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
status = WiFi.begin(ssid, pass);
delay(10000);
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
Serial.println("new client");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Content-Type: application/x-javascript");
client.println();
client.println("<!DOCTYPE HTML>");
client.print("<head>");
client.print("</head>");
client.print("<body>");
client.println("<center>
<div class='container'><h1>CONTROL YOUR ARDUINO<h1/></div></center>");
client.print("<center><h2> LED Rouge");
client.println("</h2>");
client.println("<center><div class='container'><left><button class='on' type='submit' value='ON' onmousedown=location.href='/H\'>ON</button>");
client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/L\'>OFF</button></div>
");
client.print("<h2> LED Verte");
client.println("</h2>");
client.println("<center><div class='container'><left><button class='on' type='submit' value='ON' onmousedown=location.href='/X\'>ON</button>");
client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/P\'>OFF</button></div>
");
client.print("<from action='/get>'>");
client.print("<input type='number' name=r id = 'input'> Clignotant LED
");
client.println("<button class='submit' type='submit' value='set' onmousedown=location.href='/T\' onclick='getInputValue();'>Get Value</button></div>
");
client.print("<script>");
client.print("funtion getInputValue()");
client.println("{");
client.println("var inputVal = document.getElementById('input').value");
client.println("alert(inputVal)");
client.println("}</script>");
client.print("<script>function save(tableID)");
client.print("{");
client.print("var a = document.getElementById(tableID).value;");
client.print("var x = alert(col[0].firstChild.innerHTML);");
client.print("} </script>");
client.print("<h2>Choisir_LED</h2>");
client.print("<input type='button' name='btnSave' value='Save' onclick='save('dataTable')' />");
client.print("<input type='button' value='Add Row' onclick='addRow('dataTable')' />");
client.print("<input type='button' value='Delete Row' onclick='deleteRow('dataTable')' />");
client.print("<table id='table1' width='400px' border='0' >");
client.print("<tr>");
client.print("<td style='padding-left:30px'>Nom de la LED</td>");
client.print("<td style='padding-left:120px'>Nb clignant</td>");
client.print("</tr>");
client.print("<table id='dataTable' width='350px' border='1'>");
client.print("<tr>");
client.print("<td><input type='checkbox' name='chk'/></td>");
client.print("<td><input type='text' name='txt1' /></td>");
client.print("<td><input type='text' name='txt'/></td>");
client.print(" </tr> </table> </table>");
client.println("</body>");
client.println("</html>");
client.println();
break;
}
else {
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
}
if (currentLine.endsWith("GET /X")) {
for(int i= 0 ; i< parsInt(a) ; i++){
digitalWrite(6, HIGH); // GET /L turns the pin 6 LED off
delay(500);
digitalWrite(6, LOW);
delay(500);
}
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
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 the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}