Skip to content

Commit ad0e9b2

Browse files
authored
Merge pull request #98 from sandeepmistry/mkr-gsm-1400-examples
Add new examples for MKR GSM 1400 boards
2 parents 7b2e978 + cdc129a commit ad0e9b2

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// This example uses an Arduino MKR GSM 1400 board
2+
// to connect to shiftr.io.
3+
//
4+
// IMPORTANT: This example uses the new MKRGSM library.
5+
//
6+
// You can check on your device after a successful
7+
// connection here: https://shiftr.io/try.
8+
//
9+
// by Sandeep Mistry
10+
// https://github.com/256dpi/arduino-mqtt
11+
12+
#include <MKRGSM.h>
13+
#include <MQTTClient.h>
14+
15+
const char pin[] = "";
16+
const char apn[] = "apn";
17+
const char login[] = "login";
18+
const char password[] = "password";
19+
20+
GSMClient net;
21+
GPRS gprs;
22+
GSM gsmAccess;
23+
MQTTClient client;
24+
25+
unsigned long lastMillis = 0;
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
30+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
31+
// You need to set the IP address directly.
32+
client.begin("broker.shiftr.io", net);
33+
client.onMessage(messageReceived);
34+
35+
connect();
36+
}
37+
38+
void connect() {
39+
// connection state
40+
boolean connected = false;
41+
42+
Serial.print("connecting to cellular network ...");
43+
44+
// After starting the modem with gsmAccess.begin()
45+
// attach to the GPRS network with the APN, login and password
46+
while (!connected) {
47+
if ((gsmAccess.begin(pin) == GSM_READY) &&
48+
(gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
49+
connected = true;
50+
} else {
51+
Serial.print(".");
52+
delay(1000);
53+
}
54+
}
55+
56+
Serial.print("\nconnecting...");
57+
while (!client.connect("arduino", "try", "try")) {
58+
Serial.print(".");
59+
delay(1000);
60+
}
61+
62+
Serial.println("\nconnected!");
63+
64+
client.subscribe("/hello");
65+
// client.unsubscribe("/hello");
66+
}
67+
68+
void loop() {
69+
client.loop();
70+
71+
if (!client.connected()) {
72+
connect();
73+
}
74+
75+
// publish a message roughly every second.
76+
if (millis() - lastMillis > 1000) {
77+
lastMillis = millis();
78+
client.publish("/hello", "world");
79+
}
80+
}
81+
82+
void messageReceived(String &topic, String &payload) {
83+
Serial.println("incoming: " + topic + " - " + payload);
84+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// This example uses an Arduino MKR GSM 1400 board
2+
// to securely connect to shiftr.io.
3+
//
4+
// IMPORTANT: This example uses the new MKRGSM library.
5+
//
6+
// You can check on your device after a successful
7+
// connection here: https://shiftr.io/try.
8+
//
9+
// by Sandeep Mistry
10+
// https://github.com/256dpi/arduino-mqtt
11+
12+
#include <MKRGSM.h>
13+
#include <MQTTClient.h>
14+
15+
const char pin[] = "";
16+
const char apn[] = "apn";
17+
const char login[] = "login";
18+
const char password[] = "password";
19+
20+
GSMSSLClient net;
21+
GPRS gprs;
22+
GSM gsmAccess;
23+
MQTTClient client;
24+
25+
unsigned long lastMillis = 0;
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
30+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
31+
// You need to set the IP address directly.
32+
//
33+
// MQTT brokers usually use port 8883 for secure connections.
34+
client.begin("broker.shiftr.io", 8883, net);
35+
client.onMessage(messageReceived);
36+
37+
connect();
38+
}
39+
40+
void connect() {
41+
// connection state
42+
boolean connected = false;
43+
44+
Serial.print("connecting to cellular network ...");
45+
46+
// After starting the modem with gsmAccess.begin()
47+
// attach to the GPRS network with the APN, login and password
48+
while (!connected) {
49+
if ((gsmAccess.begin(pin) == GSM_READY) &&
50+
(gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
51+
connected = true;
52+
} else {
53+
Serial.print(".");
54+
delay(1000);
55+
}
56+
}
57+
58+
Serial.print("\nconnecting...");
59+
while (!client.connect("arduino", "try", "try")) {
60+
Serial.print(".");
61+
delay(1000);
62+
}
63+
64+
Serial.println("\nconnected!");
65+
66+
client.subscribe("/hello");
67+
// client.unsubscribe("/hello");
68+
}
69+
70+
void loop() {
71+
client.loop();
72+
73+
if (!client.connected()) {
74+
connect();
75+
}
76+
77+
// publish a message roughly every second.
78+
if (millis() - lastMillis > 1000) {
79+
lastMillis = millis();
80+
client.publish("/hello", "world");
81+
}
82+
}
83+
84+
void messageReceived(String &topic, String &payload) {
85+
Serial.println("incoming: " + topic + " - " + payload);
86+
}

0 commit comments

Comments
 (0)