Docsity

include <ESP8266WiFi.h>, Study notes of C programming

Toggle sidebar
bg1
#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,
pf3

Partial preview of the text

Download include and more Study notes C programming in PDF only on Docsity!

#include 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(""); client.println("Networking and communications"); client.println(""); // CSS to style the on/off buttons // Feel free to change the background-color and font- size attributes to fit your preferences client.println("