

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!
#include <ESP8266WiFi.h> const char* ssid = "name"; const char* password = "password"; // Set web server port number to 80 WiFiServer server( 80 ); // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state int State = 0 ; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0 ; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000 ; void setup() { Serial.begin( 9600 ); // Initialize the output variables as outputs // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay( 500 ); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects,
String currentLine = ""; // make a String to hold incoming data from the client currentTime = millis(); previousTime = currentTime; while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c=client.read(); header += c; 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. 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("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /on") >= 0 ) { State= 1 ; Serial.print("E"); } else if (header.indexOf("GET /off") >= 0 ) { State= 0 ; Serial.print("A"); } // Display the HTML web page client.println(""); client.println("
<meta name="viewport" content="width=de vice-width, initial-scale=1">"); client.println("