Coder Social home page Coder Social logo

datavenueliveobjects / liveobjects_sdk_for_arduino Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 3.0 20.45 MB

Library and code samples for Live Objects on Arduino & compatible boards based on SAMD21 & ESP8266

Home Page: https://liveobjects.orange-business.com

License: MIT License

C++ 87.99% C 12.01%
arduino iot orange liveobjects mqtt sms cellular lte-m gsm mkr1000

liveobjects_sdk_for_arduino's Introduction

Prototype with Orange using Live Objects and Arduino MKR Boards

Discover Orange Live Objects using dedicated SDK for Arduino MKR family boards.

This code wraps all the functions necessary to make your object work with Live Objects.

You can declare parameters, which you can later update OTA from Live objects. You can also create commands to trigger actions remotely.

The code will manage the LTE-M, GSM and WiFi connection (depending on currently used board), as well MQTT(S) and SMS exchanges with Live objects under the hood to keep your parameters up to date or execute the commands received without you having to take care of them (apart from writing the code of these commands, of course).

Compatibility

Board MQTT MQTTS SMS
Arduino MKR1000 WIFI OK - -
Arduino MKR 1010 WiFi OK OK -
Arduino MKR 1400 GSM OK OK OK
Arduino MKR 1500 NB OK OK OK
Arduino MKR VIDOR 4000 OK OK -
Arduino Nano 33 IoT OK OK -
ESP8266 Boards OK OPT1) -
ESP32 Boards OK OK -
Adafruit Feather M0 WiFi OK - -
Adafruit Feather 32u4 OK - OK

Prerequisites/dependecies

This code needs external libraries to run, that you can install using the built-in Library Manager of the Arduino IDE.

Libraries provided by Arduino

  • MKRNB in order to handle the LTE-M module on Arduino MKR NB 1500
  • MKRGSM in order to handle the GSM module on Arduino MKR GSM 1400
  • WiFiNINA in order to handle WiFi module on Arduino MKRWIFI 1010 and Arduino Nano 33 IoT
  • WiFi101 in order to handle WiFi module on Arduino MKR 1000
  • ArduinoMqttClient that implements an MQTT client for Arduino

Library for ESP8266 and ESP32 boards

  • PubSubClient library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT

Library developed by Benoît Blanchon (mandatory for both Arduino, ESP and Adafruit boards)

  • ArduinoJson, a powerful library used to parse, store and handle JSON easily

SAMD21 Arduino core

  • You also need to install the Arduino core for Atmel SAMD21 processor, used on the boards of the MKR family. Open the Boards Manager and install the package called "Arduino SAMD Boards (32-bit ARM Cortex-M0+)".

How to use

  1. Log in to Live Objects or request a trial account (up to 10 devices for 1 year) if you don't have one.

  2. Create an API key for your device. Give it a name, select the Device access role and validate. Copy the key.

  3. Clone or download the directory from Github.

  4. In the 'src/arduino_secrets.h' file :

    • Paste it as initialization value for the SECRET_LIVEOBJECTS_API_KEY variable in the 'arduino_secrets.h' file -keep the double quotes!

    • In case of feather 32u4 you have to change type of this variable to char* from String.

    • Fill in the connection(WIFI or GSM) credentials if needed (pin code, APN information, etc). In case of GSM connection, most of the time, APN will set up automatically. Your SIM card may have a default pin code (like "0000"), unless you deactivated it using the Pin management sketch, provided with the MKRNB library.

    • In case of Feather 32u4 you have to change APN in library file LiveObjectsFona.cpp

    • Line ~179 - m_Fona.setGPRSNetworkSettings(F("APN"), F(""), F(""));

  5. Import library into the Arduino IDE, to do this select: Sketch-> Include Library-> Add .ZIP Library and select folder which you cloned in the previous step(actually it doesn't need to be .ZIP-ed to be imported). After successful import you should see example sketches in File->Examples->LiveObjectsSDK

  6. Modules MKR 1010 WiFi, MKR VIDOR 4000, Nano 33 IoT should work "out of the box" using MQTTS. If not, you need to upgrade theirs firmwares and certificates using embedded updater in Arduino IDE: Tools -> WiFi101/WiFiNINA Firmware Updater.
    Due to bug, MKR VIDOR4000 needs WiFiNINA library at the most v.1.8.5.

1) Optionally for ESP8266, for getting MQTTS:

ESP8266 uses MQTT as default. If you want to use MQTTS, you need to do below steps.

Install ESP8266 updater plugin for Arduino IDE.

File certs.ar containing necessary certificates is included in this repository.

Below procedure run once, puts file containing certificates in ESP8266 filesystem:

  • run Arduino IDE,
  • create a new (empty) sketch (eg. ESP8266fs.ino),
  • save it in some folder (eg. certificates),
  • create subfolder data in prepared folder certificates,
  • put file certs.ar (please do not change name because it is called by SDK) into data subfolder,
  • open sketch ESP8266fs.ino (if necessary),
  • close Serial Monitor (if opened),
  • use tool to upload : Tools -> ESP8266 Sketch Data Upload.

Developer guide

Declare parameters

You can update over the air some parameters of your sketch using Live Objects's MQTT Parameters. Parameters and Commands must be declared before your device connects to Live Objects.

You can declare parameters with the addParameter() instruction, which accepts the following arguments:

  • the label of your parameter (const char* expected) as it will be displayed on Live Objects;
  • the variable your parameter is stored in (the variable can be of any type except char*, char[] or any custom type);
  • (optional) a callback function, if you need to perform some tasks after the parameter has been updated;
  • (optional) the type of parameter you want to use on Live Objects, among INTEGER, UNSIGNED_INTEGER, BINARY, STRING or DECIMAL (for float values). If omitted, the type will be automatically chosen from the variable you use.
int myParam;
...
lo.addParameter("my parameter", myParam);
lo.addParameter("a second parameter", 2ndParam, myCallbackFunction);
lo.addParameter("a third parameter", 3rdParam, STRING);
lo.addParameter("another parameter", anotherParam, anotherCallbackFunction, UNSIGNED_INTEGER);

The callback function does not take any arguments. It is of form

void myCallbackFunction() {
  // do stuff
}

Further reading on Live Objects' MQTT parameters.

Declare commands

Commands lets you trigger specific actions on your device from Live Objects. Parameters and Commands must be declared before your device connects to Live Objects.

Commands can be declared using the addcommand() instruction, which accepts the following arguments:

  • the label of your command (const char* expected);
  • the callback function that will execute the command.
lo.addParameter("a command", myCallback);

The callback function is of form

void myCallback(const String arguments, String &response) {
  // do stuff
}

Arguments and response are optional when using commands, but they can be useful if you want to pass parameters to your function. For instance, you could define a play tone command that will use some parameters like the frequency of the tone, or its duration.

  • Any incoming arguments will be passed using the arguments String containing a JSON object;
  • You can pass response arguments in the form of a JSON objet stored in the response String.
void playTone(const String arguments, String &response) {
  // arguments = "{\"duration\":2000,\"frequency\":440}"
  // play the tone accordingly to arguments
  response = "{\"I played\":\"the tone\"}";
}

void setup() {
  lo.addParameter("play tone", playTone);
}

⚠️ Command name and arguments are case-sensitive when creating the command on Live Objects.: On the opposite, there is no specific order for specifying the command arguments. Live Object screenshot

You may use the ArduinoJSON library, or any other library to process the JSON objects more easily.

Further reading on Live Objects' MQTT commands.

Send data

You can send data very easily to Live Objects.

Dead simple method

Compose your payload using the addToPayload() instruction. You will need to provide a label (const char* expected) for your value, and the data itself. You data can be of any type.

Data is added on each call to addToPayload(), so repeat the instruction if you have multiple data to send. When your payload is ready, send it using sendData(). That simple.

int value;
double myOtherValue;

void loop() {
  // collect data
  lo.addToPayload("my data", value);
  lo.addToPayload("my other data", myOtherValue);
  lo.sendData();
}

As soon the data is send, your payload is cleared and waiting for the next sending.

Advanced method

Advanced users may want to add more specific fields to their payload, like geolocation information or custom timestamps. In that case, you can compose your own JSON payload as a string (char* or String) and pass it to send using sendData():

void loop() {
  String myPayloadString = "{\"value\":{\"uptime\":0, \"conditions\":\"good\"}}"
  lo.sendData(myPayloadString);

  // or
  char* myPayloadCharArray = "{\"value\":{\"uptime\":0, \"conditions\":\"good\"}}"
  lo.sendData(myPayloadCharArray);
}

Connect, disconnect and loop

You can control the connection and disconnection of your device using connect() and disconnect().

Before calling connect, using begin(Protocol, Mode, doDebug) u can specify which protocol and mode u want to use and also if you want to output debug messages.

In order to check for any incoming configuration update or command, you need to keep the loop() instruction in your main loop.

void setup()
{
  lo.begin(MQTT,NONE,true);
  lo.connect();
}
void loop()
{
  //Do some stuff
  //...
  lo.loop(); //Keep this in main loop
}

Toubleshooting

My payload is truncated on Live Objects

This can happen with large payload, because of the fixed-size JSON storage allocated for processing your payload (1024 bytes by default). You can allocate more room by modifying the value in the 'LiveObjectsBase.h' file at line 13 e.g.:

#define PAYLOAD_DATA_SIZE 2048

My parameters are not registered on Live Objects

Same reason as above, it can happen if you have a large number of parameters. You need to allocate more room by modifying the value in the 'LiveObjectsBase.h' file at line 13 e.g.:

#define PAYLOAD_DATA_SIZE 2048

One of my command is not working

If you have many arguments in your command, first check that you get all the arguments when entering the function:

// this is your callback function
void blinkLED(const String arguments, String &response) {
  // display arguments
  Serial.print("arguments: ");
  Serial.println(arguments);
  ...
}

If your arguments are incomplete, try allocating more room for JSON storage by modifying the value in the 'LiveObjectsBase.h' file at line 13 e.g.:

#define PAYLOAD_DATA_SIZE 2048

liveobjects_sdk_for_arduino's People

Contributors

kacp3r3 avatar krzysztofkrzeslak avatar mdelain avatar tomekmalek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

liveobjects_sdk_for_arduino's Issues

MKR1010 - Can't connect to LiveObjects

Hello,

I'm trying the send_data example sketch to check my connection to LiveObjects.
I followed the READme file, I am able to see that i'm connected to the broker, unfortunately all I can see in my LiveObjects logs are a bunch of errors. Therefore i'd appreciate some help, if anyone can help please...

If ever you need more details, please tell me.

EDIT: I tried to the MQTT.Fx client simulator and add the first error as well.
Might not be an issue with the Arduino ?!

Arduino IDE output:

[INFO] Connecting to MQTT broker liveobjects.orange-business.com:8883
[INFO] You're connected to the MQTT broker
Sampling data
Sending data to Live Objects
[INFO] Publishing message on topic: dev/data
{
  "value": {
    "uptime": 8250
  },
  "model": "3C71BF87D2B0"
}
Sampling data
Sending data to Live Objects
[INFO] Publishing message on topic: dev/data
{
  "value": {
    "uptime": 13256
  },
  "model": "3C71BF87D2B0"
}

First error in LiveObject (Category: connectivity - MQTT) - Do I need to change anything after downloading the repo ? I checked the LiveObjectBase.h file and the MQTT_USER is json+device.

Message d'audit

Informations
Level
error

Timestamp
2021-02-25T15:21:48.862Z

Created
2021-02-25T15:21:48.863Z

Category
Connectivity

Subcategory
Mqtt

Type
Mqtt Debug Logs

Description
Connection Refused - Bad Connection Parameters : Unsupported 'Application' MQTT Username. Please Use 'json+device', 'application' Or 'connector'.

Détail du contenu
Client Id
aeso_nodered

Session Id
4d02edc3-0203-4f1c-9228-54efadf9131c

Second error in LiveObject (Category: notification - http push)

Message d'audit

Informations
Level
error

Timestamp
2021-02-25T15:21:46.750Z

Created
2021-02-25T15:21:46.750Z

Category
Notification

Subcategory
Http

Type
Http Push Result

ActionPolicyId
22c75186-6fe7-4d54-ba21-cb9987ff0b27

Description
Error Sending Http Message: Designated As Bad Request By The Server

Detailed Description
400: {"text":"Invalid Data Format","code":6,"invalid-event-number":0}

Détail du contenu
Response Status Code
400

Response Body
{"text":"Invalid data format","code":6,"invalid-event-number":0}

Success
False

Error Message
400 Bad Request

Authorization
Splunk 0ac76041-1f01-467b-bff1-46f2fd6c9ae1

X-orange-lo-policy-id
22c75186-6fe7-4d54-ba21-cb9987ff0b27

Retry On Failure
False

Request Body
{"event": { "counter1": , "counter2": , "counter3": ,"counter4": , "timestamp" : "2021-02-25T15:21:46.715Z","devEUI": ""} }

MessageUUID
ac9e28f9-4f42-4b7b-89bd-5f7d956b7f1d

Url
http://15.188.189.216:8443/services/collector/event

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.