|
| 1 | +#include "arduino_secrets.h" |
| 2 | +/* |
| 3 | + Sketch generated by the Arduino IoT Cloud Thing "test_getting_started_18_01" |
| 4 | + https://create.arduino.cc/cloud/things/52e306ef-a7b7-47e5-aacd-9046e71a70b6 |
| 5 | +
|
| 6 | + Arduino IoT Cloud Properties description |
| 7 | +
|
| 8 | + The following variables are automatically generated and updated when changes are made to the Thing properties |
| 9 | +
|
| 10 | + bool ledSwitch; |
| 11 | + int intValue; |
| 12 | +
|
| 13 | + Properties which are marked as READ/WRITE in the Cloud Thing will also have functions |
| 14 | + which are called when their values are changed from the Dashboard. |
| 15 | + These functions are generated with the Thing and added at the end of this sketch. |
| 16 | +*/ |
| 17 | + |
| 18 | +// Your THING_ID |
| 19 | +/* THING_ID should be part of cloudProperties.h */ |
| 20 | +#define THING_ID "52e306ef-a7b7-47e5-aacd-9046e71a70b6" |
| 21 | + |
| 22 | +// the following include needs data from the Secret tab to define the arrays above |
| 23 | +#include "cloudProperties.h" |
| 24 | + |
| 25 | +/* |
| 26 | + The following data fields are filled by the Secret tab. |
| 27 | + Do not modify them here |
| 28 | +*/ |
| 29 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 30 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 31 | + |
| 32 | +#define UPDATE_CYCLE_TIME 1000 |
| 33 | +unsigned long lastTickTime; |
| 34 | +unsigned int cyclesCount = 0; |
| 35 | + |
| 36 | +void setup() { |
| 37 | + // Initialize serial and wait for port to open: |
| 38 | + Serial.begin(9600); |
| 39 | + // wait up to 5 seconds for user to open Serial port |
| 40 | + unsigned long serialBeginTime = millis(); |
| 41 | + while (!Serial && (millis() - serialBeginTime > 5000)); |
| 42 | + |
| 43 | + Serial.println("Starting Arduino IoT Cloud on MKR"); |
| 44 | + |
| 45 | + // Defined in cloudProperties.h |
| 46 | + initProperties(); |
| 47 | + // Defined in ArduinoCloudSettings.h |
| 48 | + initConnection(); |
| 49 | + |
| 50 | + // set last network check to now, since it is connected |
| 51 | + //lastNetworkCheck = millis(); |
| 52 | + //lastTickTime = millis(); |
| 53 | + |
| 54 | + ledSwitch = true; |
| 55 | + ledSwitch = false; |
| 56 | + intValue = 0; |
| 57 | +} |
| 58 | + |
| 59 | +void loop() { |
| 60 | + unsigned long msNow = millis(); |
| 61 | + ArduinoCloud.update(); |
| 62 | + if(msNow - lastTickTime > UPDATE_CYCLE_TIME){ |
| 63 | + intValue++; |
| 64 | + onIntValueChange(); |
| 65 | + Serial.println(intValue); |
| 66 | + |
| 67 | + lastTickTime = msNow; |
| 68 | + } |
| 69 | + |
| 70 | + // Check if device is connected to the network, if it is do nothing, otherwise reconnect it. Leave this at the end of loop(). |
| 71 | + checkNetworkConnection(); |
| 72 | +} |
| 73 | + |
| 74 | +ConnectionManager *conn = new GSMConnectionManager("", "", "", ""); |
| 75 | + |
| 76 | +void initConnection() { |
| 77 | + ArduinoIoTPreferredConnection->init(); |
| 78 | + |
| 79 | + // begin of the ArduinoIoTCloud |
| 80 | + if (!ArduinoCloud.begin(conn)) { |
| 81 | + Serial.println("Starting Arduino Cloud failed!"); |
| 82 | + while (true); |
| 83 | + } |
| 84 | + |
| 85 | + int attempts = 0; |
| 86 | + Serial.println("Attempting to connect to Arduino Cloud"); |
| 87 | + while (!ArduinoCloud.connect() && attempts++ < ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) { |
| 88 | + Serial.print("."); |
| 89 | + delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT); |
| 90 | + } |
| 91 | + |
| 92 | + if (attempts >= ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) { |
| 93 | + Serial.println("\nFailed to connect to Arduino Cloud!"); |
| 94 | + while (1); |
| 95 | + } |
| 96 | + |
| 97 | + Serial.println("\nSuccessfully connected to Arduino Cloud :)"); |
| 98 | + |
| 99 | + CloudSerial.begin(9600); |
| 100 | +} |
| 101 | + |
| 102 | +unsigned long lastNetworkCheck = millis(); |
| 103 | + |
| 104 | +void checkNetworkConnection() { |
| 105 | + ArduinoIoTPreferredConnection->check(); |
| 106 | + |
| 107 | + Serial.println("..Reconnected to the Nework!"); |
| 108 | + |
| 109 | + // Call the reconnect method to clean up the ArduinoCloud connection |
| 110 | + while (!ArduinoCloud.reconnect(ArduinoIoTPreferredConnection->getClient())) { |
| 111 | + delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT); |
| 112 | + } |
| 113 | + |
| 114 | + Serial.println("..Reconnected to the Cloud!"); |
| 115 | + |
| 116 | + delay(500); |
| 117 | + // update cheking time |
| 118 | + lastNetworkCheck = millis(); |
| 119 | +} |
| 120 | + |
| 121 | +void onLedSwitchChange() { |
| 122 | + Serial.print("LED set to "); |
| 123 | + Serial.println(ledSwitch); |
| 124 | + digitalWrite(LED_BUILTIN, ledSwitch); |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +void onIntValueChange() { |
| 129 | + Serial.print("integer set to "); |
| 130 | + Serial.println(intValue); |
| 131 | +} |
0 commit comments