Files

87 lines
2.1 KiB
Arduino
Raw Permalink Normal View History

/*
2015-05-24 15:40:47 +02:00
* WebSocketServer.ino
*
* Created on: 22.05.2015
*
*/
#include <Arduino.h>
2018-02-07 17:28:17 +01:00
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
2015-05-23 07:33:26 +02:00
#include <Hash.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsServer webSocket = WebSocketsServer(81);
2015-07-20 19:29:37 +02:00
#define USE_SERIAL Serial1
2017-03-06 15:07:20 -05:00
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
2015-07-20 19:29:37 +02:00
USE_SERIAL.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
2015-05-23 09:47:39 +02:00
{
IPAddress ip = webSocket.remoteIP(num);
2015-07-20 19:29:37 +02:00
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
2018-02-07 17:28:17 +01:00
2015-11-26 15:19:07 +01:00
// send message to client
webSocket.sendTXT(num, "Connected");
2015-05-23 09:47:39 +02:00
}
break;
case WStype_TEXT:
2015-07-20 19:29:37 +02:00
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
2015-11-26 15:19:07 +01:00
// send message to client
// webSocket.sendTXT(num, "message here");
// send data to all connected clients
2015-11-26 15:19:07 +01:00
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
2017-03-06 15:07:20 -05:00
USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
hexdump(payload, length);
2015-11-26 15:19:07 +01:00
// send message to client
2017-03-06 15:07:20 -05:00
// webSocket.sendBIN(num, payload, length);
break;
}
}
void setup() {
2015-07-20 19:29:37 +02:00
// USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
2015-07-20 19:29:37 +02:00
USE_SERIAL.setDebugOutput(true);
2015-07-20 19:29:37 +02:00
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
2015-07-20 19:29:37 +02:00
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("SSID", "passpasspass");
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}