Advertisement
microrobotics

Arduino GIGA R1 WiFi Toggle onboard LED webserver

May 10th, 2023
2,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. 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
  5. */
  6.  
  7.  
  8. #include <WiFi.h>
  9.  
  10. // Replace with your network credentials
  11. const char* ssid = "your_SSID";
  12. const char* password = "your_PASSWORD";
  13.  
  14. WiFiServer server(80);
  15.  
  16. // Pin for the onboard LED
  17. const int ledPin = 2;
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.   pinMode(ledPin, OUTPUT);
  22.   digitalWrite(ledPin, LOW);
  23.  
  24.   // Connect to Wi-Fi network
  25.   Serial.print("Connecting to ");
  26.   Serial.println(ssid);
  27.   WiFi.begin(ssid, password);
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(1000);
  30.     Serial.println("Connecting to WiFi...");
  31.   }
  32.  
  33.   Serial.println("Connected to WiFi");
  34.  
  35.   // Start the server
  36.   server.begin();
  37. }
  38.  
  39. void loop() {
  40.   WiFiClient client = server.available();  // Check for incoming clients
  41.  
  42.   if (client) {  // If a client connects
  43.     Serial.println("New client connected");
  44.     String currentLine = "";  // Make a String to hold incoming data
  45.     while (client.connected()) {  // Loop while the client is connected
  46.       if (client.available()) {  // If there's bytes to read from the client
  47.         char c = client.read();  // Read a byte
  48.         Serial.write(c);  // Print the byte to the serial monitor
  49.         if (c == '\n') {  // If the byte is a newline character
  50.           if (currentLine.length() == 0) {  // If the line is blank, this is the end of the client request
  51.             client.println("HTTP/1.1 200 OK");
  52.             client.println("Content-type:text/html");
  53.             client.println("Connection: close");
  54.             client.println();
  55.             client.println("<html>");
  56.             client.println("<head>");
  57.             client.println("<title>LED Control</title>");
  58.             client.println("</head>");
  59.             client.println("<body>");
  60.             client.println("<h1>Toggle LED</h1>");
  61.             client.println("<p>Click the button below to toggle the LED on and off.</p>");
  62.             client.println("<form method='get'>");
  63.             client.println("<button name='LED' value='ON' type='submit'>ON</button>");
  64.             client.println("<button name='LED' value='OFF' type='submit'>OFF</button>");
  65.             client.println("</form>");
  66.             client.println("</body>");
  67.             client.println("</html>");
  68.             break;
  69.           } else {  // If the client request is not blank, add the character to the currentLine
  70.             currentLine = "";
  71.           }
  72.         } else if (c != '\r') {  // If the byte is not a carriage return character, add it to the currentLine
  73.           currentLine += c;
  74.         }
  75.  
  76.         if (currentLine.endsWith("GET /?LED=ON")) {  // If the GET request is for turning on the LED
  77.           digitalWrite(ledPin, HIGH);
  78.         } else if (currentLine.endsWith("GET /?LED=OFF")) {  // If the GET request is for turning off the LED
  79.           digitalWrite(ledPin, LOW);
  80.         }
  81.       }
  82.     }
  83.     // Close the connection
  84.     client.stop();
  85.     Serial.println("Client disconnected");
  86.   }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement