Close
Skip to content

basic wifi and web server set up on 8622

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "secrets.h" //put any thing you dont want in your source code in here then call it.




const char* ssid = SSID; //char array containing ssid stored in secrets.
const char* password = PASSWORD; //char array containing password stored in secrets.




ESP8266WebServer server(8081); // tell ie what internal port to run on then set your port forwarding on your router to port 80 pointing to this port.
// i've used external port 8082 as i've got port 80 already poinnting at a different ESP32 for the clock
#define LED_PIN 2  // 




void handleRequest() { //defines a function that is called when a server.on routine is activated below.
  digitalWrite(LED_PIN, HIGH);
  server.send(200, "text/plain", "Make the light come on for 5 seconds!"); // whatever you type here is HTML that will pump out to the webpage. 
  delay(5000); 
  digitalWrite(LED_PIN, LOW);
}




void setup() {
  pinMode(LED_PIN, OUTPUT);




  WiFi.begin(ssid, password); //starts the connection to wifi
  while (WiFi.status() != WL_CONNECTED) delay(500); // these two lines are simplest way of connecting to wifi without any debugging.
  server.on("/sparkle", handleRequest); //this sets up an event that when this sub directory is detected it runs handleRequest function.
  server.begin(); //this just starts the webserver. esp8266 uses a different library for webserver than esp32.
}




void loop() {
  server.handleClient(); //this runs in loop to detect new connections. Its not necessary if you dont have blocking code in your functions
}