Skip to content

Commit 65297c7

Browse files
author
Mattia Bertorello
authored
Merge pull request #64 from arduino-libraries/multi-value-properties
Multi value properties management
2 parents 12919cb + 591a02a commit 65297c7

File tree

6 files changed

+146
-28
lines changed

6 files changed

+146
-28
lines changed

Diff for: examples/ArduinoIoTCloud_Travis_CI/thingProperties.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ void initProperties() {
9494
ArduinoCloud.addProperty(float_property_3, Permission::ReadWrite).publishOnChange(MIN_DELTA_FLOAT_PROPERTY);
9595
ArduinoCloud.addProperty(float_property_4, Permission::ReadWrite).publishOnChange(MIN_DELTA_FLOAT_PROPERTY).onUpdate(onFloatPropertyChange);
9696

97-
ArduinoCloud.addProperty(str_property_3, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, "" /* 'minDelta' */, MOST_RECENT_WINS);
98-
ArduinoCloud.addProperty(str_property_4, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, "" /* 'minDelta' */, CLOUD_WINS);
99-
ArduinoCloud.addProperty(str_property_5, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, "" /* 'minDelta' */, DEVICE_WINS);
97+
ArduinoCloud.addProperty(str_property_3, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, 0.0 /* 'minDelta' */, MOST_RECENT_WINS);
98+
ArduinoCloud.addProperty(str_property_4, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, 0.0 /* 'minDelta' */, CLOUD_WINS);
99+
ArduinoCloud.addProperty(str_property_5, READWRITE, 1 * SECONDS, 0 /* onStringPropertyChange */, 0.0 /* 'minDelta' */, DEVICE_WINS);
100100

101101
ArduinoCloud.addProperty(str_property_6, Permission::ReadWrite).publishEvery(1 * SECONDS).onSync(MOST_RECENT_WINS);
102102
ArduinoCloud.addProperty(str_property_7, Permission::ReadWrite).publishEvery(1 * SECONDS).onSync(CLOUD_WINS);

Diff for: examples/MultiValue_example/MultiValue_example.ino

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "arduino_secrets.h"
2+
/*
3+
Sketch generated by the Arduino IoT Cloud Thing "Test_MultiValue"
4+
https://create-dev.arduino.cc/cloud/things/06012290-ec85-4f5c-aa00-81c0525efa0c
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 Switch;
11+
12+
Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
13+
which are called when their values are changed from the Dashboard.
14+
These functions are generated with the Thing and added at the end of this sketch.
15+
*/
16+
17+
#include "thingProperties.h"
18+
19+
void setup() {
20+
// Initialize serial and wait for port to open:
21+
Serial.begin(9600);
22+
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
23+
delay(1500);
24+
25+
// Defined in thingProperties.h
26+
initProperties();
27+
28+
// Connect to Arduino IoT Cloud
29+
ArduinoCloud.begin(ArduinoIoTPreferredConnection, "mqtts-sa.iot.oniudra.cc");
30+
31+
/*
32+
The following function allows you to obtain more information
33+
related to the state of network and IoT Cloud connection and errors
34+
the higher number the more granular information you’ll get.
35+
The default is 0 (only errors).
36+
Maximum is 4
37+
*/
38+
setDebugMessageLevel(2);
39+
ArduinoCloud.printDebugInfo();
40+
}
41+
42+
float latMov = 45.5058224, lonMov = 9.1628673;
43+
float latArd = 45.0502078, lonArd = 7.6674765;
44+
45+
float hueRed = 0.0, satRed = 100.0, briRed = 100.0;
46+
float hueGreen = 80.0, satGreen = 100.0, briGreen = 100.0;
47+
48+
void loop() {
49+
ArduinoCloud.update();
50+
// Your code here
51+
52+
Switch = !Switch;
53+
if (Switch) {
54+
Loc = { .lat = latMov, .lon = lonMov };
55+
Color = { .hue = hueRed, .sat = satRed, .bri = briRed };
56+
} else {
57+
Loc = { .lat = latArd, .lon = lonArd };
58+
Color = { .hue = hueGreen, .sat = satGreen, .bri = briGreen };
59+
}
60+
delay(5000);
61+
}
62+
63+
64+
void onSwitchChange() {
65+
// Do something
66+
digitalWrite(LED_BUILTIN, Switch);
67+
}
68+
69+
void onColorChange() {
70+
// Do something
71+
Serial.print("Hue = ");
72+
Serial.println(Color.getValue().hue);
73+
Serial.print("Sat = ");
74+
Serial.println(Color.getValue().sat);
75+
Serial.print("Bri = ");
76+
Serial.println(Color.getValue().bri);
77+
}

Diff for: examples/MultiValue_example/arduino_secrets.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

Diff for: examples/MultiValue_example/thingProperties.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <WiFiConnectionManager.h>
3+
4+
// Set the Thing Id value
5+
const char THING_ID[] = "";
6+
7+
const char SSID[] = SECRET_SSID; // Network SSID (name)
8+
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
9+
10+
void onSwitchChange();
11+
void onColorChange();
12+
13+
bool Switch;
14+
CloudLocation Loc;
15+
CloudColor Color;
16+
17+
void initProperties() {
18+
ArduinoCloud.setThingId(THING_ID);
19+
ArduinoCloud.addProperty(Switch, READWRITE, ON_CHANGE, onSwitchChange);
20+
ArduinoCloud.addProperty(Loc, READ, ON_CHANGE, NULL);
21+
ArduinoCloud.addProperty(Color, READWRITE, ON_CHANGE, onColorChange);
22+
}
23+
24+
ConnectionManager *ArduinoIoTPreferredConnection = new WiFiConnectionManager(SSID, PASS);

Diff for: src/ArduinoIoTCloud.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ static unsigned long getTime() {
5151
return time;
5252
}
5353

54-
static unsigned long getTimestamp() {
55-
#ifdef ARDUINO_ARCH_SAMD
56-
return rtc.getEpoch();
57-
#else
58-
#warning "No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps ."
59-
return 0;
60-
#endif
61-
}
62-
6354
ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
6455
_thing_id(""),
6556
_bearSslClient(NULL),
@@ -226,11 +217,8 @@ void ArduinoIoTCloudClass::update(CallbackFunc onSyncCompleteCallback) {
226217
}
227218

228219
void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const reconnectionTimeoutMs, CallbackFunc onSyncCompleteCallback) {
229-
unsigned long const timestamp = getTimestamp();
230-
//check if a property is changed
231-
if (timestamp != 0) {
232-
Thing.updateTimestampOnChangedProperties(timestamp);
233-
}
220+
// Check if a primitive property wrapper is locally changed
221+
Thing.updateTimestampOnLocallyChangedProperties();
234222

235223
connectionCheck();
236224
if (iotStatus != ArduinoIoTConnectionStatus::CONNECTED) {

Diff for: src/ArduinoIoTCloud.h

+38-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#include <ArduinoIoTCloudBearSSL.h>
2323
#include <ArduinoCloudThing.h>
2424
#include "ConnectionManager.h"
25+
#include "types/CloudWrapperBool.h"
26+
#include "types/CloudWrapperFloat.h"
27+
#include "types/CloudWrapperInt.h"
28+
#include "types/CloudWrapperString.h"
29+
2530

2631
#include "CloudSerial.h"
2732

@@ -113,10 +118,7 @@ class ArduinoIoTCloudClass {
113118

114119
static unsigned long const DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS = 500; /* Data rate throttled to 2 Hz */
115120

116-
117-
118-
template<typename T, typename N = T>
119-
void addPropertyReal(T & property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, N minDelta = N(0), void(*synFn)(ArduinoCloudProperty<T> property) = CLOUD_WINS) {
121+
void addPropertyReal(ArduinoCloudProperty& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty& property) = CLOUD_WINS) {
120122
Permission permission = Permission::ReadWrite;
121123
if (permission_type == READ) {
122124
permission = Permission::Read;
@@ -127,15 +129,42 @@ class ArduinoIoTCloudClass {
127129
}
128130

129131
if (seconds == ON_CHANGE) {
130-
Thing.addPropertyReal(property, name, permission).publishOnChange((T)minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
132+
Thing.addPropertyReal(property, name, permission).publishOnChange(minDelta, DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
131133
} else {
132134
Thing.addPropertyReal(property, name, permission).publishEvery(seconds).onUpdate(fn).onSync(synFn);
133135
}
134136
}
135-
136-
template <typename T>
137-
ArduinoCloudProperty<T> addPropertyReal(T & property, String const & name, Permission const permission) {
138-
return Thing.addPropertyReal(property, name, permission);
137+
void addPropertyReal(bool& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) {
138+
ArduinoCloudProperty *p = new CloudWrapperBool(property);
139+
addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn);
140+
}
141+
ArduinoCloudProperty& addPropertyReal(bool& property, String name, Permission const permission) {
142+
ArduinoCloudProperty *p = new CloudWrapperBool(property);
143+
return Thing.addPropertyReal(*p, name, permission);
144+
}
145+
void addPropertyReal(float& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) {
146+
ArduinoCloudProperty *p = new CloudWrapperFloat(property);
147+
addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn);
148+
}
149+
ArduinoCloudProperty& addPropertyReal(float& property, String name, Permission const permission) {
150+
ArduinoCloudProperty *p = new CloudWrapperFloat(property);
151+
return Thing.addPropertyReal(*p, name, permission);
152+
}
153+
void addPropertyReal(int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) {
154+
ArduinoCloudProperty *p = new CloudWrapperInt(property);
155+
addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn);
156+
}
157+
ArduinoCloudProperty& addPropertyReal(int& property, String name, Permission const permission) {
158+
ArduinoCloudProperty *p = new CloudWrapperInt(property);
159+
return Thing.addPropertyReal(*p, name, permission);
160+
}
161+
void addPropertyReal(String& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(ArduinoCloudProperty & property) = CLOUD_WINS) {
162+
ArduinoCloudProperty *p = new CloudWrapperString(property);
163+
addPropertyReal(*p, name, permission_type, seconds, fn, minDelta, synFn);
164+
}
165+
ArduinoCloudProperty& addPropertyReal(String& property, String name, Permission const permission) {
166+
ArduinoCloudProperty *p = new CloudWrapperString(property);
167+
return Thing.addPropertyReal(*p, name, permission);
139168
}
140169

141170
void connectionCheck();
@@ -146,7 +175,6 @@ class ArduinoIoTCloudClass {
146175
return _brokerPort;
147176
}
148177
void printDebugInfo();
149-
150178
void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
151179

152180
protected:
@@ -201,7 +229,6 @@ class ArduinoIoTCloudClass {
201229
_on_disconnect_event_callback;
202230

203231
static void execCloudEventCallback(OnCloudEventCallback & callback, void * callback_arg);
204-
205232
};
206233

207234
extern ArduinoIoTCloudClass ArduinoCloud;

0 commit comments

Comments
 (0)