- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ArduinoWebsockets.h>
- const char* ssid = "SSID";
- const char* password = "PASS";
- const char* host = "ntfy.sh";
- const char* websocketServer = "ntfy.sh/esp8266/ws";
- const char* requestPath = "/esp8266";
- using namespace websockets;
- WiFiClientSecure WLClient;
- WebsocketsClient WSClient;
- void setupWLClient() {
- Serial.println("Setting up WLClient");
- WLClient.setInsecure(); // For self-signed certificates
- }
- void setupWSClient() {
- Serial.println("Setting up WSClient");
- WSClient.setInsecure(); // For self-signed certificates
- // Setup Callbacks
- WSClient.onMessage(onMessageCallback);
- WSClient.onEvent(onEventsCallback);
- // Connect to server
- bool connected = WSClient.connect(websocketServer);
- if (!connected) {
- Serial.println("Failed to connect to websocket");
- return;
- }
- Serial.println("Successfully connected to websocket");
- }
- void onMessageCallback(WebsocketsMessage message) {
- Serial.print("Got Message: ");
- Serial.println(message.data());
- }
- void onEventsCallback(WebsocketsEvent event, String data) {
- if(event == WebsocketsEvent::ConnectionOpened) {
- Serial.println("Connnection Opened");
- } else if(event == WebsocketsEvent::ConnectionClosed) {
- Serial.println("Connnection Closed");
- } else if(event == WebsocketsEvent::GotPing) {
- Serial.println("Got a Ping!");
- } else if(event == WebsocketsEvent::GotPong) {
- Serial.println("Got a Pong!");
- }
- }
- void connectWifi(const char* ssid, const char* password) {
- // Connect to Wi-Fi network
- Serial.println("Connecting to ");
- Serial.print(ssid);
- Serial.println("");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void sendMessage(const char* message) {
- WSClient.send(message);
- }
- void postMessage(const char* message) {
- // Send HTTP POST request
- if (WiFi.status() == WL_CONNECTED) {
- const int httpPort = 443;
- Serial.println("Opening connection");
- if (!WLClient.connect(host, httpPort)) {
- Serial.println("Connection failed");
- return;
- }
- 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";
- WLClient.print(request);
- delay(10);
- // Read the response
- while (WLClient.connected()) {
- while (WLClient.available()) {
- char c = WLClient.read();
- Serial.print(c);
- }
- }
- Serial.println("Message sent");
- Serial.println("Closing connection");
- }
- }
- void setup() {
- Serial.begin(9600);
- pinMode(15, OUTPUT);
- analogWrite(15, 16);
- connectWifi(ssid, password);
- setupWLClient();
- setupWSClient();
- postMessage("I can send messages using HTTP POST!");
- sendMessage("I can send messages using Websockets!");
- }
- void loop() {
- WSClient.poll();
- }
Listening to websocket messages
Listening to websocket messages
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.
Who is online
Users browsing this forum: No registered users and 38 guests