5

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>&nbsp;<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();
  }
}
~~~
3
  • What is the exact structure, where you have the file in your libraries folder? Directly in there, or in a subfolder? What name for the subfolder?
    – chrisl
    Sep 1, 2019 at 0:10
  • I try to change that with #include "WebSocketsServer.h" and WebSocketsServer.h is on actual dir.
    – daem
    Aug 4, 2022 at 22:39
  • the library has formal issues to be detected by Arduino IDE. in library.properties the headers key is missing
    – Juraj
    Aug 5, 2022 at 5:24

3 Answers 3

1

after you included the zip file to library you must rename directory like this 'WebSocketsServer'. And then you copy all file in 'src' directory and then paste them to its parent directory (WebSocketsServer)

1
  • didn't work with me .. do you mean like this: ..\OneDrive\Documents\Arduino\libraries\WebSocketServer\src ?
    – R1S8K
    Oct 26, 2020 at 20:38
0

The proper way is to add library into your Arduino IDE, it will just work.

  1. Open the Arduino IDE
  2. Go to Sketch > Include Library > Manage Libraries
  3. Search for "WebSockets Sattler' and install the library named "WebSockets by Markus Sattler"
  4. Restart the Arduino IDE.

enter image description here

enter image description here

PS

  • you will see two results for WebSockets library from the same author
  • the second "WebSockets_Generic" is a more lightweight library and it only provides support for WebSockets in the client mode (it is designed to be more memory efficient)
-1

proper way would be to add WebSocketsServer.h to list of locations that gcc looks for. see: https://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC6

another way would be to download WebSocketsServer.zip from https://www.arduinolibraries.info/libraries/web-sockets to your workdir and unzip it

replace

#include <WebSocketsServer.h>

with explicit location

#include "./WebSockets-2.3.5/src/WebSocketsServer.h"

(note unziped version in this case is WebSockets-2.3.5)

1
  • 3
    relative paths don't work with Arduino builder. the build is executed in a temporary folder where the path is not valid. with Arduino we don't setup gcc.
    – Juraj
    Dec 29, 2021 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.