Skip to content

Commit 26c939e

Browse files
committed
Add MKR GSM SSL Client example
1 parent bb0447a commit 26c939e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This example creates a client object that connects and transfers
3+
data using always SSL.
4+
5+
It is compatible with the methods normally related to plain
6+
connections, like client.connect(host, port).
7+
8+
Written by Arturo Guadalupi
9+
last revision November 2015
10+
11+
*/
12+
13+
#include <MKRGSM.h>
14+
#include <ArduinoBearSSL.h>
15+
16+
const char pin[] = "";
17+
const char apn[] = "apn";
18+
const char login[] = "login";
19+
const char password[] = "pass";
20+
21+
// if you don't want to use DNS (and reduce your sketch size)
22+
// use the numeric IP instead of the name for the server:
23+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
24+
char server[] = "www.google.com"; // name address for Google (using DNS)
25+
26+
GPRS gprs;
27+
GSM gsmAccess;
28+
29+
// Initialize the Ethernet client library
30+
// with the IP address and port of the server
31+
// that you want to connect to (port 80 is default for HTTP):
32+
GSMClient client;
33+
BearSSLClient sslClient(client);
34+
35+
unsigned long getTime() {
36+
return gsmAccess.getTime();
37+
}
38+
39+
void setup() {
40+
//Initialize serial and wait for port to open:
41+
Serial.begin(9600);
42+
while (!Serial) {
43+
; // wait for serial port to connect. Needed for native USB port only
44+
}
45+
46+
// connection state
47+
boolean notConnected = true;
48+
49+
// After starting the modem with GSM.begin()
50+
// attach the shield to the GPRS network with the APN, login and password
51+
while (notConnected) {
52+
if ((gsmAccess.begin(pin) == GSM_READY) &
53+
(gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
54+
notConnected = false;
55+
} else {
56+
Serial.println("Not connected");
57+
delay(1000);
58+
}
59+
}
60+
61+
Serial.println("Connected to GPRS");
62+
63+
ArduinoBearSSL.onGetTime(getTime);
64+
65+
Serial.println("\nStarting connection to server...");
66+
// if you get a connection, report back via serial:
67+
if (sslClient.connect(server, 443)) {
68+
Serial.println("connected to server");
69+
// Make a HTTP request:
70+
sslClient.println("GET /search?q=arduino HTTP/1.1");
71+
sslClient.println("Host: www.google.com");
72+
sslClient.println("Connection: close");
73+
sslClient.println();
74+
}
75+
}
76+
77+
void loop() {
78+
// if there are incoming bytes available
79+
// from the server, read them and print them:
80+
while (sslClient.available()) {
81+
char c = sslClient.read();
82+
Serial.write(c);
83+
}
84+
85+
// if the server's disconnected, stop the client:
86+
if (!sslClient.connected()) {
87+
Serial.println();
88+
Serial.println("disconnecting from server.");
89+
sslClient.stop();
90+
91+
// do nothing forevermore:
92+
while (true);
93+
}
94+
}
95+

0 commit comments

Comments
 (0)