Skip to content

Commit 34f35cf

Browse files
authored
Add new cloud blink examples (#55)
1 parent 6d8de5f commit 34f35cf

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ install:
5454
script:
5555
- buildExampleSketch ArduinoIoTCloud_LED_switch
5656
- buildExampleSketch ArduinoIoTCloud_Travis_CI
57+
- |
58+
if [ "$BOARD" == "arduino:samd:mkr1000" ] || [ "$BOARD" == "arduino:samd:mkrwifi1010" ];
59+
then buildExampleSketch WiFi_Cloud_Blink;
60+
fi
61+
- |
62+
if [ "$BOARD" == "arduino:samd:mkrgsm1400" ];
63+
then buildExampleSketch GSM_Cloud_Blink;
64+
fi
5765
- buildExampleUtilitySketch Provisioning
5866
notifications:
5967
webhooks:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <ConnectionManager.h>
3+
#include <GSMConnectionManager.h>
4+
5+
#ifndef SECRET_PIN
6+
#define SECRET_PIN ""
7+
#warning "You need to define SECRET_PIN in tab/arduino_secrets.h"
8+
#endif
9+
10+
#ifndef SECRET_APN
11+
#define SECRET_APN ""
12+
#warning "You need to define SECRET_PIN in tab/arduino_secrets.h"
13+
#endif
14+
15+
#ifndef SECRET_USER_NAME
16+
#define SECRET_USER_NAME ""
17+
#warning "You need to define SECRET_USER_NAME in tab/arduino_secrets.h"
18+
#endif
19+
20+
#ifndef SECRET_PASSWORD
21+
#define SECRET_PASSWORD ""
22+
#warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
23+
#endif
24+
25+
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
26+
// handles connection to the network
27+
ConnectionManager * ArduinoIoTPreferredConnection = new GSMConnectionManager(SECRET_PIN, SECRET_APN, SECRET_USER_NAME, SECRET_PASSWORD);
28+
29+
void setup() {
30+
setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4]
31+
Serial.begin(9600);
32+
while (!Serial); // waits for the serial to become available
33+
ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud
34+
pinMode(LED_BUILTIN, OUTPUT);
35+
}
36+
37+
void loop() {
38+
ArduinoCloud.update();
39+
// check if there is something waiting to be read
40+
if (ArduinoCloud.connected()) {
41+
if (CloudSerial.available()) {
42+
char character = CloudSerial.read();
43+
cloudSerialBuffer += character;
44+
// if a \n character has been received, there should be a complete command inside cloudSerialBuffer
45+
if (character == '\n') {
46+
handleString();
47+
}
48+
} else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
49+
handleString();
50+
}
51+
// Just to be able to simulate the board responses through the serial monitor
52+
if (Serial.available()) {
53+
CloudSerial.write(Serial.read());
54+
}
55+
}
56+
}
57+
void handleString() {
58+
// Don't proceed if the string is empty
59+
if (cloudSerialBuffer.equals("")) {
60+
return;
61+
}
62+
// Remove leading and trailing whitespaces
63+
cloudSerialBuffer.trim();
64+
// Make it uppercase;
65+
cloudSerialBuffer.toUpperCase();
66+
if (cloudSerialBuffer.equals("ON")) {
67+
digitalWrite(LED_BUILTIN, HIGH);
68+
} else if (cloudSerialBuffer.equals("OFF")) {
69+
digitalWrite(LED_BUILTIN, LOW);
70+
}
71+
sendString(cloudSerialBuffer);
72+
// Reset cloudSerialBuffer
73+
cloudSerialBuffer = "";
74+
}
75+
// sendString sends a string to the Arduino Cloud.
76+
void sendString(String stringToSend) {
77+
// send the characters one at a time
78+
char lastSentChar = 0;
79+
for (int i = 0; i < stringToSend.length(); i++) {
80+
lastSentChar = stringToSend.charAt(i);
81+
CloudSerial.write(lastSentChar);
82+
}
83+
// if the last sent character wasn't a '\n' add it
84+
if (lastSentChar != '\n') {
85+
CloudSerial.write('\n');
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <ConnectionManager.h>
3+
#include <WiFiConnectionManager.h>
4+
5+
#ifndef SECRET_WIFI_NAME
6+
#define SECRET_WIFI_NAME ""
7+
#warning "You need to define SECRET_WIFI_NAME in tab/arduino_secrets.h"
8+
#endif
9+
10+
#ifndef SECRET_PASSWORD
11+
#define SECRET_PASSWORD ""
12+
#warning "You need to define SECRET_PASSWORD in tab/arduino_secrets.h"
13+
#endif
14+
15+
String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters
16+
// handles connection to the network
17+
ConnectionManager * ArduinoIoTPreferredConnection = new WiFiConnectionManager(SECRET_WIFI_NAME, SECRET_PASSWORD);
18+
19+
void setup() {
20+
setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4]
21+
Serial.begin(9600);
22+
while (!Serial); // waits for the serial to become available
23+
ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud
24+
pinMode(LED_BUILTIN, OUTPUT);
25+
}
26+
27+
void loop() {
28+
ArduinoCloud.update();
29+
// check if there is something waiting to be read
30+
if (ArduinoCloud.connected()) {
31+
if (CloudSerial.available()) {
32+
char character = CloudSerial.read();
33+
cloudSerialBuffer += character;
34+
// if a \n character has been received, there should be a complete command inside cloudSerialBuffer
35+
if (character == '\n') {
36+
handleString();
37+
}
38+
} else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check.
39+
handleString();
40+
}
41+
// Just to be able to simulate the board responses through the serial monitor
42+
if (Serial.available()) {
43+
CloudSerial.write(Serial.read());
44+
}
45+
}
46+
}
47+
void handleString() {
48+
// Don't proceed if the string is empty
49+
if (cloudSerialBuffer.equals("")) {
50+
return;
51+
}
52+
// Remove leading and trailing whitespaces
53+
cloudSerialBuffer.trim();
54+
// Make it uppercase;
55+
cloudSerialBuffer.toUpperCase();
56+
if (cloudSerialBuffer.equals("ON")) {
57+
digitalWrite(LED_BUILTIN, HIGH);
58+
} else if (cloudSerialBuffer.equals("OFF")) {
59+
digitalWrite(LED_BUILTIN, LOW);
60+
}
61+
sendString(cloudSerialBuffer);
62+
// Reset cloudSerialBuffer
63+
cloudSerialBuffer = "";
64+
}
65+
// sendString sends a string to the Arduino Cloud.
66+
void sendString(String stringToSend) {
67+
// send the characters one at a time
68+
char lastSentChar = 0;
69+
for (int i = 0; i < stringToSend.length(); i++) {
70+
lastSentChar = stringToSend.charAt(i);
71+
CloudSerial.write(lastSentChar);
72+
}
73+
// if the last sent character wasn't a '\n' add it
74+
if (lastSentChar != '\n') {
75+
CloudSerial.write('\n');
76+
}
77+
}

0 commit comments

Comments
 (0)