|
| 1 | +// Copyright 2019 Blues Inc. All rights reserved. |
| 2 | +// License <here>. |
| 3 | +// (Use Adafruit BMP085 as a good structural example.) |
| 4 | + |
| 5 | +#include <Notecard.h> |
| 6 | + |
| 7 | +// If the Notecard is connected to a Serial port, define it here. If the Notecard is connected using |
| 8 | +// the I2C interface, leave this commented out. |
| 9 | +//#define serialPort Serial2 |
| 10 | + |
| 11 | +// Tell the Notehub which of its accounts manages this device, and configure how often to upload. |
| 12 | +void setup() { |
| 13 | + |
| 14 | + // Set up for debug output. If you open Arduino's serial terminal window, you'll be able to |
| 15 | + // watch JSON objects being transferred to and from the Notecard for each request. |
| 16 | + Serial.begin(115200); |
| 17 | + NoteSetDebugOutputPort(&Serial); |
| 18 | + |
| 19 | + // Initialize the physical I/O channel to the Notecard |
| 20 | +#ifdef serialPort |
| 21 | + serialPort.begin(9600); |
| 22 | + NoteInitSerial(&serialPort); |
| 23 | +#else |
| 24 | + NoteInitI2C(); |
| 25 | +#endif |
| 26 | + |
| 27 | + // "NoteNewRequest()" uses the bundled "J" json package to allocate a "req", which is a JSON object |
| 28 | + // for the request to which we will then add Request arguments. |
| 29 | + J *req = NoteNewRequest("service.set"); |
| 30 | + |
| 31 | + // This argument causes the data to be delivered to the Personal Project in our notehub.io account. |
| 32 | + JAddStringToObject(req, "product", "YOUR_EMAIL_REGISTERED_WITH_NOTEHUB.IO"); |
| 33 | + |
| 34 | + // This argument causes the modem to power on and connect continuously to the notehub.io service |
| 35 | + JAddStringToObject(req, "mode", "continuous"); |
| 36 | + |
| 37 | + // Issue the request, telling the Notecard how and how often to access the service. |
| 38 | + // This results in a JSON message to Notecard formatted like: |
| 39 | + // { "req" : "service.set", |
| 40 | + // "product" : "[email protected]", |
| 41 | + // "mode" : "continuous" |
| 42 | + // } |
| 43 | + NoteRequest(req); |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +// Repeatedly, get some sensor data from the Notecard's own internal sensors, and enqueue |
| 48 | +// that data as a new Note within a Notefile that will then be synchronized with the service. |
| 49 | +// We can name the JSON fields and the notefile anything we like. We add the ".qo" extension |
| 50 | +// to the Notefile name to indicate to the Notecard that this is an "outbound queue" of Notes. |
| 51 | +void loop() { |
| 52 | + static unsigned messagesSent = 0; |
| 53 | + double temperature, voltage; |
| 54 | + |
| 55 | + // Use the temperature sensor on the Notecard to get a sample temperature measurement |
| 56 | + // This results in a JSON message to Notecard formatted like: |
| 57 | + // { "req" : "card.temp" } |
| 58 | + NoteGetTemperature(&temperature); |
| 59 | + |
| 60 | + // Use the voltage ADC on the Notecard to get a sample voltage measurement |
| 61 | + // This results in a JSON message to Notecard formatted like: |
| 62 | + // { "req" : "card.voltage" } |
| 63 | + NoteGetVoltage(&voltage); |
| 64 | + |
| 65 | + // Enqueue the measurement to the Notecard for transmission to the Notehub, indicating that |
| 66 | + // this is "urgent" so that the data synchronizes immediately rather than being deferred. |
| 67 | + // This results in a JSON message to Notecard formatted like: |
| 68 | + // { "req" : "note.add", |
| 69 | + // "file" : "sensors.qo", |
| 70 | + // "body" : { "temperature" : "<temperature value>", |
| 71 | + // "voltage" : "<voltage value>", |
| 72 | + // "count" : "<message number>" |
| 73 | + // }, |
| 74 | + // "start" : true |
| 75 | + // } |
| 76 | + J *body = JCreateObject(); |
| 77 | + JAddNumberToObject(body, "temperature", temperature); |
| 78 | + JAddNumberToObject(body, "voltage", voltage); |
| 79 | + JAddNumberToObject(body, "count", ++messagesSent); |
| 80 | + NoteSend("sensors.qo", body, true); |
| 81 | + |
| 82 | + // Delay between measurements |
| 83 | + delay(15000); |
| 84 | + |
| 85 | +} |
0 commit comments