Skip to content

Commit 0e1d73b

Browse files
authored
Merge pull request #5 from bcmi-labs/thing_integration
Thing integration
2 parents 767f131 + 646c92a commit 0e1d73b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Diff for: examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ unsigned long getTime() {
1616
return WiFi.getTime();
1717
}
1818

19+
int position;
20+
21+
void onPositionUpdate() {
22+
Serial.print("New position value: ");
23+
Serial.println(position);
24+
}
25+
1926
void setup() {
2027
//Initialize serial and wait for port to open:
2128
Serial.begin(9600);
@@ -73,6 +80,8 @@ void setup() {
7380

7481
Serial.println("Successfully connected to Arduino Cloud :)");
7582

83+
ArduinoCloud.addProperty(position, READ, 10*SECONDS, onPositionUpdate);
84+
7685
CloudSerial.begin(9600);
7786
CloudSerial.print("I'm ready for blinking!\n");
7887
}

Diff for: src/ArduinoCloud.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int ArduinoCloudClass::begin(Client& net)
6262
// Begin function for the MQTTClient
6363
mqttClientBegin(*_bearSslClient);
6464

65+
Thing.begin();
66+
6567
return 1;
6668
}
6769

@@ -71,6 +73,8 @@ void ArduinoCloudClass::mqttClientBegin(Client& net)
7173
// MQTT topics definition
7274
_stdoutTopic = "/a/d/" + _id + "/s/o";
7375
_stdinTopic = "/a/d/" + _id + "/s/i";
76+
_dataTopicIn = "/a/d/" + _id + "/e/i";
77+
_dataTopicOut = "/a/d/" + _id + "/e/o";
7478

7579
// use onMessage as callback for received mqtt messages
7680
_mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage);
@@ -88,6 +92,7 @@ int ArduinoCloudClass::connect()
8892
return 0;
8993
}
9094
_mqttClient.subscribe(_stdinTopic);
95+
_mqttClient.subscribe(_dataTopicIn);
9196

9297
return 1;
9398
}
@@ -139,6 +144,12 @@ void ArduinoCloudClass::poll(int reconnectionMaxRetries, int reconnectionTimeout
139144

140145
// MTTQClient connected!, poll() used to retrieve data from MQTT broker
141146
_mqttClient.loop();
147+
148+
uint8_t data[MQTT_BUFFER_SIZE];
149+
int length = Thing.poll(data, sizeof(data));
150+
if (length > 0) {
151+
writeProperties(data, length);
152+
}
142153
}
143154

144155
void ArduinoCloudClass::reconnect(Client& net)
@@ -158,6 +169,11 @@ int ArduinoCloudClass::connected()
158169
return _mqttClient.connected();
159170
}
160171

172+
int ArduinoCloudClass::writeProperties(const byte data[], int length)
173+
{
174+
return _mqttClient.publish(_dataTopicOut.c_str(), (const char*)data, length);
175+
}
176+
161177
int ArduinoCloudClass::writeStdout(const byte data[], int length)
162178
{
163179
return _mqttClient.publish(_stdoutTopic.c_str(), (const char*)data, length);
@@ -173,6 +189,9 @@ void ArduinoCloudClass::handleMessage(char topic[], char bytes[], int length)
173189
if (_stdinTopic == topic) {
174190
CloudSerial.appendStdin((uint8_t*)bytes, length);
175191
}
192+
if (_dataTopicIn == topic) {
193+
Thing.decode((uint8_t*)bytes, length);
194+
}
176195
}
177196

178197
ArduinoCloudClass ArduinoCloud;

Diff for: src/ArduinoCloudV2.h

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <MQTTClient.h>
55
#include <ArduinoBearSSL.h>
6+
#include <ArduinoCloudThing.h>
67

78
#include "CloudSerial.h"
89

@@ -41,9 +42,24 @@ class ArduinoCloudClass {
4142
// Clean up existing Mqtt connection, create a new one and initialize it
4243
void reconnect(Client& net);
4344

45+
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
46+
47+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, T minDelta = T(0), void(*fn)(void) = NULL) {
48+
Thing.addPropertyReal(property, name, _permission, seconds, fn, minDelta);
49+
}
50+
51+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, T minDelta = T(0)) {
52+
Thing.addPropertyReal(property, name, _permission, seconds, fn, minDelta);
53+
}
54+
55+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, void(*fn)(void) = NULL, long seconds = ON_CHANGE, T minDelta = T(0)) {
56+
Thing.addPropertyReal(property, name, _permission, seconds, fn, minDelta);
57+
}
58+
4459
protected:
4560
friend class CloudSerialClass;
4661
int writeStdout(const byte data[], int length);
62+
int writeProperties(const byte data[], int length);
4763
// Used to initialize MQTTClient
4864
void mqttClientBegin(Client& net);
4965
// Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout)
@@ -54,12 +70,16 @@ class ArduinoCloudClass {
5470
void handleMessage(char topic[], char bytes[], int length);
5571

5672
String _id;
73+
ArduinoCloudThing Thing;
5774
BearSSLClient* _bearSslClient;
5875
MQTTClient _mqttClient;
5976

6077
// Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload
6178
String _stdinTopic;
6279
String _stdoutTopic;
80+
String _dataTopicOut;
81+
String _dataTopicIn;
82+
String _otaTopic;
6383
};
6484

6585
extern ArduinoCloudClass ArduinoCloud;

0 commit comments

Comments
 (0)