|
| 1 | +/**************************************************************************************************************************** |
| 2 | + WebSocketClient_RP2040W.ino |
| 3 | + For RP2040W boards using CYC43439 WiFi |
| 4 | +
|
| 5 | + Blynk_WiFiNINA_WM is a library for the Mega, Teensy, SAM DUE, nRF52, STM32 and SAMD boards |
| 6 | + (https://github.com/khoih-prog/Blynk_WiFiNINA_WM) to enable easy configuration/reconfiguration and |
| 7 | + autoconnect/autoreconnect of WiFiNINA/Blynk |
| 8 | +
|
| 9 | + Based on and modified from WebSockets libarary https://github.com/Links2004/arduinoWebSockets |
| 10 | + to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc. |
| 11 | +
|
| 12 | + Built by Khoi Hoang https://github.com/khoih-prog/WebSockets_Generic |
| 13 | + Licensed under MIT license |
| 14 | +
|
| 15 | + Created on: 24.05.2015 |
| 16 | + Author: Markus Sattler |
| 17 | + *****************************************************************************************************************************/ |
| 18 | + |
| 19 | +#if ( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) |
| 20 | + #if defined(WEBSOCKETS_NETWORK_TYPE) |
| 21 | + #undef WEBSOCKETS_NETWORK_TYPE |
| 22 | + #endif |
| 23 | + #define WEBSOCKETS_NETWORK_TYPE NETWORK_RP2040W_WIFI |
| 24 | +#else |
| 25 | + #error This code is intended to run only on the RP2040W boards ! Please check your Tools->Board setting. |
| 26 | +#endif |
| 27 | + |
| 28 | +#define _WEBSOCKETS_LOGLEVEL_ 2 |
| 29 | + |
| 30 | +#include <WiFi.h> |
| 31 | + |
| 32 | +#include <WebSocketsClient_Generic.h> |
| 33 | + |
| 34 | +WebSocketsClient webSocket; |
| 35 | + |
| 36 | +int status = WL_IDLE_STATUS; |
| 37 | + |
| 38 | +// Deprecated echo.websocket.org to be replaced |
| 39 | +#define WS_SERVER "wss://echo.websocket.org" |
| 40 | +#define SSL_PORT 443 |
| 41 | + |
| 42 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 43 | + |
| 44 | +char ssid[] = "your_ssid"; // your network SSID (name) |
| 45 | +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ |
| 46 | + |
| 47 | +bool alreadyConnected = false; |
| 48 | + |
| 49 | +void webSocketEvent(const WStype_t& type, uint8_t * payload, const size_t& length) |
| 50 | +{ |
| 51 | + switch (type) |
| 52 | + { |
| 53 | + case WStype_DISCONNECTED: |
| 54 | + if (alreadyConnected) |
| 55 | + { |
| 56 | + Serial.println("[WSc] Disconnected!"); |
| 57 | + alreadyConnected = false; |
| 58 | + } |
| 59 | + |
| 60 | + break; |
| 61 | + |
| 62 | + case WStype_CONNECTED: |
| 63 | + { |
| 64 | + alreadyConnected = true; |
| 65 | + |
| 66 | + Serial.print("[WSc] Connected to url: "); |
| 67 | + Serial.println((char *) payload); |
| 68 | + |
| 69 | + // send message to server when Connected |
| 70 | + webSocket.sendTXT("Connected"); |
| 71 | + } |
| 72 | + |
| 73 | + break; |
| 74 | + |
| 75 | + case WStype_TEXT: |
| 76 | + Serial.print("[WSc] get text: "); |
| 77 | + Serial.println((char *) payload); |
| 78 | + |
| 79 | + // send message to server |
| 80 | + webSocket.sendTXT("message here"); |
| 81 | + |
| 82 | + break; |
| 83 | + |
| 84 | + case WStype_BIN: |
| 85 | + Serial.print("[WSc] get binary length: "); |
| 86 | + Serial.println(length); |
| 87 | + |
| 88 | + // KH, To check |
| 89 | + // hexdump(payload, length); |
| 90 | + |
| 91 | + // send data to server |
| 92 | + webSocket.sendBIN(payload, length); |
| 93 | + |
| 94 | + break; |
| 95 | + |
| 96 | + case WStype_PING: |
| 97 | + // pong will be send automatically |
| 98 | + Serial.println("[WSc] get ping"); |
| 99 | + |
| 100 | + break; |
| 101 | + |
| 102 | + case WStype_PONG: |
| 103 | + // answer to a ping we send |
| 104 | + Serial.println("[WSc] get pong"); |
| 105 | + |
| 106 | + break; |
| 107 | + |
| 108 | + default: |
| 109 | + break; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void printWifiStatus() |
| 114 | +{ |
| 115 | + // print the SSID of the network you're attached to: |
| 116 | + Serial.print("SSID: "); |
| 117 | + Serial.println(WiFi.SSID()); |
| 118 | + |
| 119 | + // print your board's IP address: |
| 120 | + IPAddress ip = WiFi.localIP(); |
| 121 | + Serial.print("WebSockets Client IP Address: "); |
| 122 | + Serial.println(ip); |
| 123 | + |
| 124 | + // print the received signal strength: |
| 125 | + long rssi = WiFi.RSSI(); |
| 126 | + Serial.print("signal strength (RSSI):"); |
| 127 | + Serial.print(rssi); |
| 128 | + Serial.println(" dBm"); |
| 129 | +} |
| 130 | + |
| 131 | +void setup() |
| 132 | +{ |
| 133 | + //Initialize serial and wait for port to open: |
| 134 | + Serial.begin(115200); |
| 135 | + while (!Serial); |
| 136 | + |
| 137 | + Serial.print("\nStart WebSocketClientSSL_RP2040W on "); Serial.println(BOARD_NAME); |
| 138 | + Serial.println(WEBSOCKETS_GENERIC_VERSION); |
| 139 | + |
| 140 | + /////////////////////////////////// |
| 141 | + |
| 142 | + // check for the WiFi module: |
| 143 | + if (WiFi.status() == WL_NO_MODULE) |
| 144 | + { |
| 145 | + Serial.println("Communication with WiFi module failed!"); |
| 146 | + // don't continue |
| 147 | + while (true); |
| 148 | + } |
| 149 | + |
| 150 | + Serial.print(F("Connecting to SSID: ")); |
| 151 | + Serial.println(ssid); |
| 152 | + |
| 153 | + status = WiFi.begin(ssid, pass); |
| 154 | + |
| 155 | + delay(1000); |
| 156 | + |
| 157 | + // attempt to connect to WiFi network |
| 158 | + while ( status != WL_CONNECTED) |
| 159 | + { |
| 160 | + delay(500); |
| 161 | + |
| 162 | + // Connect to WPA/WPA2 network |
| 163 | + status = WiFi.status(); |
| 164 | + } |
| 165 | + |
| 166 | + printWifiStatus(); |
| 167 | + |
| 168 | + /////////////////////////////////// |
| 169 | + |
| 170 | + // server address, port and URL |
| 171 | + Serial.print("WebSockets Server : "); |
| 172 | + Serial.println(WS_SERVER); |
| 173 | + |
| 174 | + // server address, port and URL |
| 175 | + webSocket.beginSSL(WS_SERVER, SSL_PORT); |
| 176 | + |
| 177 | + // event handler |
| 178 | + webSocket.onEvent(webSocketEvent); |
| 179 | + |
| 180 | + // server address, port and URL |
| 181 | + Serial.print("Connecting to WebSockets Server @ "); |
| 182 | + Serial.println(WS_SERVER); |
| 183 | +} |
| 184 | + |
| 185 | +void loop() |
| 186 | +{ |
| 187 | + webSocket.loop(); |
| 188 | +} |
0 commit comments