|
| 1 | +/* This sketch is used during the getting started tutorial when |
| 2 | + initialising a Arduino cloud-enabled board with the Arduino |
| 3 | + cloud for the first time. |
| 4 | +
|
| 5 | + This sketch is compatible with: |
| 6 | + - MKR NB 1500 |
| 7 | +*/ |
| 8 | + |
| 9 | +#include <ArduinoIoTCloud.h> |
| 10 | +#include <Arduino_ConnectionHandler.h> |
| 11 | + |
| 12 | +#include "arduino_secrets.h" |
| 13 | + |
| 14 | +String cloudSerialBuffer = ""; // the string used to compose network messages from the received characters |
| 15 | +// handles connection to the network |
| 16 | +NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_PIN, SECRET_OPTIONAL_APN, SECRET_OPTIONAL_USER_NAME, SECRET_OPTIONAL_PASSWORD); |
| 17 | + |
| 18 | +void setup() { |
| 19 | + setDebugMessageLevel(3); // used to set a level of granularity in information output [0...4] |
| 20 | + Serial.begin(9600); |
| 21 | + while (!Serial); // waits for the serial to become available |
| 22 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection); // initialize a connection to the Arduino IoT Cloud |
| 23 | + pinMode(LED_BUILTIN, OUTPUT); |
| 24 | +} |
| 25 | + |
| 26 | +void loop() { |
| 27 | + ArduinoCloud.update(); |
| 28 | + // check if there is something waiting to be read |
| 29 | + if (ArduinoCloud.connected()) { |
| 30 | + if (CloudSerial.available()) { |
| 31 | + char character = CloudSerial.read(); |
| 32 | + cloudSerialBuffer += character; |
| 33 | + // if a \n character has been received, there should be a complete command inside cloudSerialBuffer |
| 34 | + if (character == '\n') { |
| 35 | + handleString(); |
| 36 | + } |
| 37 | + } else { // if there is nothing to read, it could be that the last command didn't end with a '\n'. Check. |
| 38 | + handleString(); |
| 39 | + } |
| 40 | + // Just to be able to simulate the board responses through the serial monitor |
| 41 | + if (Serial.available()) { |
| 42 | + CloudSerial.write(Serial.read()); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +void handleString() { |
| 47 | + // Don't proceed if the string is empty |
| 48 | + if (cloudSerialBuffer.equals("")) { |
| 49 | + return; |
| 50 | + } |
| 51 | + // Remove leading and trailing whitespaces |
| 52 | + cloudSerialBuffer.trim(); |
| 53 | + // Make it uppercase; |
| 54 | + cloudSerialBuffer.toUpperCase(); |
| 55 | + if (cloudSerialBuffer.equals("ON")) { |
| 56 | + digitalWrite(LED_BUILTIN, HIGH); |
| 57 | + } else if (cloudSerialBuffer.equals("OFF")) { |
| 58 | + digitalWrite(LED_BUILTIN, LOW); |
| 59 | + } |
| 60 | + sendString(cloudSerialBuffer); |
| 61 | + // Reset cloudSerialBuffer |
| 62 | + cloudSerialBuffer = ""; |
| 63 | +} |
| 64 | +// sendString sends a string to the Arduino Cloud. |
| 65 | +void sendString(String stringToSend) { |
| 66 | + // send the characters one at a time |
| 67 | + char lastSentChar = 0; |
| 68 | + for (unsigned int i = 0; i < stringToSend.length(); i++) { |
| 69 | + lastSentChar = stringToSend.charAt(i); |
| 70 | + CloudSerial.write(lastSentChar); |
| 71 | + } |
| 72 | + // if the last sent character wasn't a '\n' add it |
| 73 | + if (lastSentChar != '\n') { |
| 74 | + CloudSerial.write('\n'); |
| 75 | + } |
| 76 | +} |
0 commit comments