Created
November 15, 2023 21:47
-
-
Save rdlauer/4f50bbb44a1b10100a0bc62b7cc820b2 to your computer and use it in GitHub Desktop.
Example of downloading/uploading binary data with a Blues Notecard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Wire.h> | |
#include <Notecard.h> | |
#include <SD.h> | |
Notecard notecard; | |
File myImageFile; | |
Sd2Card card; | |
SdVolume volume; | |
SdFile root; | |
const int chipSelect = A3; | |
HardwareSerial stlinkSerial(PIN_VCP_RX, PIN_VCP_TX); | |
#define usbSerial Serial | |
int ledPin = LED_BUILTIN; // references onboard LED on Swan | |
void setup() | |
{ | |
pinMode(ledPin, OUTPUT); | |
Wire.begin(); | |
// using STLINK debugger | |
stlinkSerial.begin(115200); | |
const size_t usb_timeout_ms = 5000; | |
for (const size_t start_ms = millis(); !stlinkSerial && (millis() - start_ms) < usb_timeout_ms;) | |
; | |
notecard.setDebugOutputStream(stlinkSerial); | |
notecard.begin(); | |
J *req = notecard.newRequest("hub.set"); | |
JAddStringToObject(req, "product", "com.your.project:uid"); | |
JAddStringToObject(req, "mode", "continuous"); | |
JAddBoolToObject(req, "sync", true); | |
notecard.sendRequestWithRetry(req, 5); | |
// wait until we are connected to notehub | |
bool connected = false; | |
while (!connected) | |
{ | |
req = notecard.newRequest("hub.status"); | |
J *rsp = notecard.requestAndResponse(req); | |
connected = JGetBool(rsp, "connected"); | |
delay(2000); | |
} | |
// Reset the Notecard's binary storage area, so we can start fresh | |
NoteBinaryStoreReset(); | |
// init the SD card for usage | |
stlinkSerial.println("Initializing SD card..."); | |
if (!card.init(SPI_HALF_SPEED, chipSelect)) | |
{ | |
stlinkSerial.println("SD card initialization failed!"); | |
while (1) | |
; | |
} | |
// try to open the 'volume'/'partition' - it should be FAT16 or FAT32 | |
if (!volume.init(card)) | |
{ | |
stlinkSerial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); | |
while (1) | |
; | |
} | |
// ########################## | |
// START INBOUND FILE EXAMPLE | |
// ########################## | |
if (J *req = NoteNewRequest("web.get")) | |
{ | |
JAddStringToObject(req, "route", "GetImage"); | |
JAddBoolToObject(req, "binary", true); | |
if (!NoteRequest(req)) | |
{ | |
notecard.logDebug("Error receiving image\n"); | |
} | |
uint32_t data_len = 0; | |
NoteBinaryStoreDecodedLength(&data_len); | |
// Create a buffer to receive the entire data store. This buffer must be | |
// large enough to hold the encoded data that will be transferred from | |
// the Notecard, as well as the terminating newline. | |
// `NoteBinaryMaxEncodedLength()` will compute the worst-case size of | |
// the encoded length plus the byte required for the newline terminator. | |
uint32_t rx_buffer_len = NoteBinaryCodecMaxEncodedLength(data_len); | |
uint8_t *rx_buffer = (uint8_t *)malloc(rx_buffer_len); | |
// Receive the data | |
NoteBinaryStoreReceive(reinterpret_cast<uint8_t *>(rx_buffer), rx_buffer_len, 0, data_len); | |
notecard.logDebugf("\n[INFO] Received %d bytes.\n", data_len); | |
// save the buffer to the specified file name on the SD card | |
SD.begin(chipSelect); | |
SD.remove("logo.jpg"); // remove the file if it already exists | |
myImageFile = SD.open("logo.jpg", FILE_WRITE); | |
if (myImageFile) | |
{ | |
myImageFile.write(rx_buffer, data_len); | |
myImageFile.close(); | |
stlinkSerial.println("Completed writing the file!\n"); | |
} | |
// ########################### | |
// START OUTBOUND FILE EXAMPLE | |
// ########################### | |
const uint32_t notecard_binary_area_offset = 0; | |
NoteBinaryStoreTransmit(reinterpret_cast<uint8_t *>(rx_buffer), data_len, sizeof(rx_buffer), notecard_binary_area_offset); | |
notecard.logDebugf("\n[INFO] Transmitted %d bytes.\n", data_len); | |
// Send the binary data to Notehub | |
{ | |
// Submit binary object to the Notehub using `note.add`. This will send | |
// the binary to Notehub in the `payload` field of the Note. The payload | |
// will not be visible in the Notehub UI, but the data will be forwarded | |
// to any pre-configured routes. | |
if (J *req = notecard.newRequest("note.add")) | |
{ | |
JAddStringToObject(req, "file", "binary.qo"); | |
JAddBoolToObject(req, "binary", true); | |
JAddBoolToObject(req, "live", true); | |
if (!notecard.sendRequest(req)) | |
{ | |
// The binary data store is cleared on successful transmission, | |
// but we need to reset it manually if the request failed. | |
NoteBinaryStoreReset(); | |
} | |
} | |
} | |
// Free the receive buffer | |
free(rx_buffer); | |
stlinkSerial.println("Completed sending the file!\n"); | |
} | |
} | |
void loop() | |
{ | |
// Finally, blink the Swan's LED when done! | |
digitalWrite(ledPin, HIGH); | |
delay(1000); | |
digitalWrite(ledPin, LOW); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment