• Hardware : ESP8266, 200Ohm Resistor, LED
  • Connection : ESP8266 D1 PIN → LED → Resistor → Ground PIN
  • Wifi AP IP: 192.168.4.1
  • Wifi Name: ESP8266class
  • Wifi Password: 123123123sorry123123
MainPrograme
#include "constants.h"

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>

ESP8266WebServer server = ESP8266WebServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);

WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;

unsigned long ts_report_ip;
const int ts_report_ip_interval = 5000;

void handleRoot() {
  server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () {  connection.send('Connect ' + new Date()); }; connection.onerror = function (error) {    console.log('WebSocket Error ', error);}; connection.onmessage = function (e) {  console.log('Server: ', e.data);}; function sendONOFF(e) {  var onoff = '#'+e;    console.log('e: ' + e); connection.send(onoff); } </script></head><body>LED Control:<br/><br/> <button type=\"button\" onclick=\"sendONOFF('1');\">ON</button><br/><button type=\"button\" onclick=\"sendONOFF('0');\">OFF</button><br/></body></html>");
}

// websocket handler
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

  switch (type) {
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", num);
      break;
    case WStype_CONNECTED: {
        IPAddress ip = webSocket.remoteIP(num);
        Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
        // send message to client
        webSocket.sendTXT(num, "Connected");
      }
      break;
    case WStype_TEXT:
      Serial.printf("[%u] get Text: %s\n", num, payload);
      if (payload[0] == '#') {
        // On off
        if (payload[1] == '0') {
          digitalWrite( LED_PIN, LOW);
        }
        if (payload[1] == '1') {
          digitalWrite( LED_PIN, HIGH);
        }
      }
      break;
  }
}

void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
  Serial.print("Station connected: ");
  Serial.println(macToString(evt.mac));
}

void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
  Serial.print("Station disconnected: ");
  Serial.println(macToString(evt.mac));
}

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  // Init wifi
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Setting up access point...");

  WiFi.softAP(ssid, password);
  // Call "onStationConnected" each time a station connects
  stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
  // Call "onStationDisconnected" each time a station disconnects
  stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);

  // Webserver
  server.on("/", handleRoot);
  server.begin();
  Serial.println("Web server started");

  // Add service to MDNS
  MDNS.addService("http", "tcp", 80);
  MDNS.addService("ws", "tcp", 81);

  // Websocket server
  webSocket.begin();
  webSocket.onEvent(webSocketEvent);

  // Set time
  ts_report_ip = millis();
}

// the loop function runs over and over again forever
void loop() {
  webSocket.loop();
  server.handleClient();

  unsigned long ts_frame = millis();  // frame time
  if ( ts_frame - ts_report_ip >= ts_report_ip_interval ) {
    ts_report_ip = ts_frame;
    Serial.println();
    Serial.print("My IP is:");
    IPAddress myIP = WiFi.softAPIP();
    Serial.println(myIP);
  }
}

String macToString(const unsigned char* mac) {
  char buf[20];
  snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return String(buf);
}
constants.h
#define LED_PIN D1

static const char *ssid = "ESP8266class";
static const char *password = "123123123sorry123123";