|
| 1 | +// Copyright 2023 Blues Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by licenses granted by the |
| 4 | +// copyright holder including that found in the LICENSE file. |
| 5 | +// |
| 6 | +// This example exercises the Notecard's ability to send and receive a binary |
| 7 | + |
| 8 | +#include <stddef.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +#include <Notecard.h> |
| 14 | + |
| 15 | +// Note that both of these definitions are optional; just prefix either line |
| 16 | +// with `//` to remove it. |
| 17 | +// - Remove txRxPinsSerial if you wired your Notecard using I2C SDA/SCL pins |
| 18 | +// instead of serial RX/TX |
| 19 | +// - Remove usbSerial if you don't want the Notecard library to output debug |
| 20 | +// information |
| 21 | + |
| 22 | +// #define txRxPinsSerial Serial1 |
| 23 | +#define usbSerial Serial |
| 24 | + |
| 25 | +// This is the unique Product Identifier for your device |
| 26 | +#ifndef PRODUCT_UID |
| 27 | +#define PRODUCT_UID "" // "com.my-company.my-name:my-project" |
| 28 | +#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" |
| 29 | +#endif |
| 30 | + |
| 31 | +#define myProductID PRODUCT_UID |
| 32 | +Notecard notecard; |
| 33 | + |
| 34 | +// One-time Arduino initialization |
| 35 | +void setup() |
| 36 | +{ |
| 37 | + // Set up for debug output (if available). |
| 38 | +#ifdef usbSerial |
| 39 | + // If you open Arduino's serial terminal window, you'll be able to watch |
| 40 | + // JSON objects being transferred to and from the Notecard for each request. |
| 41 | + usbSerial.begin(115200); |
| 42 | + const size_t usb_timeout_ms = 3000; |
| 43 | + for (const size_t start_ms = millis(); !usbSerial && (millis() - start_ms) < usb_timeout_ms;) |
| 44 | + ; |
| 45 | + notecard.setDebugOutputStream(usbSerial); |
| 46 | +#endif |
| 47 | + |
| 48 | + // Initialize the physical I/O channel to the Notecard |
| 49 | +#ifdef txRxPinsSerial |
| 50 | + notecard.begin(txRxPinsSerial, 9600); |
| 51 | +#else |
| 52 | + notecard.begin(); |
| 53 | +#endif |
| 54 | + |
| 55 | + // Configure the productUID, and instruct the Notecard to stay connected to |
| 56 | + // the service. |
| 57 | + J *req = notecard.newRequest("hub.set"); |
| 58 | + if (myProductID[0]) |
| 59 | + { |
| 60 | + JAddStringToObject(req, "product", myProductID); |
| 61 | + } |
| 62 | + JAddStringToObject(req, "mode", "continuous"); |
| 63 | + notecard.sendRequestWithRetry(req, 5); // 5 seconds |
| 64 | + |
| 65 | + // Reset the state of the Notecard's binary data store to a known value. |
| 66 | + NoteBinaryReset(); |
| 67 | +} |
| 68 | + |
| 69 | +// In the Arduino main loop which is called repeatedly, add outbound data every |
| 70 | +// 30 seconds |
| 71 | +void loop() |
| 72 | +{ |
| 73 | + // Stop the demo after five iterations to conserve data |
| 74 | + static unsigned event_counter = 0; |
| 75 | + if (++event_counter > 5) |
| 76 | + { |
| 77 | + notecard.logDebug("Demo cycle complete. Program stopped. Press RESET to restart."); |
| 78 | + delay(10000); // 10 seconds |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Send data to the Notecard storage and pull it back |
| 83 | + { |
| 84 | + ///////////////////////////////////////////////// |
| 85 | + // Transmit that beautiful bean footage |
| 86 | + ///////////////////////////////////////////////// |
| 87 | + char data[64] = "https://youtu.be/0epWToAOlFY?t=21"; |
| 88 | + size_t data_len = strlen(data); |
| 89 | + const size_t notecard_binary_area_offset = 0; |
| 90 | + NoteBinaryTransmit(reinterpret_cast<uint8_t *>(data), data_len, sizeof(data), notecard_binary_area_offset); |
| 91 | + notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len); |
| 92 | + |
| 93 | + // Log for the sake of curiosity |
| 94 | + notecard.logDebug("\n*** Encoded Binary Transmission ***\n"); |
| 95 | + size_t tx_len = NoteBinaryEncodedLength(reinterpret_cast<uint8_t *>(data), data_len); |
| 96 | + for (size_t i = 0 ; i < tx_len ; ++i) { |
| 97 | + notecard.logDebugf("%02x ", data[i]); |
| 98 | + if ((i + 1) % 16 == 0) { |
| 99 | + notecard.logDebug("\n"); |
| 100 | + } |
| 101 | + } |
| 102 | + notecard.logDebug("\n*** Encoded Binary Transmission ***\n\n"); |
| 103 | + |
| 104 | + ///////////////////////////////////////////////// |
| 105 | + // Receive data from the Notecard binary data store |
| 106 | + ///////////////////////////////////////////////// |
| 107 | + size_t rx_buffer_len = 0; |
| 108 | + NoteBinaryRequiredRxMaxBuffer(&rx_buffer_len); |
| 109 | + uint8_t *rx_buffer = (uint8_t *)malloc(rx_buffer_len); |
| 110 | + data_len = NOTE_C_BINARY_RX_ALL; // NOTE_C_BINARY_RX_ALL is a special value |
| 111 | + // meaning "return all bytes from offset" |
| 112 | + NoteBinaryReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, notecard_binary_area_offset, &data_len); |
| 113 | + notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len); |
| 114 | + |
| 115 | + // Display received buffer |
| 116 | + notecard.logDebug("\n*** Decoded Data ***\n"); |
| 117 | + for (size_t i = 0 ; i < data_len ; ++i) { |
| 118 | + notecard.logDebugf("%c", rx_buffer[i]); |
| 119 | + } |
| 120 | + notecard.logDebug("\n*** Decoded Data ***\n\n"); |
| 121 | + |
| 122 | + // NOTE: The binary data store is not cleared on receive, which |
| 123 | + // allows us to submit it to Notehub in the next step. |
| 124 | + } |
| 125 | + |
| 126 | + // Send it to Notehub |
| 127 | + { |
| 128 | + // Submit binary object to the Notehub using `note.add`. This will send |
| 129 | + // the binary to Notehub in the payload field of the Note. The payload |
| 130 | + // will not be visible in the Notehub UI, but the data will be forwarded |
| 131 | + // to any pre-configured routes. |
| 132 | + if (J *req = notecard.newRequest("note.add")) |
| 133 | + { |
| 134 | + JAddStringToObject(req, "file", "cobs.qo"); |
| 135 | + JAddBoolToObject(req, "binary", true); |
| 136 | + JAddBoolToObject(req, "live", true); |
| 137 | + if (!notecard.sendRequest(req)) { |
| 138 | + // The binary data store is cleared on successful transmission, |
| 139 | + // but we need to reset it manually if the request failed. |
| 140 | + NoteBinaryReset(); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Delay between sends |
| 146 | + delay(30 * 1000); // 30 seconds |
| 147 | +} |
0 commit comments