forked from amcewen/HttpClient
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathMKRGSM1400WebSocket.ino
84 lines (67 loc) · 2.07 KB
/
MKRGSM1400WebSocket.ino
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
/*
MKRGSM1400 WebSocket client for ArduinoHttpClient library
Connects to the Echo WebSocket server, and sends a hello
message every 5 seconds.
created 19 Dic 2020
by Fernando Hidalgo
This example is in the public domain
*/
#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
#include "arduino_secrets.h"
char PINNUMBER[] = SECRET_PINNUMBER;
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
char serverAddress[] = "echo.websocket.org"; // server address
int port = 80; // To make a secure connection use 443
GSM gsmAccess; // pass true as param to enable debug mode: GSM gsmAccess(true);
GPRS gprs;
GSMClient gsmClient; // To make a secure connection use GSMSSLClient
WebSocketClient client = WebSocketClient(gsmClient, serverAddress, port);
int count = 0;
void setup() {
bool connected = false;
Serial.begin(9600);
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
}
void loop() {
if (!client.connected()) {
Serial.println("Starting WebSocket client");
client.begin();
} else {
startEchoMessages();
}
}
void startEchoMessages() {
sendMessage(); // send a message to receive an echo replay from the server
while (client.connected()) {
// check if a message is available to be received
int messageSize = client.parseMessage();
if (messageSize > 0) {
Serial.print("Received message: ");
Serial.println(client.readString());
// increment count for next message
count++;
sendMessage();
}
delay(5000);
}
Serial.println("Disconnected...");
}
void sendMessage() {
Serial.print("Sending: hello ");
Serial.println(count);
client.beginMessage(TYPE_TEXT);
client.print("hello ");
client.print(count);
client.endMessage();
}