1
- //
2
1
// Copyright 2019 Blues Inc. All rights reserved.
2
+ //
3
3
// Use of this source code is governed by licenses granted by the
4
4
// copyright holder including that found in the LICENSE file.
5
5
//
6
- // This is the simplest example of how a device may send commands to the Notecard over a serial
7
- // port by using nothing but simple "print line" functions targeting that Arduino serial port.
6
+ // This is the simplest example of how a device may send commands to the
7
+ // Notecard over a serial port by using nothing but simple "print line"
8
+ // functions targeting that Arduino serial port.
8
9
//
9
-
10
- // If the Notecard is connected to a serial port, define it here. For example, if you are using
11
- // the Adafruit Feather NRF52840 Express, the RX/TX pins (and thus the Notecard) are on Serial1.
12
- // If however you are using an M5Stack Basic Core IoT Development Kit, you would connect the
13
- // R2 pin to the Notecard's TX pin , and the M5Stack's T2 pin to the Notecard's RX pin, and then
14
- // would use Serial2.
10
+ // If the Notecard is connected to a serial port, define it here. For example,
11
+ // if you are using the Adafruit Feather NRF52840 Express, the RX/TX pins (and
12
+ // thus the Notecard) are on Serial1. If however you are using an M5Stack Basic
13
+ // Core IoT Development Kit, you would connect the R2 pin to the Notecard's TX
14
+ // pin, and the M5Stack's T2 pin to the Notecard's RX pin, and then would
15
+ // use Serial2.
15
16
//
16
- // Note that both of these definitions are optional; just prefix either line with // to remove it.
17
- // Remove txRxPinsSerial if you wired your Notecard using I2C SDA/SCL pins instead of serial RX/TX
17
+ // Note that both of these definitions are optional; just prefix either line
18
+ // with `//` to remove it.
19
+ // - Remove txRxPinsSerial if you wired your Notecard using I2C SDA/SCL pins
20
+ // instead of serial RX/TX
18
21
19
22
#if defined(ARDUINO_ARCH_AVR) && not defined(HAVE_HWSERIAL1)
20
23
#define txRxPinsSerial Serial
28
31
#include " Adafruit_TinyUSB.h"
29
32
#endif
30
33
31
- // This is the unique Product Identifier for your device. This Product ID tells the Notecard what
32
- // type of device has embedded the Notecard, and by extension which vendor or customer is in charge
33
- // of "managing" it. In order to set this value, you must first register with notehub.io and
34
- // "claim" a unique product ID for your device. It could be something as simple as as your email
35
- // address in reverse, such as "com.gmail.smith.lisa.test-device" or "com.outlook.gates.bill.demo"
34
+ // This is the unique Product Identifier for your device. This Product ID tells
35
+ // the Notecard what type of device has embedded the Notecard, and by extension
36
+ // which vendor or customer is in charge of "managing" it. In order to set this
37
+ // value, you must first register with notehub.io and "claim" a unique product
38
+ // ID for your device. It could be something as simple as as your email address
39
+ // in reverse, such as "com.gmail.smith.lisa.test-device" or
40
+ // "com.outlook.gates.bill.demo"
36
41
37
42
// This is the unique Product Identifier for your device
38
43
#ifndef PRODUCT_UID
39
- #define PRODUCT_UID " " // "com.my-company.my-name:my-project"
44
+ #define PRODUCT_UID " " // "com.my-company.my-name:my-project"
40
45
#pragma message "PRODUCT_UID is not defined in this example. Please ensure your Notecard has a product identifier set before running this example or define it in code here. More details at https:// dev.blues.io/tools-and-sdks/samples/product-uid"
41
46
#endif
47
+
42
48
#define myProductID PRODUCT_UID
43
- #define myLiveDemo true
49
+ #define myLiveDemo true
44
50
45
51
// One-time Arduino initialization
46
52
void setup ()
47
53
{
48
-
49
- // Initialize the serial port being used by the Notecard, and send a newline to clear out any data
50
- // that the Arduino software may have pending so that we always start sending commands "cleanly".
51
- // By delaying for 250ms, we ensure the any pending commands can be processed or discarded. We use
52
- // the speed of 9600 because the Notecard's RX/TX pins are always configured for that speed.
54
+ // Initialize the serial port being used by the Notecard, and send a newline
55
+ // to clear out any data that the Arduino software may have pending so that
56
+ // we always start sending commands "cleanly". By delaying for 250ms, we
57
+ // ensure the any pending commands can be processed or discarded. We use the
58
+ // speed of 9600 because the Notecard's RX/TX pins are always configured for
59
+ // that speed.
53
60
txRxPinsSerial.begin (9600 );
54
61
txRxPinsSerial.println (" \n " );
55
62
delay (250 );
56
63
57
- // This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
58
- // this Product ID. (see above)
59
- if (myProductID[0 ]) {
64
+ // This command (required) causes the data to be delivered to the Project on
65
+ // notehub.io that has claimed this Product ID (see above).
66
+ if (myProductID[0 ])
67
+ {
60
68
txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" product\" :\" " myProductID " \" }" );
61
69
}
62
- // This command determines how often the Notecard connects to the service. If "continuous" the Notecard
63
- // immediately establishes a session with the service at notehub.io, and keeps it active continuously.
64
- // Because of the power requirements of a continuous connection, a battery powered device would instead
65
- // only sample its sensors occasionally, and would only upload to the service on a periodic basis.
70
+
71
+ // This command determines how often the Notecard connects to the service.
72
+ // If "continuous" the Notecard immediately establishes a session with the
73
+ // service at notehub.io, and keeps it active continuously. Because of the
74
+ // power requirements of a continuous connection, a battery powered device
75
+ // would instead only sample its sensors occasionally, and would only upload
76
+ // to the service on a periodic basis.
66
77
#if myLiveDemo
67
78
txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" mode\" :\" continuous\" }" );
68
79
#else
69
80
txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" mode\" :\" periodic\" ,\" outbound\" :60}" );
70
81
#endif
71
-
72
82
}
73
83
74
- // In the Arduino main loop which is called repeatedly, add outbound data every 15 seconds
84
+ // In the Arduino main loop which is called repeatedly, add outbound data every
85
+ // 15 seconds if `myLiveDemo` is `true`, or 15 minutes if `false`.
75
86
void loop ()
76
87
{
77
-
78
- // Count the simulated measurements that we send to the cloud, and stop the demo before long.
88
+ // Count the simulated measurements that we send to the cloud, and stop the
89
+ // demo before long.
79
90
static unsigned eventCounter = 0 ;
80
- if (eventCounter++ > 25 ) {
91
+ if (++eventCounter > 25 )
92
+ {
81
93
return ;
82
94
}
83
95
84
96
// Simulate a temperature reading, between 5.0 and 35.0 degrees C
85
- double temperature = (double ) random (50 , 350 ) / 10.0 ;
97
+ double temperature = (double )random (50 , 350 ) / 10.0 ;
86
98
87
99
// Simulate a voltage reading, between 3.1 and 4.2 degrees
88
- double voltage = (double ) random (31 , 42 ) / 10.0 ;
89
-
90
- // Add a "note" to the Notecard, in the default data notefile. The "body" of the note is
91
- // JSON object completely of our own design, and is passed straight through as-is to notehub.io.
92
- // (Note that we add the "sync" flag for demonstration purposes to upload the data instantaneously,
93
- // so that if you are looking at this on notehub.io you will see the data appearing 'live'.)
94
- // Note that we use a somewhat convoluted way of displaying a floating point number because %f
95
- // isn't supported in many versions of Arduino (newlib).
100
+ double voltage = (double )random (31 , 42 ) / 10.0 ;
101
+
102
+ // Add a "note" to the Notecard, in the default data notefile. The "body"
103
+ // of the note is JSON object completely of our own design, and is passed
104
+ // straight through as-is to notehub.io.
105
+ // Note that we add the "sync" flag for demonstration purposes to upload the
106
+ // data instantaneously, so that if you are looking at this on notehub.io
107
+ // you will see the data appearing 'live'.
108
+ // Note that we use a somewhat convoluted way of displaying a floating point
109
+ // number because `%f` isn't supported in many versions of Arduino (newlib).
96
110
char message[150 ];
97
111
snprintf (message, sizeof (message),
98
112
" {"
@@ -102,16 +116,15 @@ void loop()
102
116
" ,"
103
117
" \" body\" :{\" temp\" :%d.%02d,\" voltage\" :%d.%02d,\" count\" :%d}"
104
118
" }" ,
105
- (int )temperature, abs (((int )(temperature* 100.0 )% 100 )),
106
- (int )voltage, (int )(voltage* 100.0 )% 100 ,
119
+ (int )temperature, abs (((int )(temperature * 100.0 ) % 100 )),
120
+ (int )voltage, (int )(voltage * 100.0 ) % 100 ,
107
121
eventCounter);
108
122
txRxPinsSerial.println (message);
109
123
110
124
// Delay between simulated measurements
111
125
#if myLiveDemo
112
- delay (15 * 1000 ); // 15 seconds
126
+ delay (15 * 1000 ); // 15 seconds
113
127
#else
114
- delay (15 * 60 * 1000 ); // 15 minutes
128
+ delay (15 * 60 * 1000 ); // 15 minutes
115
129
#endif
116
-
117
130
}
0 commit comments