|
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 |
27 | 8 |
|
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> |
41 | 11 |
|
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 |
50 | 13 |
|
51 | 14 | void setup() { |
52 | 15 | //Initialize serial and wait for port to open: |
53 | 16 | Serial.begin(115200); |
54 | 17 | while (!Serial) { |
55 | 18 | ; // wait for serial port to connect. Needed for native USB port only |
56 | 19 | } |
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); |
88 | 20 |
|
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"); |
90 | 27 | Serial.println(); |
91 | 28 | } |
92 | 29 |
|
93 | 30 | 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(); |
118 | 34 |
|
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)}; |
120 | 39 |
|
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); |
123 | 46 | } |
0 commit comments