Page 1 of 1

Listening to websocket messages

Posted: Sun Dec 17, 2023 12:22 pm
by FrameXX
Hi there! I am trying to connect my ESP8266 to listen to ntfy websocket. I am using this library (https://github.com/gilmaimon/ArduinoWebsockets) which should work with the ESP8266. In their example they have this line of code. client.connect(websockets_server); where websocket server is "www.myserver.com:8080". I am using ntfy.sh/mytopic/ws as the url, but the connect method returns false => connection failed. Maybe I am using wrong url format or it's someting else? Can someone help? I know this is not an arduino community, but I have tried and searched for hours using different libraries, solutions and I can't get it working. I even tryed the HTTP stream method. I only got sending messages from the ESP to ntfy server. I could poll for the messages in intervals, but that's much less efficient. Here's the code I am using. I removed the Wi-Fi SSID and password for privacy.
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ArduinoWebsockets.h>
  4.  
  5. const char* ssid = "SSID";
  6. const char* password = "PASS";
  7.  
  8. const char* host = "ntfy.sh";
  9. const char* websocketServer = "ntfy.sh/esp8266/ws";
  10. const char* requestPath = "/esp8266";
  11.  
  12. using namespace websockets;
  13.  
  14. WiFiClientSecure WLClient;
  15. WebsocketsClient WSClient;
  16.  
  17. void setupWLClient() {
  18.   Serial.println("Setting up WLClient");
  19.   WLClient.setInsecure();  // For self-signed certificates
  20. }
  21.  
  22. void setupWSClient() {
  23.   Serial.println("Setting up WSClient");
  24.   WSClient.setInsecure(); // For self-signed certificates
  25.  
  26.   // Setup Callbacks
  27.   WSClient.onMessage(onMessageCallback);
  28.   WSClient.onEvent(onEventsCallback);
  29.  
  30.   // Connect to server
  31.   bool connected =  WSClient.connect(websocketServer);
  32.   if (!connected) {
  33.     Serial.println("Failed to connect to websocket");
  34.     return;
  35.   }
  36.   Serial.println("Successfully connected to websocket");
  37. }
  38.  
  39. void onMessageCallback(WebsocketsMessage message) {
  40.     Serial.print("Got Message: ");
  41.     Serial.println(message.data());
  42. }
  43.  
  44. void onEventsCallback(WebsocketsEvent event, String data) {
  45.     if(event == WebsocketsEvent::ConnectionOpened) {
  46.         Serial.println("Connnection Opened");
  47.     } else if(event == WebsocketsEvent::ConnectionClosed) {
  48.         Serial.println("Connnection Closed");
  49.     } else if(event == WebsocketsEvent::GotPing) {
  50.         Serial.println("Got a Ping!");
  51.     } else if(event == WebsocketsEvent::GotPong) {
  52.         Serial.println("Got a Pong!");
  53.     }
  54. }
  55.  
  56. void connectWifi(const char* ssid, const char* password) {
  57.   // Connect to Wi-Fi network
  58.   Serial.println("Connecting to ");
  59.   Serial.print(ssid);
  60.   Serial.println("");
  61.  
  62.   WiFi.begin(ssid, password);
  63.  
  64.   while (WiFi.status() != WL_CONNECTED) {
  65.     delay(500);
  66.     Serial.print(".");
  67.   }
  68.   Serial.println("");
  69.   Serial.println("WiFi connected");
  70.   Serial.println("IP address: ");
  71.   Serial.println(WiFi.localIP());
  72. }
  73.  
  74. void sendMessage(const char* message) {
  75.   WSClient.send(message);
  76. }
  77.  
  78. void postMessage(const char* message) {
  79.   // Send HTTP POST request
  80.   if (WiFi.status() == WL_CONNECTED) {
  81.     const int httpPort = 443;
  82.     Serial.println("Opening connection");
  83.     if (!WLClient.connect(host, httpPort)) {
  84.       Serial.println("Connection failed");
  85.       return;
  86.     }
  87.  
  88.     String request = "POST " + String(requestPath) + " HTTP/1.1\r\n" + "Host: " + String(host) + "\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + String(strlen(message)) + "\r\n" + "Connection: close\r\n" + "\r\n" + message + "\r\n";
  89.  
  90.     WLClient.print(request);
  91.     delay(10);
  92.  
  93.     // Read the response
  94.     while (WLClient.connected()) {
  95.       while (WLClient.available()) {
  96.         char c = WLClient.read();
  97.         Serial.print(c);
  98.       }
  99.     }
  100.  
  101.     Serial.println("Message sent");
  102.     Serial.println("Closing connection");
  103.   }
  104. }
  105.  
  106. void setup() {
  107.   Serial.begin(9600);
  108.  
  109.   pinMode(15, OUTPUT);
  110.   analogWrite(15, 16);
  111.  
  112.   connectWifi(ssid, password);
  113.   setupWLClient();
  114.   setupWSClient();
  115.   postMessage("I can send messages using HTTP POST!");
  116.   sendMessage("I can send messages using Websockets!");
  117. }
  118.  
  119. void loop() {
  120.   WSClient.poll();
  121. }