Coder Social home page Coder Social logo

arduinocloudthing's Introduction

Arduino Cloud Thing Middleware

Build Status Code Coverage

This library contains the implementation of the class ArduinoCloudThing which allows to add int, bool, float, String properties which can be read from or written to the cloud. To achieve this goal ArduinoCloudThing possesses functions for encoding and decoding CBOR (Concise Binary Object Representation) encoded data which is used to transfer the properties value between a thing and the cloud.

The main focus of the library is on resource usage (never use more data than needed), transport independence and user friendliness.

How-to-Use

  • Instantiate a thing with

ArduinoCloudThing thing

  • Initialize the thing (initialisation adds a status property) via

thing.begin()

  • Now various properties can be registered with the thing which are then readable/writeable by the cloud. For adding a new property call

thing.addProperty(my_property_variable, my_property_name, my_property_permission)

my_property_variable can be an int, a bool, a String or a float. You can use that variable anywhere and its value will be transparently updated to the cloud.

my_property_name is a string which uniquely identifies a property for the system.

my_property_permission can be either Permission::Read, Permission::Write or Permission::ReadWrite.

Additional configuration can be added to the property via composition

.publishEvery(unsigned long seconds) configures a property to report its status (even if unchanged) every x seconds

.publishOnChange()

.onUpdate(functionPointer) configures the property to call functionPointer when the value is changed by the cloud.

.onSync(syncFunctionPointer) configures the property to call syncFunctionPointer when as consequence of a connection/reconnection (so after a period passed offline) the value of the property must be synchronized with the one stored in the cloud. The implementation syncFunctionPointer() should contain some synchronization logic; standard implementations for this functions are:

  • MOST_RECENT_WINS: that compares the cloud timestamp of the last change with the corresponding device timestamp. The property is assigned the value with the higher timestamp. If the cloud value is used the functionPointer is called
  • CLOUD_WINS: the property is assigned the value coming from cloud regardless of timestamps and device value.
  • DEVICE_WINS: the device property value is kept. The cloud property value will be updated at the next update cycle.

A custom synchronization logic may be implemented by setting a custom callback function with the following signature: void (*_sync_callback_func)(ArduinoCloudProperty<T> property); use one of the specific types supported. The property object exposes several methods which will enable the custom logic to select the appropriate value.

A typical property creation could look like that:

void onUpdateCallback() {
  /* Do stuff */
}
...
int       int_property = 0;
int const min_delta    = 6;
...
thing.addProperty(int_property, "test_int_property", Permission::ReadWrite).publishOnChange(min_delta).onUpdate(onUpdateCallback).onSync(onSynchronizationCallback);
...
  • encode Checks all properties if their value has changed and if so populates the buffer with the desired CBOR update message.

int encode(uint8_t * data, size_t const size)

  • decode decodes a CBOR buffer received from the cloud and if the syncMessage parameter is set to false (default), updates writeable properties accordingly and calls the update callback if the value of a property has changed. If the syncMessage parameter is set to true, the value of the property received by the cloud and the relative last change timestamp are passed to a synchronization callback. The synchronization callback could apply the logic to assign a value to the property choosing between the local value and the cloud one. The synchronization logic also decides if the call to the update callback is necessary.

decode(uint8_t const * const payload, size_t const length, bool syncMessage = false)

Unit Tests

The code of ArduinoCloudThing is verified via unit tests which are build and executed by the Travis CI system after every commit. However, one can also build and execute the unit tests manually using the following steps:

cd test && mkdir build && cd build
cmake ..
make -j4
bin/testArduinoCloudThing

arduinocloudthing's People

Contributors

aentinger avatar facchinm avatar ilcato avatar per1234 avatar mirkokurt avatar sid23 avatar cmaglie avatar mattiabertorello avatar sandeepmistry avatar agdl avatar lorenzoromagnoli avatar mastrolinux avatar peteous avatar marcusjoh avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.