Skip to content

Commit 587ac05

Browse files
authored
Merge pull request #1 from facchinm/thing_integration
Integrate ArduinoCloudThing
2 parents 6123cb7 + 11fe613 commit 587ac05

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed

examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ unsigned long getTime() {
1313
return WiFi.getTime();
1414
}
1515

16+
int position;
17+
18+
void onPositionUpdate() {
19+
Serial.print("New position value: ");
20+
Serial.println(position);
21+
}
22+
1623
void setup() {
1724
//Initialize serial and wait for port to open:
1825
Serial.begin(9600);
@@ -57,6 +64,8 @@ void setup() {
5764

5865
Serial.println("Successfully connected to Arduino Cloud :)");
5966

67+
ArduinoCloud.addProperty(position, READ, 10*SECONDS, onPositionUpdate);
68+
6069
CloudSerial.begin(9600);
6170
}
6271

src/ArduinoCloud.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ int ArduinoCloudClass::begin(Client& net)
6161
_mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage);
6262
_mqttClient.begin(server, 8883, *_bearSslClient);
6363

64+
Thing.begin();
65+
6466
_stdoutTopic = "$aws/things/" + _id + "/stdout";
6567
_stdinTopic = "$aws/things/" + _id + "/stdin";
68+
_dataTopic = "$aws/things/" + _id + "/data";
6669

6770
return 1;
6871
}
@@ -74,13 +77,19 @@ int ArduinoCloudClass::connect()
7477
}
7578

7679
_mqttClient.subscribe(_stdinTopic);
80+
_mqttClient.subscribe(_dataTopic);
7781

7882
return 1;
7983
}
8084

8185
void ArduinoCloudClass::poll()
8286
{
8387
_mqttClient.loop();
88+
uint8_t data[1024];
89+
int length = Thing.poll(data, sizeof(data));
90+
if (length > 0) {
91+
writeProperties(data, length);
92+
}
8493
}
8594

8695
void ArduinoCloudClass::onGetTime(unsigned long(*callback)(void))
@@ -93,6 +102,11 @@ int ArduinoCloudClass::connected()
93102
return _mqttClient.connected();
94103
}
95104

105+
int ArduinoCloudClass::writeProperties(const byte data[], int length)
106+
{
107+
return _mqttClient.publish(_dataTopic.c_str(), (const char*)data, length);
108+
}
109+
96110
int ArduinoCloudClass::writeStdout(const byte data[], int length)
97111
{
98112
return _mqttClient.publish(_stdoutTopic.c_str(), (const char*)data, length);
@@ -108,6 +122,9 @@ void ArduinoCloudClass::handleMessage(char topic[], char bytes[], int length)
108122
if (_stdinTopic == topic) {
109123
CloudSerial.appendStdin((uint8_t*)bytes, length);
110124
}
125+
if (_dataTopic == topic) {
126+
Thing.decode((uint8_t*)bytes, length);
127+
}
111128
}
112129

113130
ArduinoCloudClass ArduinoCloud;

src/ArduinoCloudV2.h

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <MQTTClient.h>
55
#include <ArduinoBearSSL.h>
6+
#include <ArduinoCloudThing.h>
7+
#include <ArduinoHttpClient.h>
68

79
#include "CloudSerial.h"
810

@@ -22,9 +24,24 @@ class ArduinoCloudClass {
2224

2325
int connected();
2426

27+
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
28+
29+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, T minDelta = 0, void(*fn)(void) = NULL) {
30+
Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta);
31+
}
32+
33+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, T minDelta = 0) {
34+
Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta);
35+
}
36+
37+
template<typename T> void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, void(*fn)(void) = NULL, long seconds = ON_CHANGE, T minDelta = 0) {
38+
Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta);
39+
}
40+
2541
protected:
2642
friend class CloudSerialClass;
2743
int writeStdout(const byte data[], int length);
44+
int writeProperties(const byte data[], int length);
2845

2946
private:
3047
static void onMessage(MQTTClient *client, char topic[], char bytes[], int length);
@@ -33,11 +50,15 @@ class ArduinoCloudClass {
3350

3451
private:
3552
String _id;
53+
ArduinoCloudThing Thing;
3654
BearSSLClient* _bearSslClient;
55+
HttpClient* _otaClient;
3756
MQTTClient _mqttClient;
3857

3958
String _stdinTopic;
4059
String _stdoutTopic;
60+
String _dataTopic;
61+
String _otaTopic;
4162
};
4263

4364

src/OTAStorage.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _OTA_STORAGE_H_INCLUDED
20+
#define _OTA_STORAGE_H_INCLUDED
21+
22+
class OTAStorage {
23+
public:
24+
virtual int open(int length) = 0;
25+
virtual size_t write(uint8_t* data, size_t size) = 0;
26+
virtual void close() = 0;
27+
virtual void clear() = 0;
28+
virtual void apply() = 0;
29+
30+
virtual long maxSize() {
31+
return ((256 * 1024) - 0x2000);
32+
}
33+
};
34+
35+
#endif

src/SerialFlashStorage.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "SerialFlashStorage.h"
20+
21+
#define UPDATE_FILE "UPDATE.BIN"
22+
23+
int SerialFlashStorageClass::open(int contentLength)
24+
{
25+
if (!SerialFlash.begin(SERIAL_FLASH_CS)) {
26+
return 0;
27+
}
28+
29+
while (!SerialFlash.ready()) {}
30+
31+
if (SerialFlash.exists(UPDATE_FILE)) {
32+
SerialFlash.remove(UPDATE_FILE);
33+
}
34+
35+
if (SerialFlash.create(UPDATE_FILE, contentLength)) {
36+
_file = SerialFlash.open(UPDATE_FILE);
37+
}
38+
39+
if (!_file) {
40+
return 0;
41+
}
42+
43+
return 1;
44+
}
45+
46+
size_t SerialFlashStorageClass::write(uint8_t *data, size_t size)
47+
{
48+
while (!SerialFlash.ready()) {}
49+
int ret = _file.write(data, size);
50+
return ret;
51+
}
52+
53+
void SerialFlashStorageClass::close()
54+
{
55+
_file.close();
56+
}
57+
58+
void SerialFlashStorageClass::clear()
59+
{
60+
SerialFlash.remove(UPDATE_FILE);
61+
}
62+
63+
void SerialFlashStorageClass::apply()
64+
{
65+
// just reset, SDU copies the data to flash
66+
NVIC_SystemReset();
67+
}
68+
69+
SerialFlashStorageClass SerialFlashStorage;

src/SerialFlashStorage.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _SERIALFLASH_STORAGE_H_INCLUDED
20+
#define _SERIALFLASH_STORAGE_H_INCLUDED
21+
22+
#include <SerialFlash.h>
23+
24+
#include "OTAStorage.h"
25+
26+
#define SERIAL_FLASH_BUFFER_SIZE 64
27+
#define SERIAL_FLASH_CS 5
28+
29+
class SerialFlashStorageClass : public OTAStorage {
30+
public:
31+
virtual int open(int length);
32+
virtual size_t write(uint8_t* data, size_t size);
33+
virtual void close();
34+
virtual void clear();
35+
virtual void apply();
36+
37+
private:
38+
SerialFlashFile _file;
39+
};
40+
41+
extern SerialFlashStorageClass SerialFlashStorage;
42+
43+
#endif

0 commit comments

Comments
 (0)