1+ #include < Arduino.h>
2+ #include < SPI.h>
3+ #include < WiFiNINA.h>
4+ #include < WebSocketsClient.h>
5+
6+ #define WIFI_SSID " "
7+ #define WIFI_PASS " "
8+
9+ int status = WL_IDLE_STATUS;
10+ WiFiClient client;
11+ WebSocketsClient webSocket;
12+
13+ void webSocketEvent (WStype_t type, uint8_t *payload, size_t length) {
14+
15+ switch (type) {
16+ case WStype_DISCONNECTED:
17+ Serial.println (" [WSc] Disconnected!" );
18+ break ;
19+ case WStype_CONNECTED:
20+ Serial.println (" [WSc] Connected!" );
21+
22+ // send message to server when Connected
23+ webSocket.sendTXT (" Connected" );
24+ break ;
25+ case WStype_TEXT:
26+ Serial.print (" [WSc] get text:" );
27+ Serial.println ((char *)payload);
28+
29+ // send message to server
30+ // webSocket.sendTXT("message here");
31+ break ;
32+ case WStype_BIN:
33+ // send data to server
34+ // webSocket.sendBIN(payload, length);
35+ break ;
36+ case WStype_ERROR:
37+ case WStype_FRAGMENT_TEXT_START:
38+ case WStype_FRAGMENT_BIN_START:
39+ case WStype_FRAGMENT:
40+ case WStype_PING:
41+ case WStype_PONG:
42+ case WStype_FRAGMENT_FIN:
43+ break ;
44+ }
45+ }
46+
47+ void setup () {
48+ Serial.begin (115200 );
49+
50+ while (!Serial) {
51+ ; // wait for serial port to connect. Needed for native USB port only
52+ }
53+
54+ Serial.println ();
55+ Serial.println ();
56+ Serial.println ();
57+
58+ // check for the WiFi module:
59+ if (WiFi.status () == WL_NO_MODULE) {
60+ Serial.println (" Communication with WiFi module failed!" );
61+ // don't continue
62+ while (true );
63+ }
64+
65+ String fv = WiFi.firmwareVersion ();
66+ if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
67+ Serial.println (" Please upgrade the firmware" );
68+ }
69+
70+ // attempt to connect to WiFi network:
71+ while (status != WL_CONNECTED) {
72+ Serial.print (" Attempting to connect to SSID: " );
73+ Serial.println (WIFI_SSID);
74+ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
75+ status = WiFi.begin (WIFI_SSID, WIFI_PASS);
76+
77+ // wait 10 seconds for connection:
78+ delay (10000 );
79+ }
80+
81+ Serial.println (" Connected to WiFi" );
82+
83+ // print your board's IP address:
84+ IPAddress ip = WiFi.localIP ();
85+ Serial.print (" IP Address: " );
86+ Serial.println (ip);
87+
88+ // server address, port and URL
89+ webSocket.begin (" 192.168.0.123" , 8011 );
90+
91+ // event handler
92+ webSocket.onEvent (webSocketEvent);
93+
94+ // try ever 5000 again if connection has failed
95+ webSocket.setReconnectInterval (5000 );
96+ }
97+
98+ void loop () {
99+ webSocket.loop ();
100+ }
0 commit comments