/*
Here's a code snippet to toggle the onboard LED of the Arduino GIGA R1 WiFi using a webserver. This code creates a simple webpage that has a button to turn on and off the LED.
When you upload this code to your Arduino GIGA R1 WiFi board and connect it to a Wi-Fi network, you should be able to access the webpage at the board's IP address. Simply click the "ON" or "OFF" button to toggle the LED on
*/
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
WiFiServer server(80);
// Pin for the onboard LED
const int ledPin = 2;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to Wi-Fi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Start the server
server.begin();
}
void loop() {
WiFiClient client = server.available(); // Check for incoming clients
if (client) { // If a client connects
Serial.println("New client connected");
String currentLine = ""; // Make a String to hold incoming data
while (client.connected()) { // Loop while the client is connected
if (client.available()) { // If there's bytes to read from the client
char c = client.read(); // Read a byte
Serial.write(c); // Print the byte to the serial monitor
if (c == '\n') { // If the byte is a newline character
if (currentLine.length() == 0) { // If the line is blank, this is the end of the client request
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>LED Control</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Toggle LED</h1>");
client.println("<p>Click the button below to toggle the LED on and off.</p>");
client.println("<form method='get'>");
client.println("<button name='LED' value='ON' type='submit'>ON</button>");
client.println("<button name='LED' value='OFF' type='submit'>OFF</button>");
client.println("</form>");
client.println("</body>");
client.println("</html>");
break;
} else { // If the client request is not blank, add the character to the currentLine
currentLine = "";
}
} else if (c != '\r') { // If the byte is not a carriage return character, add it to the currentLine
currentLine += c;
}
if (currentLine.endsWith("GET /?LED=ON")) { // If the GET request is for turning on the LED
digitalWrite(ledPin, HIGH);
} else if (currentLine.endsWith("GET /?LED=OFF")) { // If the GET request is for turning off the LED
digitalWrite(ledPin, LOW);
}
}
}
// Close the connection
client.stop();
Serial.println("Client disconnected");
}
}
Comments