Skip to content

Integrate ArduinoCloudThing #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 6, 2018
9 changes: 9 additions & 0 deletions examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -57,6 +64,8 @@ void setup() {

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

ArduinoCloud.addProperty(position, READ, 10*SECONDS, onPositionUpdate);

CloudSerial.begin(9600);
}

Expand Down
17 changes: 17 additions & 0 deletions src/ArduinoCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -74,13 +77,19 @@ int ArduinoCloudClass::connect()
}

_mqttClient.subscribe(_stdinTopic);
_mqttClient.subscribe(_dataTopic);

return 1;
}

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))
Expand All @@ -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);
Expand All @@ -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;
21 changes: 21 additions & 0 deletions src/ArduinoCloudV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <MQTTClient.h>
#include <ArduinoBearSSL.h>
#include <ArduinoCloudThing.h>
#include <ArduinoHttpClient.h>

#include "CloudSerial.h"

Expand All @@ -22,9 +24,24 @@ class ArduinoCloudClass {

int connected();

#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)

template<typename T> 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<typename T> 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<typename T> 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);
Expand All @@ -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;
};


Expand Down
35 changes: 35 additions & 0 deletions src/OTAStorage.h
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions src/SerialFlashStorage.cpp
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 43 additions & 0 deletions src/SerialFlashStorage.h
Original file line number Diff line number Diff line change
@@ -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 <SerialFlash.h>

#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