1- //
21// Copyright 2019 Blues Inc. All rights reserved.
2+ //
33// Use of this source code is governed by licenses granted by the
44// copyright holder including that found in the LICENSE file.
55//
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.
89//
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.
1516//
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
1821
1922#if defined(ARDUINO_ARCH_AVR) && not defined(HAVE_HWSERIAL1)
2023#define txRxPinsSerial Serial
2831#include " Adafruit_TinyUSB.h"
2932#endif
3033
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"
3641
3742// This is the unique Product Identifier for your device
3843#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"
4045#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"
4146#endif
47+
4248#define myProductID PRODUCT_UID
43- #define myLiveDemo true
49+ #define myLiveDemo true
4450
4551// One-time Arduino initialization
4652void setup ()
4753{
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.
5360 txRxPinsSerial.begin (9600 );
5461 txRxPinsSerial.println (" \n " );
5562 delay (250 );
5663
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+ {
6068 txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" product\" :\" " myProductID " \" }" );
6169 }
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.
6677#if myLiveDemo
6778 txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" mode\" :\" continuous\" }" );
6879#else
6980 txRxPinsSerial.println (" {\" cmd\" :\" hub.set\" ,\" mode\" :\" periodic\" ,\" outbound\" :60}" );
7081#endif
71-
7282}
7383
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`.
7586void loop ()
7687{
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.
7990 static unsigned eventCounter = 0 ;
80- if (eventCounter++ > 25 ) {
91+ if (++eventCounter > 25 )
92+ {
8193 return ;
8294 }
8395
8496 // 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 ;
8698
8799 // 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).
96110 char message[150 ];
97111 snprintf (message, sizeof (message),
98112 " {"
@@ -102,16 +116,15 @@ void loop()
102116 " ,"
103117 " \" body\" :{\" temp\" :%d.%02d,\" voltage\" :%d.%02d,\" count\" :%d}"
104118 " }" ,
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 ,
107121 eventCounter);
108122 txRxPinsSerial.println (message);
109123
110124 // Delay between simulated measurements
111125#if myLiveDemo
112- delay (15 * 1000 ); // 15 seconds
126+ delay (15 * 1000 ); // 15 seconds
113127#else
114- delay (15 * 60 * 1000 ); // 15 minutes
128+ delay (15 * 60 * 1000 ); // 15 minutes
115129#endif
116-
117130}
0 commit comments