|
46 | 46 | Notecard notecard; |
47 | 47 |
|
48 | 48 | // 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. |
59 | 60 | #ifdef serialDebugOut |
60 | 61 | delay(2500); |
61 | 62 | serialDebugOut.begin(115200); |
62 | 63 | notecard.setDebugOutputStream(serialDebugOut); |
63 | 64 | #endif |
64 | 65 |
|
65 | | - // Initialize the physical I/O channel to the Notecard |
| 66 | + // Initialize the physical I/O channel to the Notecard |
66 | 67 | #ifdef serialNotecard |
67 | | - notecard.begin(serialNotecard, 9600); |
| 68 | + notecard.begin(serialNotecard, 9600); |
68 | 69 | #else |
69 | | - Wire.begin(); |
| 70 | + Wire.begin(); |
70 | 71 |
|
71 | | - notecard.begin(); |
| 72 | + notecard.begin(); |
72 | 73 | #endif |
73 | 74 |
|
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"); |
78 | 79 |
|
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); |
82 | 83 |
|
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 |
84 | 85 | // immediately establishes a session with the service at notehub.io, and keeps it active continuously. |
85 | 86 | // Because of the power requirements of a continuous connection, a battery powered device would instead |
86 | 87 | // 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); |
98 | 99 | } |
99 | 100 |
|
100 | 101 | // 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; |
116 | 119 | J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp")); |
117 | 120 | if (rsp != NULL) { |
118 | 121 | temperature = JGetNumber(rsp, "value"); |
119 | 122 | notecard.deleteResponse(rsp); |
120 | 123 | } |
121 | 124 |
|
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; |
124 | 127 | rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage")); |
125 | 128 | if (rsp != NULL) { |
126 | 129 | voltage = JGetNumber(rsp, "value"); |
127 | 130 | notecard.deleteResponse(rsp); |
128 | 131 | } |
129 | 132 |
|
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'.) |
133 | 136 | 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 |
148 | 151 |
|
149 | 152 | } |
0 commit comments