-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmsGlobal.cpp
99 lines (87 loc) · 2.47 KB
/
SmsGlobal.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
#include <Arduino.h>
#include <SmsGlobal.h>
#include <WiFiClientSecure.h>
SmsGlobal::SmsGlobal(String user, String password) {
_user = user;
_password = password;
}
bool SmsGlobal::send(String from, String to, String text) {
char *fingerprint = "6E 0A 2A CD 42 A5 8E A6 23 9B 63 D8 88 8B DF C5 2E D6 6F A7";
String host = "api.smsglobal.com";
int httpsPort = 443;
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return false;
}
String url = "/http-api.php?action=sendsms"
"&user=" + _user +
"&password=" + _password +
"&from=" + urlEncode(from) +
"&to=" + urlEncode(to) +
"&text=" + urlEncode(text);
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url +
" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
if (line.startsWith("{\"state\":\"success\"")) {
return true;
} else {
return false;
}
}
// https://github.com/zenmanenergy/ESP8266-Arduino-Examples/blob/master/helloWorld_urlencoded/urlencode.ino
String SmsGlobal::urlEncode(String value)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < value.length(); i++){
c=value.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}