I'm trying use the NodeMCU WebSocket.
I downloaded the required libraries from: https://github.com/Links2004/arduinoWebSockets and pasted them in my Arduino libraries folder. I uploaded my code but got this error:
WebSocketsServer.h: No such file or directory.
Can you please tell me why my Arduino IDE won't find the WebSocketsServer.h header file when it is physically present in my Arduino libraries folder? Or if there's an alternative way to use the NodeMCU WebSocket?
Here's the code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Hash.h>
// Replace with your network credentials
const char* ssid = "<WiFi-SSID>";
const char* password = "WiFi-PASSWORD";
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
int LEDPin = D5;
void setup(void){
//the HTML of the web page
page = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p>";
//make the LED pin output and initially turned off
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password); //begin WiFi connection
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", [](){
server.send(200, "text/html", page);
});
server.on("/LEDOn", [](){
server.send(200, "text/html", page);
digitalWrite(LEDPin, HIGH);
delay(1000);
});
server.on("/LEDOff", [](){
server.send(200, "text/html", page);
digitalWrite(LEDPin, LOW);
delay(1000);
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
Serial.println("Web server started!");
}
void loop(void){
webSocket.loop();
server.handleClient();
if (Serial.available() > 0){
char c[] = {(char)Serial.read()};
webSocket.broadcastTXT(c, sizeof(c));
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){
if (type == WStype_TEXT){
for(int i = 0; i < length; i++) Serial.print((char) payload[i]);
Serial.println();
}
}
~~~
headers
key is missing