diff --git a/examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino b/examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino index 07a7deed3..96590c9a0 100644 --- a/examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino +++ b/examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino @@ -13,6 +13,13 @@ unsigned long getTime() { return WiFi.getTime(); } +int position; + +void onPositionUpdate() { + Serial.print("New position value: "); + Serial.println(position); +} + void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); @@ -57,6 +64,8 @@ void setup() { Serial.println("Successfully connected to Arduino Cloud :)"); + ArduinoCloud.addProperty(position, READ, 10*SECONDS, onPositionUpdate); + CloudSerial.begin(9600); } diff --git a/src/ArduinoCloud.cpp b/src/ArduinoCloud.cpp index 316bb6bd3..9a0b598d2 100644 --- a/src/ArduinoCloud.cpp +++ b/src/ArduinoCloud.cpp @@ -61,8 +61,11 @@ int ArduinoCloudClass::begin(Client& net) _mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage); _mqttClient.begin(server, 8883, *_bearSslClient); + Thing.begin(); + _stdoutTopic = "$aws/things/" + _id + "/stdout"; _stdinTopic = "$aws/things/" + _id + "/stdin"; + _dataTopic = "$aws/things/" + _id + "/data"; return 1; } @@ -74,6 +77,7 @@ int ArduinoCloudClass::connect() } _mqttClient.subscribe(_stdinTopic); + _mqttClient.subscribe(_dataTopic); return 1; } @@ -81,6 +85,11 @@ int ArduinoCloudClass::connect() void ArduinoCloudClass::poll() { _mqttClient.loop(); + uint8_t data[1024]; + int length = Thing.poll(data, sizeof(data)); + if (length > 0) { + writeProperties(data, length); + } } void ArduinoCloudClass::onGetTime(unsigned long(*callback)(void)) @@ -93,6 +102,11 @@ int ArduinoCloudClass::connected() return _mqttClient.connected(); } +int ArduinoCloudClass::writeProperties(const byte data[], int length) +{ + return _mqttClient.publish(_dataTopic.c_str(), (const char*)data, length); +} + int ArduinoCloudClass::writeStdout(const byte data[], int length) { return _mqttClient.publish(_stdoutTopic.c_str(), (const char*)data, length); @@ -108,6 +122,9 @@ void ArduinoCloudClass::handleMessage(char topic[], char bytes[], int length) if (_stdinTopic == topic) { CloudSerial.appendStdin((uint8_t*)bytes, length); } + if (_dataTopic == topic) { + Thing.decode((uint8_t*)bytes, length); + } } ArduinoCloudClass ArduinoCloud; diff --git a/src/ArduinoCloudV2.h b/src/ArduinoCloudV2.h index 6a592565f..5f1f71479 100644 --- a/src/ArduinoCloudV2.h +++ b/src/ArduinoCloudV2.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "CloudSerial.h" @@ -22,9 +24,24 @@ class ArduinoCloudClass { int connected(); + #define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__) + + template void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, T minDelta = 0, void(*fn)(void) = NULL) { + Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta); + } + + template void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, T minDelta = 0) { + Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta); + } + + template void addPropertyReal(T& property, String name, permissionType _permission = READWRITE, void(*fn)(void) = NULL, long seconds = ON_CHANGE, T minDelta = 0) { + Thing.addPropertyReal(property, name).publishEvery(seconds).setPermission(_permission).onUpdate(fn).minimumDelta(&minDelta); + } + protected: friend class CloudSerialClass; int writeStdout(const byte data[], int length); + int writeProperties(const byte data[], int length); private: static void onMessage(MQTTClient *client, char topic[], char bytes[], int length); @@ -33,11 +50,15 @@ class ArduinoCloudClass { private: String _id; + ArduinoCloudThing Thing; BearSSLClient* _bearSslClient; + HttpClient* _otaClient; MQTTClient _mqttClient; String _stdinTopic; String _stdoutTopic; + String _dataTopic; + String _otaTopic; }; diff --git a/src/OTAStorage.h b/src/OTAStorage.h new file mode 100644 index 000000000..0f96e2b3f --- /dev/null +++ b/src/OTAStorage.h @@ -0,0 +1,35 @@ +/* + Copyright (c) 2017 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _OTA_STORAGE_H_INCLUDED +#define _OTA_STORAGE_H_INCLUDED + +class OTAStorage { +public: + virtual int open(int length) = 0; + virtual size_t write(uint8_t* data, size_t size) = 0; + virtual void close() = 0; + virtual void clear() = 0; + virtual void apply() = 0; + + virtual long maxSize() { + return ((256 * 1024) - 0x2000); + } +}; + +#endif diff --git a/src/SerialFlashStorage.cpp b/src/SerialFlashStorage.cpp new file mode 100644 index 000000000..432b7c7b6 --- /dev/null +++ b/src/SerialFlashStorage.cpp @@ -0,0 +1,69 @@ +/* + Copyright (c) 2017 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "SerialFlashStorage.h" + +#define UPDATE_FILE "UPDATE.BIN" + +int SerialFlashStorageClass::open(int contentLength) +{ + if (!SerialFlash.begin(SERIAL_FLASH_CS)) { + return 0; + } + + while (!SerialFlash.ready()) {} + + if (SerialFlash.exists(UPDATE_FILE)) { + SerialFlash.remove(UPDATE_FILE); + } + + if (SerialFlash.create(UPDATE_FILE, contentLength)) { + _file = SerialFlash.open(UPDATE_FILE); + } + + if (!_file) { + return 0; + } + + return 1; +} + +size_t SerialFlashStorageClass::write(uint8_t *data, size_t size) +{ + while (!SerialFlash.ready()) {} + int ret = _file.write(data, size); + return ret; +} + +void SerialFlashStorageClass::close() +{ + _file.close(); +} + +void SerialFlashStorageClass::clear() +{ + SerialFlash.remove(UPDATE_FILE); +} + +void SerialFlashStorageClass::apply() +{ + // just reset, SDU copies the data to flash + NVIC_SystemReset(); +} + +SerialFlashStorageClass SerialFlashStorage; diff --git a/src/SerialFlashStorage.h b/src/SerialFlashStorage.h new file mode 100644 index 000000000..4409969f5 --- /dev/null +++ b/src/SerialFlashStorage.h @@ -0,0 +1,43 @@ +/* + Copyright (c) 2017 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _SERIALFLASH_STORAGE_H_INCLUDED +#define _SERIALFLASH_STORAGE_H_INCLUDED + +#include + +#include "OTAStorage.h" + +#define SERIAL_FLASH_BUFFER_SIZE 64 +#define SERIAL_FLASH_CS 5 + +class SerialFlashStorageClass : public OTAStorage { +public: + virtual int open(int length); + virtual size_t write(uint8_t* data, size_t size); + virtual void close(); + virtual void clear(); + virtual void apply(); + +private: + SerialFlashFile _file; +}; + +extern SerialFlashStorageClass SerialFlashStorage; + +#endif