1
+ #include < Arduino.h>
2
+ #include < stdarg.h>
3
+ #include < stdio.h>
4
+
5
+ #include " WiFiS3.h"
6
+ #include < WebSocketsClient.h>
7
+
8
+ #define WIFI_SSID " "
9
+ #define WIFI_PASS " "
10
+
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_FRAGMENT_FIN:
41
+ break ;
42
+ }
43
+ }
44
+
45
+ void setup () {
46
+ Serial.begin (115200 );
47
+
48
+ while (!Serial) {
49
+ ; // wait for serial port to connect. Needed for native USB port only
50
+ }
51
+
52
+ Serial.println ();
53
+ Serial.println ();
54
+ Serial.println ();
55
+
56
+ for (uint8_t t = 4 ; t > 0 ; t--) {
57
+ Serial.println (" [SETUP] BOOT WAIT ..." );
58
+ Serial.flush ();
59
+ delay (1000 );
60
+ }
61
+
62
+ // check for the WiFi module:
63
+ if (WiFi.status () == WL_NO_MODULE) {
64
+ Serial.println (" Communication with WiFi module failed!" );
65
+ // don't continue
66
+ while (true )
67
+ ;
68
+ }
69
+
70
+ String fv = WiFi.firmwareVersion ();
71
+ if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
72
+ Serial.println (" Please upgrade the firmware" );
73
+ }
74
+
75
+ Serial.println (" [Wifi]: Connecting" );
76
+
77
+ int status = WL_IDLE_STATUS;
78
+
79
+ // attempt to connect to WiFi network:
80
+ while (status != WL_CONNECTED) {
81
+ Serial.print (" [Wifi]: Attempting to connect to SSID: " );
82
+ Serial.println (WIFI_SSID);
83
+
84
+ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
85
+ status = WiFi.begin (WIFI_SSID, WIFI_PASS);
86
+
87
+ delay (1000 );
88
+ }
89
+
90
+ Serial.println (" Connected!" );
91
+
92
+ // print your board's IP address:
93
+ IPAddress ip = WiFi.localIP ();
94
+ Serial.print (" IP Address: " );
95
+ Serial.println (ip);
96
+
97
+ // server address, port and URL
98
+ webSocket.begin (" 192.168.0.123" , 8011 );
99
+
100
+ // event handler
101
+ webSocket.onEvent (webSocketEvent);
102
+
103
+ // try ever 5000 again if connection has failed
104
+ webSocket.setReconnectInterval (5000 );
105
+ }
106
+
107
+ void loop () {
108
+ webSocket.loop ();
109
+ }
0 commit comments