-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathapplication.cpp
146 lines (118 loc) · 3.82 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Websocket Client Demo
* By hrsavla https://github.com/hrsavla
* 08/08/2015
* This is a simple demo of Websocket client
* Client tries to connect to echo.websocket.org
* It sents 25 messages then client connection is closed.
* It reconnects and sends 25 messages and continues doing same.
*
* This demo shows connection, closing , reconnection methods of
* websocket client.
*/
#include <SmingCore.h>
#include <Network/WebsocketClient.h>
#ifndef WIFI_SSID
#define WIFI_SSID "PutSsidHere" // Put your SSID and password here
#define WIFI_PWD "PutPasswordHere"
#endif
//Uncomment next line to enable websocket binary transfer test
//#define WS_BINARY
namespace
{
WebsocketClient wsClient;
SimpleTimer msgTimer;
// Number of messages to send
const unsigned MESSAGES_TO_SEND = 5;
// Interval (in seconds) between sending of messages
const unsigned MESSAGE_INTERVAL = 1;
// Time (in seconds) to wait before restarting client and sending another group of messages
const unsigned RESTART_PERIOD = 20;
unsigned messageCount;
bool sendBinary;
DEFINE_FSTR_LOCAL(ws_Url, WS_URL);
void sendNewMessage();
void wsConnected(WebsocketConnection&)
{
Serial << _F("Start sending messages every ") << MESSAGE_INTERVAL << _F(" second(s)...") << endl;
msgTimer.initializeMs<MESSAGE_INTERVAL * 1000>(sendNewMessage);
msgTimer.startOnce();
}
void wsMessageReceived(WebsocketConnection&, const String& message)
{
Serial << _F("WebSocket message received: ") << message << endl;
Serial << _F("Free Heap: ") << system_get_free_heap_size() << endl;
}
void wsBinReceived(WebsocketConnection&, uint8_t* data, size_t size)
{
m_printHex(_F("WebSocket BINARY received"), data, size);
Serial << _F("Free Heap: ") << system_get_free_heap_size() << endl;
}
void restart()
{
Serial.println(_F("restart..."));
messageCount = 0;
sendBinary = false;
wsClient.connect(String(ws_Url));
}
void wsDisconnected(WebsocketConnection&)
{
Serial << _F("Restarting websocket client after ") << RESTART_PERIOD << _F(" seconds") << endl;
msgTimer.initializeMs<RESTART_PERIOD * 1000>(restart);
msgTimer.startOnce();
}
void sendNewMessage()
{
if(!WifiStation.isConnected()) {
// Check if Esp8266 is connected to router
return;
}
if(messageCount >= MESSAGES_TO_SEND) {
if(sendBinary) {
Serial.println(_F("End Websocket client session"));
wsClient.close(); // clean disconnect.
return;
}
messageCount = 0;
sendBinary = true;
}
if(sendBinary) {
uint8_t buf[10];
os_get_random(buf, sizeof(buf));
buf[1] = messageCount;
m_printHex(_F("Sending websocket binary"), buf, sizeof(buf));
wsClient.sendBinary(buf, sizeof(buf));
} else {
String message = F("Hello ") + String(messageCount);
Serial << _F("Sending websocket message: ") << message << endl;
wsClient.sendString(message);
}
++messageCount;
msgTimer.startOnce();
}
void STAGotIP(IpAddress ip, IpAddress mask, IpAddress gateway)
{
Serial << _F("GOTIP - IP: ") << ip << _F(", MASK: ") << mask << _F(", GW: ") << gateway << endl;
Serial << _F("Connecting to Websocket Server ") << ws_Url << endl;
wsClient.setMessageHandler(wsMessageReceived);
wsClient.setBinaryHandler(wsBinReceived);
wsClient.setDisconnectionHandler(wsDisconnected);
wsClient.setConnectionHandler(wsConnected);
wsClient.setSslInitHandler([](Ssl::Session& session) { session.options.verifyLater = true; });
wsClient.connect(String(ws_Url));
}
void STADisconnect(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
{
Serial << _F("DISCONNECT - SSID: ") << ssid << _F(", REASON: ") << WifiEvents.getDisconnectReasonDesc(reason)
<< endl;
}
} // namespace
void init()
{
Serial.begin(COM_SPEED_SERIAL);
Serial.systemDebugOutput(true);
WifiAccessPoint.enable(false);
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiStation.enable(true);
WifiEvents.onStationGotIP(STAGotIP);
WifiEvents.onStationDisconnect(STADisconnect);
}