/
Arduino_GSM.ino
189 lines (155 loc) · 4.93 KB
/
Arduino_GSM.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*************************************************************
Blynk is a platform with iOS and Android apps to control
ESP32, Arduino, Raspberry Pi and the likes over the Internet.
You can easily build mobile and web interfaces for any
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: https://www.blynk.io
Sketch generator: https://examples.blynk.cc
Blynk community: https://community.blynk.cc
Follow us: https://www.fb.com/blynkapp
https://twitter.com/blynk_app
This example code is in public domain.
*************************************************************
App dashboard setup:
Value Display widget on V2
*************************************************************/
#include <GSM.h>
/* Fill in information from Blynk Device Info here */
//#define BLYNK_TEMPLATE_ID "TMPxxxxxx"
//#define BLYNK_TEMPLATE_NAME "Device"
//#define BLYNK_AUTH_TOKEN "YourAuthToken"
// Blynk cloud server
const char* host = "blynk.cloud";
unsigned int port = 80;
// Network settings
#define PINNUMBER ""
// APN data
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// Start the GSM connection
void connectNetwork()
{
Serial.println("Connecting to GSM...");
bool status = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (status == false) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
status = true;
} else {
Serial.print(".");
delay(1000);
}
}
Serial.println();
Serial.println("GSM connected");
}
bool httpRequest(const String& method,
const String& url,
const String& request,
String& response)
{
Serial.print(F("Connecting to "));
Serial.print(host);
Serial.print(":");
Serial.print(port);
Serial.print("... ");
if (client.connect(host, port)) {
Serial.println("OK");
} else {
Serial.println("failed");
return false;
}
Serial.print(method); Serial.print(" "); Serial.println(url);
client.print(method); client.print(" ");
client.print(url); client.println(F(" HTTP/1.1"));
client.print(F("Host: ")); client.println(host);
client.println(F("Connection: close"));
if (request.length()) {
client.println(F("Content-Type: application/json"));
client.print(F("Content-Length: ")); client.println(request.length());
client.println();
client.print(request);
} else {
client.println();
}
//Serial.println("Waiting response");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return false;
}
}
//Serial.println("Reading response");
int contentLength = -1;
while (client.available()) {
String line = client.readStringUntil('\n');
line.trim();
line.toLowerCase();
if (line.startsWith("content-length:")) {
contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
} else if (line.length() == 0) {
break;
}
}
//Serial.println("Reading response body");
response = "";
response.reserve(contentLength + 1);
while (response.length() < contentLength) {
if (client.available()) {
char c = client.read();
response += c;
} else if (!client.connected()) {
break;
}
}
client.stop();
return true;
}
void setup()
{
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
connectNetwork();
}
void loop() {
String response;
unsigned long value = millis();
// Send value to the cloud
// similar to Blynk.virtualWrite()
Serial.print("Sending value: ");
Serial.println(value);
if (httpRequest("GET", String("/external/api/update?token=") + BLYNK_AUTH_TOKEN + "&pin=V2&value=" + value, "", response)) {
if (response.length() != 0) {
Serial.print("WARNING: ");
Serial.println(response);
}
}
// Read the value back
// similar to Blynk.syncVirtual()
Serial.println("Reading value");
if (httpRequest("GET", String("/external/api/get?token=") + BLYNK_AUTH_TOKEN + "&pin=V2", "", response)) {
Serial.print("Value from server: ");
Serial.println(response);
}
// Set Property
Serial.println("Setting property");
if (httpRequest("GET", String("/external/api/update/property?token=") + BLYNK_AUTH_TOKEN + "&pin=V2&label=" + value, "", response)) {
if (response.length() != 0) {
Serial.print("WARNING: ");
Serial.println(response);
}
}
// For more HTTP API, see https://docs.blynk.io/en/blynk.cloud/https-api-overview
// Wait
delay(30000L);
}