Skip to content

Commit 4a253e2

Browse files
authoredOct 3, 2021
Create ws-client.ino
1 parent ad8d1d6 commit 4a253e2

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
 

‎ws-client.ino

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Author : bangjii
3+
Board : ESP8266 (WeMOS D1 R2 Mini)
4+
Library : WebSockets
5+
Updated : 03 Oct 2021
6+
7+
ESP8266 client connected to heroku websocket server.
8+
at begin connection use {"user":"youruser"} for allowed connection
9+
your-url-heroku.com/{"user":"youruser"}
10+
11+
send message using JSON format
12+
- broadcast : {"to": "brdALL", "textMsg": "holaaa"}
13+
arduino : webSocket.sendTXT("{\"to\": \"brdALL\", \"textMsg\": \"holaaa\"}");
14+
return message {"tipe":"broadcast","from":"jason","textMsg":"holaaa"}
15+
16+
- private : {"to": "john", "textMsg": "holaaa"}
17+
arduino : webSocket.sendTXT("{\"to\": \"john\", \"textMsg\": \"holaaa\"}");
18+
return message {"tipe":"private","from":"jason","textMsg":"holaaa"}
19+
20+
Arduino library for websocket : https://github.com/Links2004/arduinoWebSockets
21+
*/
22+
#include <Arduino.h>
23+
#include <ESP8266WiFi.h>
24+
#include <ESP8266WiFiMulti.h>
25+
#include <WebSocketsClient.h>
26+
#include <Hash.h>
27+
28+
ESP8266WiFiMulti WiFiMulti;
29+
WebSocketsClient webSocket;
30+
31+
#define USE_SERIAL Serial
32+
String inputString;
33+
34+
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
35+
switch(type) {
36+
case WStype_DISCONNECTED:
37+
USE_SERIAL.printf("[WSc] Disconnected!\n");
38+
break;
39+
40+
case WStype_CONNECTED: {
41+
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
42+
// send message to server when Connected
43+
webSocket.sendTXT("{\"to\": \"brdALL\", \"textMsg\": \"holaaa from ESP ;)\"}");
44+
}
45+
break;
46+
47+
case WStype_TEXT:
48+
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
49+
// send message to server
50+
// webSocket.sendTXT("message here");
51+
break;
52+
53+
case WStype_BIN:
54+
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
55+
hexdump(payload, length);
56+
// send data to server
57+
// webSocket.sendBIN(payload, length);
58+
break;
59+
60+
case WStype_PING:
61+
// pong will be send automatically
62+
//USE_SERIAL.printf("[WSc] get ping\n");
63+
break;
64+
case WStype_PONG:
65+
// answer to a ping we send
66+
//USE_SERIAL.printf("[WSc] get pong\n");
67+
break;
68+
}
69+
70+
}
71+
72+
void setup() {
73+
USE_SERIAL.begin(115200);
74+
USE_SERIAL.setDebugOutput(true);
75+
76+
USE_SERIAL.println();
77+
USE_SERIAL.println();
78+
USE_SERIAL.println();
79+
80+
for(uint8_t t = 4; t > 0; t--) {
81+
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
82+
USE_SERIAL.flush();
83+
delay(1000);
84+
}
85+
86+
WiFiMulti.addAP("Your Wifi Name", "somepassword");
87+
88+
while(WiFiMulti.run() != WL_CONNECTED) {
89+
delay(100);
90+
}
91+
92+
//begin websocket connection
93+
String user = "/{\"user\":\"jason\"}";
94+
webSocket.begin("my-greatest-app.herokuapp.com", 80, user);
95+
webSocket.onEvent(webSocketEvent);
96+
webSocket.setReconnectInterval(5000);
97+
webSocket.enableHeartbeat(15000, 3000, 2);
98+
99+
}
100+
101+
void loop() {
102+
webSocket.loop();
103+
while (Serial.available()) {
104+
char inChar = (char)Serial.read();
105+
inputString += inChar;
106+
if (inChar == '\n') {
107+
inputString.trim();
108+
//broadcast to all client
109+
String text = "{\"to\": \"brdALL\", \"textMsg\": \""+ inputString +"\"}";
110+
webSocket.sendTXT(text);
111+
inputString = "";
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)
Please sign in to comment.