Skip to content

Commit 589dcdf

Browse files
committed
convert to otbs to ensure consistency in our public repos
1 parent c78cfe7 commit 589dcdf

File tree

9 files changed

+832
-772
lines changed

9 files changed

+832
-772
lines changed

examples/Example0_LibrarylessCommunication/Example0_LibrarylessCommunication.ino

+52-49
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,79 @@
2121
// This is the unique Product Identifier for your device. This Product ID tells the Notecard what
2222
// type of device has embedded the Notecard, and by extension which vendor or customer is in charge
2323
// of "managing" it. In order to set this value, you must first register with notehub.io and
24-
// "claim" a unique product ID for your device. It could be something as simple as as your email
24+
// "claim" a unique product ID for your device. It could be something as simple as as your email
2525
// address in reverse, such as "com.gmail.smith.lisa.test-device" or "com.outlook.gates.bill.demo"
2626

2727
#define myProductID "com.your-company.your-name:your_project"
2828
#define myLiveDemo true
2929

3030
// One-time Arduino initialization
31-
void setup() {
31+
void setup()
32+
{
3233

33-
// Initialize the serial port being used by the Notecard, and send a newline to clear out any data
34-
// that the Arduino software may have pending so that we always start sending commands "cleanly".
35-
// We use the speed of 9600 because the Notecard's RX/TX pins are always configured for that speed.
36-
serialNotecard.begin(9600);
37-
serialNotecard.println("\n");
34+
// Initialize the serial port being used by the Notecard, and send a newline to clear out any data
35+
// that the Arduino software may have pending so that we always start sending commands "cleanly".
36+
// We use the speed of 9600 because the Notecard's RX/TX pins are always configured for that speed.
37+
serialNotecard.begin(9600);
38+
serialNotecard.println("\n");
3839

39-
// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
40-
// this Product ID. (see above)
41-
serialNotecard.println("{\"req\":\"service.set\",\"product\":\"" myProductID "\"}");
40+
// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
41+
// this Product ID. (see above)
42+
serialNotecard.println("{\"req\":\"service.set\",\"product\":\"" myProductID "\"}");
4243

43-
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
44+
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
4445
// immediately establishes a session with the service at notehub.io, and keeps it active continuously.
4546
// Because of the power requirements of a continuous connection, a battery powered device would instead
4647
// only sample its sensors occasionally, and would only upload to the service on a periodic basis.
4748
#if myLiveDemo
48-
serialNotecard.println("{\"req\":\"service.set\",\"mode\":\"continuous\"}");
49+
serialNotecard.println("{\"req\":\"service.set\",\"mode\":\"continuous\"}");
4950
#else
50-
serialNotecard.println("{\"req\":\"service.set\",\"mode\":\"periodic\",\"outbound\":60}");
51+
serialNotecard.println("{\"req\":\"service.set\",\"mode\":\"periodic\",\"outbound\":60}");
5152
#endif
5253

5354
}
5455

5556
// In the Arduino main loop which is called repeatedly, add outbound data every 15 seconds
56-
void loop() {
57-
58-
// Count the simulated measurements that we send to the cloud, and stop the demo before long.
59-
static unsigned eventCounter = 0;
60-
if (eventCounter++ > 25)
61-
return;
62-
63-
// Simulate a temperature reading, between 5.0 and 35.0 degrees C
64-
double temperature = (double) random(50, 350) / 10.0;
65-
66-
// Simulate a voltage reading, between 3.1 and 4.2 degrees
67-
double voltage = (double) random(31, 42) / 10.0;
68-
69-
// Add a "note" to the Notecard, in the default data notefile. The "body" of the note is
70-
// JSON object completely of our own design, and is passed straight through as-is to notehub.io.
71-
// (Note that we add the "start" flag for demonstration purposes to upload the data instantaneously,
72-
// so that if you are looking at this on notehub.io you will see the data appearing 'live'.)
73-
// Note that we use a somewhat convoluted way of displaying a floating point number because %f
74-
// isn't supported in many versions of Arduino (newlib).
75-
char message[150];
76-
snprintf(message, sizeof(message),
77-
"{"
78-
"\"req\":\"note.add\""
79-
","
80-
"\"start\":true"
81-
","
82-
"\"body\":{\"temp\":%d.%02d,\"voltage\":%d.%02d,\"count\":%d}"
83-
"}",
84-
(int)temperature, abs(((int)(temperature*100.0)%100)),
85-
(int)voltage, (int)(voltage*100.0)%100,
86-
eventCounter);
87-
serialNotecard.println(message);
88-
89-
// Delay between simulated measurements
57+
void loop()
58+
{
59+
60+
// Count the simulated measurements that we send to the cloud, and stop the demo before long.
61+
static unsigned eventCounter = 0;
62+
if (eventCounter++ > 25) {
63+
return;
64+
}
65+
66+
// Simulate a temperature reading, between 5.0 and 35.0 degrees C
67+
double temperature = (double) random(50, 350) / 10.0;
68+
69+
// Simulate a voltage reading, between 3.1 and 4.2 degrees
70+
double voltage = (double) random(31, 42) / 10.0;
71+
72+
// Add a "note" to the Notecard, in the default data notefile. The "body" of the note is
73+
// JSON object completely of our own design, and is passed straight through as-is to notehub.io.
74+
// (Note that we add the "start" flag for demonstration purposes to upload the data instantaneously,
75+
// so that if you are looking at this on notehub.io you will see the data appearing 'live'.)
76+
// Note that we use a somewhat convoluted way of displaying a floating point number because %f
77+
// isn't supported in many versions of Arduino (newlib).
78+
char message[150];
79+
snprintf(message, sizeof(message),
80+
"{"
81+
"\"req\":\"note.add\""
82+
","
83+
"\"start\":true"
84+
","
85+
"\"body\":{\"temp\":%d.%02d,\"voltage\":%d.%02d,\"count\":%d}"
86+
"}",
87+
(int)temperature, abs(((int)(temperature*100.0)%100)),
88+
(int)voltage, (int)(voltage*100.0)%100,
89+
eventCounter);
90+
serialNotecard.println(message);
91+
92+
// Delay between simulated measurements
9093
#if myLiveDemo
91-
delay(15*1000); // 15 seconds
94+
delay(15*1000); // 15 seconds
9295
#else
93-
delay(15*60*1000); // 15 minutes
96+
delay(15*60*1000); // 15 minutes
9497
#endif
9598

9699
}

examples/Example1_NotecardBasics/Example1_NotecardBasics.ino

+70-67
Original file line numberDiff line numberDiff line change
@@ -46,104 +46,107 @@
4646
Notecard notecard;
4747

4848
// One-time Arduino initialization
49-
void setup() {
50-
51-
// Set up for debug output. If you open Arduino's serial terminal window, you'll be able to
52-
// watch JSON objects being transferred to and from the Notecard for each request. On most
53-
// Arduino devices, Arduino's serial debug output is on the "Serial" device at 115200.
54-
// If you don't wish to see the Notecard's debug output, or if your device doesn't have
55-
// any debug output port, just comment out these lines by preceding them with //
56-
// Note that the initial 2.5s delay is required by some Arduino cards before debug
57-
// UART output can be successfully displayed in the Arduino IDE, including the
58-
// Adafruit Feather nRF52840 Express.
49+
void setup()
50+
{
51+
52+
// Set up for debug output. If you open Arduino's serial terminal window, you'll be able to
53+
// watch JSON objects being transferred to and from the Notecard for each request. On most
54+
// Arduino devices, Arduino's serial debug output is on the "Serial" device at 115200.
55+
// If you don't wish to see the Notecard's debug output, or if your device doesn't have
56+
// any debug output port, just comment out these lines by preceding them with //
57+
// Note that the initial 2.5s delay is required by some Arduino cards before debug
58+
// UART output can be successfully displayed in the Arduino IDE, including the
59+
// Adafruit Feather nRF52840 Express.
5960
#ifdef serialDebugOut
6061
delay(2500);
6162
serialDebugOut.begin(115200);
6263
notecard.setDebugOutputStream(serialDebugOut);
6364
#endif
6465

65-
// Initialize the physical I/O channel to the Notecard
66+
// Initialize the physical I/O channel to the Notecard
6667
#ifdef serialNotecard
67-
notecard.begin(serialNotecard, 9600);
68+
notecard.begin(serialNotecard, 9600);
6869
#else
69-
Wire.begin();
70+
Wire.begin();
7071

71-
notecard.begin();
72+
notecard.begin();
7273
#endif
7374

74-
// "newRequest()" uses the bundled "J" json package to allocate a "req", which is a JSON object
75-
// for the request to which we will then add Request arguments. The function allocates a "req"
76-
// request structure using malloc() and initializes its "req" field with the type of request.
77-
J *req = notecard.newRequest("hub.set");
75+
// "newRequest()" uses the bundled "J" json package to allocate a "req", which is a JSON object
76+
// for the request to which we will then add Request arguments. The function allocates a "req"
77+
// request structure using malloc() and initializes its "req" field with the type of request.
78+
J *req = notecard.newRequest("hub.set");
7879

79-
// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
80-
// this Product ID. (see above)
81-
JAddStringToObject(req, "product", myProductID);
80+
// This command (required) causes the data to be delivered to the Project on notehub.io that has claimed
81+
// this Product ID. (see above)
82+
JAddStringToObject(req, "product", myProductID);
8283

83-
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
84+
// This command determines how often the Notecard connects to the service. If "continuous" the Notecard
8485
// immediately establishes a session with the service at notehub.io, and keeps it active continuously.
8586
// Because of the power requirements of a continuous connection, a battery powered device would instead
8687
// only sample its sensors occasionally, and would only upload to the service on a periodic basis.
87-
JAddStringToObject(req, "mode", "continuous");
88-
89-
// Issue the request, telling the Notecard how and how often to access the service.
90-
// This results in a JSON message to Notecard formatted like:
91-
// { "req" : "service.set",
92-
// "product" : myProductID,
93-
// "mode" : "continuous"
94-
// }
95-
// Note that sendRequest() always uses free() to release the request data structure, and it
96-
// returns "true" if success and "false" if there is any failure.
97-
notecard.sendRequest(req);
88+
JAddStringToObject(req, "mode", "continuous");
89+
90+
// Issue the request, telling the Notecard how and how often to access the service.
91+
// This results in a JSON message to Notecard formatted like:
92+
// { "req" : "service.set",
93+
// "product" : myProductID,
94+
// "mode" : "continuous"
95+
// }
96+
// Note that sendRequest() always uses free() to release the request data structure, and it
97+
// returns "true" if success and "false" if there is any failure.
98+
notecard.sendRequest(req);
9899
}
99100

100101
// In the Arduino main loop which is called repeatedly, add outbound data every 15 seconds
101-
void loop() {
102-
103-
// Count the simulated measurements that we send to the cloud, and stop the demo before long.
104-
static unsigned eventCounter = 0;
105-
if (eventCounter++ > 25)
106-
return;
107-
108-
// Rather than simulating a temperature reading, use a Notecard request to read the temp
109-
// from the Notecard's built-in temperature sensor. We use requestAndResponse() to indicate
110-
// that we would like to examine the response of the transaction. This method takes a "request" JSON
111-
// data structure as input, then processes it and returns a "response" JSON data structure with
112-
// the response. Note that because the Notecard library uses malloc(), developers must always
113-
// check for NULL to ensure that there was enough memory available on the microcontroller to
114-
// satisfy the allocation request.
115-
double temperature = 0;
102+
void loop()
103+
{
104+
105+
// Count the simulated measurements that we send to the cloud, and stop the demo before long.
106+
static unsigned eventCounter = 0;
107+
if (eventCounter++ > 25) {
108+
return;
109+
}
110+
111+
// Rather than simulating a temperature reading, use a Notecard request to read the temp
112+
// from the Notecard's built-in temperature sensor. We use requestAndResponse() to indicate
113+
// that we would like to examine the response of the transaction. This method takes a "request" JSON
114+
// data structure as input, then processes it and returns a "response" JSON data structure with
115+
// the response. Note that because the Notecard library uses malloc(), developers must always
116+
// check for NULL to ensure that there was enough memory available on the microcontroller to
117+
// satisfy the allocation request.
118+
double temperature = 0;
116119
J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp"));
117120
if (rsp != NULL) {
118121
temperature = JGetNumber(rsp, "value");
119122
notecard.deleteResponse(rsp);
120123
}
121124

122-
// Do the same to retrieve the voltage that is detected by the Notecard on its V+ pin.
123-
double voltage = 0;
125+
// Do the same to retrieve the voltage that is detected by the Notecard on its V+ pin.
126+
double voltage = 0;
124127
rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage"));
125128
if (rsp != NULL) {
126129
voltage = JGetNumber(rsp, "value");
127130
notecard.deleteResponse(rsp);
128131
}
129132

130-
// Enqueue the measurement to the Notecard for transmission to the Notehub, adding the "start"
131-
// flag for demonstration purposes to upload the data instantaneously, so that if you are looking
132-
// at this on notehub.io you will see the data appearing 'live'.)
133+
// Enqueue the measurement to the Notecard for transmission to the Notehub, adding the "start"
134+
// flag for demonstration purposes to upload the data instantaneously, so that if you are looking
135+
// at this on notehub.io you will see the data appearing 'live'.)
133136
J *req = notecard.newRequest("note.add");
134-
if (req != NULL) {
135-
JAddBoolToObject(req, "sync", true);
136-
J *body = JCreateObject();
137-
if (body != NULL) {
138-
JAddNumberToObject(body, "temp", temperature);
139-
JAddNumberToObject(body, "voltage", voltage);
140-
JAddNumberToObject(body, "count", eventCounter);
141-
JAddItemToObject(req, "body", body);
142-
}
143-
notecard.sendRequest(req);
144-
}
145-
146-
// Delay between measurements
147-
delay(15*1000); // 15 seconds
137+
if (req != NULL) {
138+
JAddBoolToObject(req, "sync", true);
139+
J *body = JCreateObject();
140+
if (body != NULL) {
141+
JAddNumberToObject(body, "temp", temperature);
142+
JAddNumberToObject(body, "voltage", voltage);
143+
JAddNumberToObject(body, "count", eventCounter);
144+
JAddItemToObject(req, "body", body);
145+
}
146+
notecard.sendRequest(req);
147+
}
148+
149+
// Delay between measurements
150+
delay(15*1000); // 15 seconds
148151

149152
}

0 commit comments

Comments
 (0)