Skip to content

Commit 06a2c6c

Browse files
authored
Merge pull request #284 from pennam/cloudschedule
CloudSchedule
2 parents 9ed5a57 + fda11b5 commit 06a2c6c

16 files changed

+1482
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* MKR1000, MKR WiFi 1010 */
4+
#if defined(BOARD_HAS_WIFI)
5+
#define SECRET_SSID "YOUR_WIFI_NETWORK_NAME"
6+
#define SECRET_PASS "YOUR_WIFI_PASSWORD"
7+
#endif
8+
9+
/* ESP8266 */
10+
#if defined(BOARD_ESP8266)
11+
#define SECRET_DEVICE_KEY "my-device-password"
12+
#endif
13+
14+
/* MKR GSM 1400 */
15+
#if defined(BOARD_HAS_GSM)
16+
#define SECRET_PIN ""
17+
#define SECRET_APN ""
18+
#define SECRET_LOGIN ""
19+
#define SECRET_PASS ""
20+
#endif
21+
22+
/* MKR WAN 1300/1310 */
23+
#if defined(BOARD_HAS_LORA)
24+
#define SECRET_APP_EUI ""
25+
#define SECRET_APP_KEY ""
26+
#endif
27+
28+
/* MKR NB 1500 */
29+
#if defined(BOARD_HAS_NB)
30+
#define SECRET_PIN ""
31+
#define SECRET_APN ""
32+
#define SECRET_LOGIN ""
33+
#define SECRET_PASS ""
34+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
4+
#define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
5+
#define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
6+
7+
void onSwitchButtonChange();
8+
9+
bool switchButton;
10+
CloudSchedule oneShot;
11+
CloudSchedule minute;
12+
CloudSchedule hourly;
13+
CloudSchedule daily;
14+
CloudSchedule weekly;
15+
CloudSchedule monthly;
16+
CloudSchedule yearly;
17+
18+
void initProperties() {
19+
#if defined(BOARD_ESP8266)
20+
ArduinoCloud.setBoardId(BOARD_ID);
21+
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
22+
#endif
23+
ArduinoCloud.setThingId(THING_ID);
24+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
25+
ArduinoCloud.addProperty(switchButton, WRITE, ON_CHANGE);
26+
ArduinoCloud.addProperty(oneShot, READWRITE, ON_CHANGE);
27+
ArduinoCloud.addProperty(minute, READWRITE, ON_CHANGE);
28+
ArduinoCloud.addProperty(hourly, READWRITE, ON_CHANGE);
29+
ArduinoCloud.addProperty(daily, READWRITE, ON_CHANGE);
30+
ArduinoCloud.addProperty(weekly, READWRITE, ON_CHANGE);
31+
ArduinoCloud.addProperty(monthly, READWRITE, ON_CHANGE);
32+
ArduinoCloud.addProperty(yearly, READWRITE, ON_CHANGE);
33+
#elif defined(BOARD_HAS_LORA)
34+
ArduinoCloud.addProperty(switchButton, 1, WRITE, ON_CHANGE;
35+
#endif
36+
}
37+
38+
#if defined(BOARD_HAS_WIFI)
39+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS);
40+
#elif defined(BOARD_HAS_GSM)
41+
GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
42+
#elif defined(BOARD_HAS_LORA)
43+
LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A);
44+
#elif defined(BOARD_HAS_NB)
45+
NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS);
46+
#endif

extras/test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include_directories(include)
1212
include_directories(../../src)
1313
include_directories(../../src/cbor)
1414
include_directories(../../src/property)
15+
include_directories(../../src/utility/time)
1516
include_directories(external/catch/v2.12.1/include)
1617
include_directories(external/fakeit/v2.0.5/include)
1718

@@ -32,6 +33,7 @@ set(TEST_SRCS
3233
src/test_callback.cpp
3334
src/test_CloudColor.cpp
3435
src/test_CloudLocation.cpp
36+
src/test_CloudSchedule.cpp
3537
src/test_decode.cpp
3638
src/test_encode.cpp
3739
src/test_publishEvery.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
#ifndef TEST_ARDUINO_CONNECTION_HANDLER_H_
6+
#define TEST_ARDUINO_CONNECTION_HANDLER_H_
7+
8+
/******************************************************************************
9+
TYPEDEF
10+
******************************************************************************/
11+
12+
typedef void ConnectionHandler;
13+
14+
#endif /* TEST_ARDUINO_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)