Skip to content

Commit d51713d

Browse files
authored
Merge pull request #2 from datacentricdesign/develop
version 0.0.1
2 parents 49858cf + 3065357 commit d51713d

File tree

10 files changed

+306
-107
lines changed

10 files changed

+306
-107
lines changed

.vscode/arduino.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"board": "arduino:samd:nano_33_iot",
3+
"port": "/dev/cu.usbmodem14201",
4+
"sketch": "example/Arduino_nano33_MQTT/Arduino_nano33_MQTT.ino"
5+
}

.vscode/c_cpp_properties.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"/Users/neer/Library/Arduino15/packages/arduino/tools/**",
7+
"/Users/neer/Library/Arduino15/packages/arduino/hardware/samd/1.8.4/**"
8+
],
9+
"forcedInclude": [
10+
"/Users/neer/Library/Arduino15/packages/arduino/hardware/samd/1.8.4/cores/arduino/Arduino.h"
11+
],
12+
"macFrameworkPath": [
13+
"/System/Library/Frameworks",
14+
"/Library/Frameworks"
15+
],
16+
"intelliSenseMode": "clang-x64",
17+
"compilerPath": "/usr/bin/clang",
18+
"cStandard": "c11",
19+
"cppStandard": "c++17"
20+
}
21+
],
22+
"version": 4
23+
}

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [{
7+
"name": "cpp build and debug active file",
8+
"type": "cppdbg",
9+
"request": "launch",
10+
"program": "${fileDirname}/${fileBasenameNoExtension}",
11+
"args": [],
12+
"stopAtEntry": false,
13+
"cwd": "${workspaceFolder}",
14+
"environment": [],
15+
"externalConsole": false,
16+
"MIMode": "lldb",
17+
"preLaunchTask": "cpp build active file"
18+
}]
19+
}

.vscode/tasks.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [{
6+
"type": "shell",
7+
"label": "cpp build active file",
8+
"command": "/usr/bin/cpp",
9+
"args": [
10+
"-g",
11+
"${file}",
12+
"-o",
13+
"${fileDirname}/${fileBasenameNoExtension}"
14+
],
15+
"options": {
16+
"cwd": "/usr/bin"
17+
},
18+
"problemMatcher": [
19+
"$gcc"
20+
],
21+
"group": "build"
22+
}]
23+
}

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# dcd-sdk-arduino
1+
# DCD SDK Arduino
2+
3+
4+
5+
# Changelog
6+
7+
All notable changes to this project will be documented in this file.
8+
9+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
10+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
11+
12+
## [Unreleased]
13+
14+
### Added
Lines changed: 29 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,46 @@
1-
#include <ArduinoMqttClient.h>
2-
#include <WiFiNINA.h>
3-
#include <Wire.h>
4-
#include <Adafruit_Sensor.h>
5-
#include <Adafruit_BNO055.h>
6-
#include <utility/imumaths.h>
7-
#include "arduino_secrets.h"
8-
9-
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
10-
char ssid[] = SECRET_SSID; // your network SSID (name)
11-
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
12-
const String thing_id = THING_ID; // thing Id
13-
const String thing_token = THING_TOKEN; // Token for created thing
14-
15-
WiFiSSLClient wifiClient;
16-
MqttClient mqttClient(wifiClient);
17-
18-
String broker = "dwd.tudelft.nl"; //mqtt host
19-
int port = 8883; //mqtt port
20-
const String client_id = "Arduino Nano 33 IoT";
21-
String topic = "/things/" + thing_id + "/properties/random-02d3"; // topic to publish data on MQTT for DCD hub
22-
23-
const long interval = 1000;
24-
unsigned long previousMillis = 0;
25-
int values[] = {0, 0, 0};
26-
int count = 0;
1+
// The example uses an arduno sdk for DCD hub to connect and update
2+
// individual thing's property.
3+
// You can check live update on your dcd hub dashboard after a successful
4+
// connection here: https://dwd.tudelft.nl/subject.
5+
//
6+
// by Nirav Malsattar <[email protected]>
7+
// https://github.com/datacentricdesign/dcd-sdk-arduino
278

28-
void messageReceived(int messageSize) {
29-
// we received a message, print out the topic and contents
30-
Serial.print("Received a message with topic '");
31-
Serial.print(mqttClient.messageTopic());
32-
Serial.print("', duplicate = ");
33-
Serial.print(mqttClient.messageDup() ? "true" : "false");
34-
Serial.print(", QoS = ");
35-
Serial.print(mqttClient.messageQoS());
36-
Serial.print(", retained = ");
37-
Serial.print(mqttClient.messageRetain() ? "true" : "false");
38-
Serial.print("', length ");
39-
Serial.print(messageSize);
40-
Serial.println(" bytes:");
9+
#include "arduino_secrets.h"
10+
#include <dcd_hub_arduino.h>
4111

42-
// use the Stream interface to print the contents
43-
while (mqttClient.available()) {
44-
Serial.print((char)mqttClient.read());
45-
}
46-
Serial.println();
47-
48-
Serial.println();
49-
}
12+
dcd_hub_arduino dcdHub; //creates a class object from library
5013

5114
void setup() {
5215
//Initialize serial and wait for port to open:
5316
Serial.begin(115200);
5417
while (!Serial) {
5518
; // wait for serial port to connect. Needed for native USB port only
5619
}
57-
58-
// attempt to connect to Wifi network:
59-
Serial.print("Attempting to connect to WPA SSID: ");
60-
Serial.println(ssid);
61-
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
62-
// failed, retry
63-
Serial.print(".");
64-
delay(3000);
65-
}
66-
67-
Serial.println("You're connected to the network");
68-
Serial.println();
69-
70-
// You can provide a unique client ID, if not set the library uses Arduino-millis()
71-
// Each client must have a unique client ID
72-
mqttClient.setId(client_id);
73-
74-
// You can provide a username and password for authentication
75-
mqttClient.setUsernamePassword(THING_ID, THING_TOKEN);
76-
77-
Serial.print("Attempting to connect to the MQTT broker: ");
78-
Serial.println(broker);
79-
80-
if (!mqttClient.connect(broker.c_str(), port)) {
81-
Serial.print("MQTT connection failed! Error code = ");
82-
Serial.println(mqttClient.connectError());
83-
84-
while (1);
85-
}
86-
87-
mqttClient.onMessage(messageReceived);
8820

89-
Serial.println("You're connected to the MQTT broker!");
21+
// Connects to dcd hub using your secret credential from "arduino_secrets.h"
22+
//{class_object}.connect(SECRET_SSID, SECRET_PASS, THING_ID, THING_TOKEN)
23+
24+
// Make sure you have stored your credentials on "arduino_secrets.h" before running this command
25+
26+
dcdHub.connect(SECRET_SSID, SECRET_PASS, THING_ID, THING_TOKEN, "Arduino Nano 33 IoT");
9027
Serial.println();
9128
}
9229

9330
void loop() {
94-
// call poll() regularly to allow the library to send MQTT keep alives which
95-
// avoids being disconnected by the broker
96-
mqttClient.poll();
97-
98-
unsigned long currentMillis = millis();
99-
100-
if (currentMillis - previousMillis >= interval) {
101-
// save the last time a message was sent
102-
previousMillis = currentMillis;
103-
104-
Serial.print("Sending message to topic: ");
105-
Serial.println(topic);
106-
107-
int value = random(5);
108-
109-
String json = "{\"id\":\"random-02d3\",\"values\":[[";
110-
json.concat(value);
111-
json.concat("]]}");
112-
Serial.print("pushing...");
113-
114-
//send message, the Print interface can be used to set the message contents
115-
mqttClient.beginMessage(topic);
116-
mqttClient.print(json);
117-
mqttClient.endMessage();
31+
32+
// call keep_alive_mqtt() regularly to avoids being disconnected by the MQTT broker
33+
dcdHub.keep_alive_mqtt();
11834

119-
Serial.println();
35+
// Some random 1D, 2D, 3D values to upload on the hub. Later, you can replace these with your sensors value.
36+
float value[] = {random(5)};
37+
float value2[] = {random(80), random(25)};
38+
float value3[] = {random(80), random(25), random(60)};
12039

121-
count++;
122-
}
40+
//call to an update_property object to update property value as an array according to it's "proeprty_id"
41+
//{class_object}.update property (property_id, value[], dimension)
42+
43+
dcdHub.update_property("random-02d3",value, 1);
44+
dcdHub.update_property("my-random-property3-e0cf",value2, 2);
45+
dcdHub.update_property("my-random-property2-563b",value3, 3);
12346
}

keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map For DCD SDK Arduino
3+
#######################################
4+
5+
#######################################
6+
# Library (KEYWORD1)
7+
#######################################
8+
9+
dcd_hub_arduino KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
connect KEYWORD2
16+
update_property KEYWORD2
17+
keep_alive_mqtt KEYWORD2
18+
19+
#######################################
20+
# Constants (LITERAL1)
21+
#######################################
22+

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=DCD SDK for Arduino
2+
version=0.0.1
3+
author=Nirav Malsattar <[email protected]>, Jacky Bourgeois <[email protected]>
4+
maintainer=Nirav Malsattar <[email protected]>
5+
sentence=A library that allows to connect to dcd hub and update value of thing property provided by property_id.
6+
paragraph= Currently this library uses MQTT interface to connect to the DCD hub and update particular thing property. In future release there will be more functionality added to create a new property or seatch for exising one.
7+
category=Communication
8+
url=https://github.com/datacentricdesign/dcd-sdk-arduino
9+
architectures=*
10+
includes=arduino_dcd_hub.h

src/dcd_hub_arduino.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "dcd_hub_arduino.h"
2+
3+
dcd_hub_arduino::~dcd_hub_arduino() {
4+
delete(mqttClientPtr);
5+
}
6+
7+
dcd_hub_arduino::dcd_hub_arduino() {
8+
mqttClientPtr = new MqttClient(wifiClient);
9+
}
10+
11+
void dcd_hub_arduino::wifiConnect(String ssid, String pass) {
12+
Serial.print("Attempting to connect to WPA SSID: ");
13+
Serial.println(ssid);
14+
15+
while (status != WL_CONNECTED) {
16+
status = WiFi.begin((ssid).c_str(), wifiKeyIndex, (pass).c_str());
17+
Serial.print(".");
18+
delay(3000);
19+
}
20+
21+
Serial.println("You're connected to the network");
22+
}
23+
24+
void dcd_hub_arduino::mqttConnect(String client_id) {
25+
mqttClientPtr->setId(client_id);
26+
27+
mqttClientPtr->setUsernamePassword(_thing_id, _thing_token);
28+
29+
Serial.print("Attempting to connect to the MQTT broker: ");
30+
Serial.println(broker);
31+
32+
if (!mqttClientPtr->connect((broker).c_str(), port))
33+
{
34+
Serial.print("MQTT connection failed! Error code = ");
35+
Serial.println(mqttClientPtr->connectError());
36+
37+
while (1)
38+
;
39+
}
40+
41+
Serial.println("You're connected to the DCD hub MQTT broker!");
42+
}
43+
44+
void dcd_hub_arduino::connect(String ssid, String pass, String thing_id, String thing_token, String client_id)
45+
{
46+
47+
_ssid = ssid;
48+
_pass = pass;
49+
_client_id = client_id;
50+
_thing_id = thing_id;
51+
_thing_token = thing_token;
52+
53+
wifiConnect(_ssid, _pass); //Connect to wifi
54+
mqttConnect(_client_id); // Connect to mqtt broker
55+
56+
}
57+
58+
void dcd_hub_arduino::update_property (String property_id, float values[], int value_type) {
59+
60+
topic = "/things/" + _thing_id + "/properties/" + property_id; // topic to publish data on MQTT for DCD hub
61+
62+
if (WiFi.status() == WL_CONNECTED) {
63+
64+
unsigned long currentMillis = millis();
65+
66+
if (currentMillis - previousMillis >= 500) {
67+
// save the last time a message was sent
68+
previousMillis = currentMillis;
69+
Serial.println();
70+
Serial.print("Sending message to topic: ");
71+
Serial.println(topic);
72+
String json = "{\"id\":\"";
73+
json.concat(property_id);
74+
json.concat("\",\"values\":[[");
75+
76+
for (int i = 0; i < value_type; ++i) {
77+
json.concat(values[i]);
78+
if (i < value_type - 1) {
79+
json.concat(",");
80+
}
81+
}
82+
83+
json.concat("]]}");
84+
Serial.print("pushing..");
85+
86+
//send message, the Print interface can be used to set the message contents
87+
mqttClientPtr->beginMessage(topic);
88+
mqttClientPtr->print(json);
89+
mqttClientPtr->endMessage();
90+
91+
Serial.println(json);
92+
}
93+
}
94+
95+
else {
96+
Serial.println ("No internet connection....Trying to reconnect");
97+
wifiConnect(_ssid, _pass); //Connect to wifi
98+
mqttConnect(_client_id); // Connect to mqtt broker
99+
}
100+
}
101+
102+
void dcd_hub_arduino::keep_alive_mqtt () {
103+
104+
unsigned long currentMillis = millis();
105+
106+
if (currentMillis - previousMillis >= interval) {
107+
// save the last time a message was sent
108+
previousMillis = currentMillis;
109+
110+
mqttClientPtr->poll();
111+
}
112+
}

0 commit comments

Comments
 (0)