• You might need to go Aeroplane mode to get the browser to recognize the WIFI


main.cpp
#include "constants.h"
#include <Adafruit_NeoPixel.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;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, LED_PIN, NEO_GRB + NEO_KHZ800);

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) {
  int col;
  int rgb[3];
  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);
          for ( int i = 0; i < 5; i++ ) {
            strip.setPixelColor(i, strip.Color(0, 0, 0));
          }
          strip.show();
        }
        if (payload[1] == '1') {
          // digitalWrite( LED_PIN, HIGH);
          col = random(1, 8);
          rgb[0] = col & 0x01 ? 255 : 0;
          rgb[1] = col & 0x02 ? 255 : 0;
          rgb[2] = col & 0x04 ? 255 : 0;
          for ( int i = 0; i < 5; i++ ) {
            strip.setPixelColor(i, strip.Color(rgb[0], rgb[1], rgb[2]));
          }
          strip.show();
        }
      }
      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);

  strip.setBrightness(64);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  // Set time
  ts_report_ip = millis();

  randomSeed(ts_report_ip);
}

// 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 D3
 
static const char *ssid = "ESP8266NEO";
static const char *password = "123123123";


  • No labels