Last active
March 12, 2024 19:33
-
-
Save tjvantoll/031b06bb3811b51d941f076e704d3aa5 to your computer and use it in GitHub Desktop.
Host firmware that communicates with the Notecard and works on cellular, Wi-Fi, and LoRa. See https://dev.blues.io/blog/firmware-cellular-wifi-lora/ for details on how to use this firmware for your projects.
This file contains hidden or 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 <Notecard.h> | |
#define usbSerial Serial | |
// See https://dev.blues.io/notehub/notehub-walkthrough/#finding-a-productuid | |
#define PRODUCT_UID "<your-product-uid>" | |
#define WIFI_SSID "<your-ssid>" | |
#define WIFI_PASSWORD "<your-password>" | |
Notecard notecard; | |
void setup() | |
{ | |
static const size_t MAX_SERIAL_WAIT_MS = 5000; | |
size_t begin_serial_wait_ms = ::millis(); | |
// Wait for the serial port to become available | |
while (!usbSerial && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))); | |
usbSerial.begin(115200); | |
notecard.setDebugOutputStream(usbSerial); | |
notecard.begin(); | |
if (J *req = notecard.newRequest("card.version")) | |
{ | |
J* rsp = notecard.requestAndResponse(req); | |
usbSerial.print("Notecard SKU: "); | |
usbSerial.println(JGetString(rsp, "sku")); | |
notecard.deleteResponse(rsp); | |
} | |
// For ease of development only. In production scenarios we recommend | |
// using the Notecard’s AP button to perform Wi-Fi setup. | |
// See https://dev.blues.io/guides-and-tutorials/notecard-guides/connecting-to-a-wi-fi-access-point/#using-your-smartphone | |
if (J *req = notecard.newRequest("card.wifi")) | |
{ | |
JAddStringToObject(req, "ssid", WIFI_SSID); | |
JAddStringToObject(req, "password", WIFI_PASSWORD); | |
if (!notecard.sendRequest(req)) | |
{ | |
usbSerial.println("Notecard does not support Wi-Fi connectivity."); | |
} | |
} | |
if (J *req = notecard.newRequest("hub.set")) | |
{ | |
JAddStringToObject(req, "product", PRODUCT_UID); | |
JAddStringToObject(req, "mode", "periodic"); | |
JAddNumberToObject(req, "outbound", 60); | |
JAddNumberToObject(req, "inbound", 60 * 4); | |
notecard.sendRequest(req); | |
} | |
if (J *req = notecard.newRequest("note.template")) | |
{ | |
JAddStringToObject(req, "file", "sensors.qo"); | |
JAddNumberToObject(req, "port", 1); | |
JAddStringToObject(req, "format", "compact"); | |
J *body = JAddObjectToObject(req, "body"); | |
JAddNumberToObject(body, "temp", 14.1); | |
JAddNumberToObject(body, "_time", 14); | |
notecard.sendRequest(req); | |
} | |
if (J *req = notecard.newRequest("env.template")) | |
{ | |
J *body = JAddObjectToObject(req, "body"); | |
JAddNumberToObject(body, "reading_interval", 12); | |
notecard.sendRequest(req); | |
} | |
} | |
float getTemperature() | |
{ | |
float temp = 0; | |
J *req = notecard.newRequest("card.temp"); | |
if (J *rsp = notecard.requestAndResponse(req)) | |
{ | |
temp = JGetNumber(rsp, "value"); | |
notecard.deleteResponse(rsp); | |
} | |
return temp; | |
} | |
// This function assumes you’ll set the reading_interval environment variable to | |
// a positive integer. If the variable is not set, set to 0, or set to an invalid | |
// type, this function returns a default value of 30. | |
int getSensorInterval() | |
{ | |
int sensorIntervalMinutes = 30; | |
if (J *req = notecard.newRequest("env.get")) | |
{ | |
JAddStringToObject(req, "name", "reading_interval"); | |
J* rsp = notecard.requestAndResponse(req); | |
int readingIntervalEnvVar = atoi(JGetString(rsp, "text")); | |
if (readingIntervalEnvVar > 0) { | |
sensorIntervalMinutes = readingIntervalEnvVar; | |
} | |
notecard.deleteResponse(rsp); | |
} | |
return sensorIntervalMinutes; | |
} | |
void loop() | |
{ | |
float temperature = getTemperature(); | |
usbSerial.print("Temperature = "); | |
usbSerial.print(temperature); | |
usbSerial.println(" *C"); | |
if (J *req = notecard.newRequest("note.add")) | |
{ | |
JAddStringToObject(req, "file", "sensors.qo"); | |
J *body = JAddObjectToObject(req, "body"); | |
JAddNumberToObject(body, "temp", temperature); | |
notecard.sendRequest(req); | |
} | |
int sensorIntervalMinutes = getSensorInterval(); | |
usbSerial.print("Delaying "); | |
usbSerial.print(sensorIntervalMinutes); | |
usbSerial.println(" minutes"); | |
delay(sensorIntervalMinutes * 60 * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment