|
| 1 | +/* |
| 2 | + This sketch demonstrates how to use the cloud schedule variable type. |
| 3 | +
|
| 4 | + This sketch is compatible with the following boards: |
| 5 | + - MKR 1000 |
| 6 | + - MKR WIFI 1010 |
| 7 | + - MKR GSM 1400 |
| 8 | + - MKR NB 1500 |
| 9 | + - MKR WAN 1300/1310 |
| 10 | + - Nano 33 IoT |
| 11 | + - ESP 8266 |
| 12 | +*/ |
| 13 | + |
| 14 | +#include "arduino_secrets.h" |
| 15 | +#include "thingProperties.h" |
| 16 | + |
| 17 | +#if defined(ESP32) |
| 18 | +static int const LED_BUILTIN = 2; |
| 19 | +#endif |
| 20 | + |
| 21 | +void setup() { |
| 22 | + /* Initialize the serial port and wait up to 5 seconds for a connection */ |
| 23 | + Serial.begin(9600); |
| 24 | + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { } |
| 25 | + |
| 26 | + /* Configure LED pin as an output */ |
| 27 | + pinMode(LED_BUILTIN, OUTPUT); |
| 28 | + |
| 29 | + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ |
| 30 | + initProperties(); |
| 31 | + |
| 32 | + /* Initialize Arduino IoT Cloud library */ |
| 33 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection); |
| 34 | + |
| 35 | + setDebugMessageLevel(DBG_INFO); |
| 36 | + ArduinoCloud.printDebugInfo(); |
| 37 | + |
| 38 | + /* Setup one shot schedule example */ |
| 39 | + setupOneShotSchedule(); |
| 40 | + |
| 41 | + /* Setup per minute schedule example */ |
| 42 | + setupMinuteSchedule(); |
| 43 | + |
| 44 | + /* Setup hourly schedule example */ |
| 45 | + setupHourlySchedule(); |
| 46 | + |
| 47 | + /* Setup daily schedule example */ |
| 48 | + setupDailySchedule(); |
| 49 | + |
| 50 | + /* Setup weekly schedule example */ |
| 51 | + setupWeeklySchedule(); |
| 52 | + |
| 53 | + /* Setup monthly schedule example */ |
| 54 | + setupMonthlySchedule(); |
| 55 | + |
| 56 | + /* Setup yearly schedule example */ |
| 57 | + setupYearlySchedule(); |
| 58 | +} |
| 59 | + |
| 60 | + /* Setup a schedule with an active period of 5 minutes that doesn't repeat |
| 61 | + * Starting from 2021 11 01 17:00:00 |
| 62 | + * Until 2021 11 02 17:00:00 |
| 63 | + */ |
| 64 | +void setupOneShotSchedule() { |
| 65 | + |
| 66 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 67 | + ScheduleTimeType until = startingFrom + ( DAYS * 1 ); |
| 68 | + ScheduleTimeType activePeriod = MINUTES * 5; |
| 69 | + |
| 70 | + /* Warning: there is no cross check between until and activePeriod */ |
| 71 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createOneShotScheduleConfiguration(); |
| 72 | + |
| 73 | + oneShot = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 74 | +} |
| 75 | + |
| 76 | + /* Setup a schedule with an active period of 15 seconds that repeats each minute |
| 77 | + * Starting from 2021 11 01 17:00:00 |
| 78 | + * Until 2021 11 02 17:00:00 |
| 79 | + */ |
| 80 | +void setupMinuteSchedule() { |
| 81 | + |
| 82 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 83 | + ScheduleTimeType until = startingFrom + ( DAYS * 1 ); |
| 84 | + ScheduleTimeType activePeriod = SECONDS * 15; |
| 85 | + unsigned int repetitionPeriod = 1; |
| 86 | + |
| 87 | + /* Warning: there is no cross check between repetitionPeriod and activePeriod */ |
| 88 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration(ScheduleUnit::Minutes, repetitionPeriod); |
| 89 | + |
| 90 | + minute = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 91 | +} |
| 92 | + |
| 93 | +/* Setup a schedule with an active period of 20 minutes that repeats each hour |
| 94 | + * Starting from 2021 11 01 17:00:00 |
| 95 | + * Until 2021 11 15 13:00:00 |
| 96 | + */ |
| 97 | +void setupHourlySchedule() { |
| 98 | + |
| 99 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 100 | + ScheduleTimeType until = TimeService::getTimeFromString("2021 Nov 15 13:00:00"); |
| 101 | + ScheduleTimeType activePeriod = MINUTES * 20; |
| 102 | + unsigned int repetitionPeriod = 1; |
| 103 | + |
| 104 | + /* Warning: there is no cross check between repetitionPeriod and activePeriod */ |
| 105 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration(ScheduleUnit::Hours, repetitionPeriod); |
| 106 | + |
| 107 | + hourly = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 108 | +} |
| 109 | + |
| 110 | +/* Setup a schedule with an active period of 2 hours that repeats each day |
| 111 | + * Starting from 2021 11 01 17:00:00 |
| 112 | + * Until 2021 11 15 13:00:00 |
| 113 | + */ |
| 114 | +void setupDailySchedule() { |
| 115 | + |
| 116 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 117 | + ScheduleTimeType until = TimeService::getTimeFromString("2021 Nov 15 13:00:00"); |
| 118 | + ScheduleTimeType activePeriod = HOURS * 2; |
| 119 | + unsigned int repetitionPeriod = 1; |
| 120 | + |
| 121 | + /* Warning: there is no cross check between repetitionPeriod and activePeriod */ |
| 122 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createFixedDeltaScheduleConfiguration(ScheduleUnit::Days, repetitionPeriod); |
| 123 | + |
| 124 | + daily = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 125 | +} |
| 126 | + |
| 127 | +/* Setup a schedule with an active period of 3 minutes with a weekly configuration |
| 128 | + * Starting from 2021 11 01 17:00:00 |
| 129 | + * Until 2021 11 31 17:00:00 |
| 130 | + * Weekly configuration |
| 131 | + * Sunday -> Inactive |
| 132 | + * Monday -> Active |
| 133 | + * Tuesday -> Inactive |
| 134 | + * Wednesday -> Active |
| 135 | + * Thursday -> Inactive |
| 136 | + * Friday -> Active |
| 137 | + * Saturday -> Inactive |
| 138 | + */ |
| 139 | +void setupWeeklySchedule() { |
| 140 | + |
| 141 | + unsigned int startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 142 | + unsigned int until = startingFrom + ( DAYS * 30 ); |
| 143 | + unsigned int executionPeriod = MINUTES * 3; |
| 144 | + |
| 145 | + ScheduleWeeklyMask WeeklyMask = { |
| 146 | + ScheduleState::Inactive, /* Sunday */ |
| 147 | + ScheduleState::Active, /* Monday */ |
| 148 | + ScheduleState::Inactive, /* Tuesday */ |
| 149 | + ScheduleState::Active, /* Wednesday */ |
| 150 | + ScheduleState::Inactive, /* Thursday */ |
| 151 | + ScheduleState::Active, /* Friday */ |
| 152 | + ScheduleState::Inactive, /* Saturday */ |
| 153 | + }; |
| 154 | + |
| 155 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createWeeklyScheduleConfiguration(WeeklyMask); |
| 156 | + |
| 157 | + weekly = Schedule(startingFrom, until, executionPeriod, scheduleConfiguration); |
| 158 | +} |
| 159 | + |
| 160 | +/* Setup a schedule with an active period of 1 day that repeats each third day of the month |
| 161 | + * Starting from 2021 11 01 17:00:00 |
| 162 | + * Until 2022 11 15 13:00:00 |
| 163 | + */ |
| 164 | +void setupMonthlySchedule() { |
| 165 | + |
| 166 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 01 17:00:00"); |
| 167 | + ScheduleTimeType until = TimeService::getTimeFromString("2021 Nov 15 13:00:00"); |
| 168 | + ScheduleTimeType activePeriod = DAYS * 1; |
| 169 | + int dayOfMonth = 3; |
| 170 | + |
| 171 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createMonthlyScheduleConfiguration(dayOfMonth); |
| 172 | + |
| 173 | + monthly = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +/* Setup a schedule with an active period of 2 days that repeats each year on November 6th |
| 178 | + * Starting from 2021 11 06 17:00:00 |
| 179 | + * Until 2041 11 15 13:00:00 |
| 180 | + */ |
| 181 | +void setupYearlySchedule() { |
| 182 | + |
| 183 | + ScheduleTimeType startingFrom = TimeService::getTimeFromString("2021 Nov 06 17:00:00"); |
| 184 | + ScheduleTimeType until = TimeService::getTimeFromString("2041 Nov 06 13:00:00"); |
| 185 | + ScheduleTimeType activePeriod = DAYS * 2; |
| 186 | + int dayOfMonth = 6; |
| 187 | + |
| 188 | + ScheduleConfigurationType scheduleConfiguration = Schedule::createYearlyScheduleConfiguration(ScheduleMonth::Nov, dayOfMonth); |
| 189 | + |
| 190 | + yearly = Schedule(startingFrom, until, activePeriod, scheduleConfiguration); |
| 191 | +} |
| 192 | + |
| 193 | +void loop() { |
| 194 | + ArduinoCloud.update(); |
| 195 | + |
| 196 | + /* Print a message when the oneShot schedule is active */ |
| 197 | + if(oneShot.isActive()) { |
| 198 | + Serial.println("One shot schedule is active"); |
| 199 | + } |
| 200 | + |
| 201 | + /* Print a message when the per minute schedule is active */ |
| 202 | + if(minute.isActive()) { |
| 203 | + Serial.println("Per minute schedule is active"); |
| 204 | + } |
| 205 | + |
| 206 | + /* Print a message when the hourly schedule is active */ |
| 207 | + if(hourly.isActive()) { |
| 208 | + Serial.println("Hourly schedule is active"); |
| 209 | + } |
| 210 | + |
| 211 | + /* Print a message when the daily schedule is active */ |
| 212 | + if(daily.isActive()) { |
| 213 | + Serial.println("Daily schedule is active"); |
| 214 | + } |
| 215 | + |
| 216 | + /* Activate LED when the weekly schedule is active */ |
| 217 | + digitalWrite(LED_BUILTIN, weekly.isActive()); |
| 218 | + |
| 219 | + /* Print a message when the monthly schedule is active */ |
| 220 | + if(monthly.isActive()) { |
| 221 | + Serial.println("Monthly schedule is active"); |
| 222 | + } |
| 223 | + |
| 224 | + /* Print a message when the yearly schedule is active */ |
| 225 | + if(yearly.isActive()) { |
| 226 | + Serial.println("Yearly schedule is active"); |
| 227 | + } |
| 228 | + |
| 229 | +} |
| 230 | + |
0 commit comments