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
+ }
0 commit comments