Hi Blues team,
I’m having trouble getting an environment variable to update on my Notecard. II’ve tried the following:
- Ensured the variable is set correctly in my Notehub project (under “Device environment variables”).
- Included
sync: true
in myhub.set
request. - Restarted the Notecard using
card.restart
.
However, when I send an env.get
request for the variable, I consistently get an empty {}
response, indicating that the variable is not being found (?)
Here’s the relevant part of my code:
#include <Wire.h>
#include <Notecard.h>
#include <NotecardPseudoSensor.h>
int getSensorInterval();
// Define the serial port for debugging output
#define USB_SERIAL Serial
// Replace with your Notehub project's ProductUID
#define NOTE_PRODUCT_UID "com.gmail.e.danesh:methaneguard_002"
using namespace blues;
// Create a Notecard object
Notecard notecard;
NotecardPseudoSensor sensor(notecard);
// 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 60.
int getSensorInterval() {
int sensorIntervalSeconds = 30; // Default value
J *req = notecard.newRequest("env.get");
if (req != NULL) {
JAddStringToObject(req, "name", "reading_interval");
J *rsp = notecard.requestAndResponse(req);
// Check for errors and the correct type
if (rsp != NULL) {
if (JHasObjectItem(rsp, "err")) {
USB_SERIAL.print("[ERROR] env.get: ");
USB_SERIAL.println(JGetString(rsp, "err"));
} else if (JIsNumber(JGetObjectItem(rsp, "text"))) {
// Successfully retrieved the variable as a number
sensorIntervalSeconds = JGetNumber(rsp, "text");
} else if (JIsString(JGetObjectItem(rsp, "text"))) {
// Handle the case where the variable is a string
USB_SERIAL.println("[WARNING] env.get: 'reading_interval' is a string, not a number.");
} else {
USB_SERIAL.println("[WARNING] env.get: 'text' is not a number or string.");
}
// Print the response for debugging
USB_SERIAL.print("env.get response: ");
USB_SERIAL.println(JConvertToJSONString(rsp));
notecard.deleteResponse(rsp);
} else {
USB_SERIAL.println("[ERROR] env.get: No response received.");
}
} else {
USB_SERIAL.println("[ERROR] env.get: Could not create request.");
}
USB_SERIAL.print("sensorIntervalSeconds: ");
USB_SERIAL.println(sensorIntervalSeconds);
return sensorIntervalSeconds;
}
void setup() {
// Begin serial communication for debugging
USB_SERIAL.begin(115200);
while (!USB_SERIAL); // Wait for serial port to connect
USB_SERIAL.println("Starting...");
// Begin I2C communication with the Notecard
Wire.begin();
notecard.begin();
// notecard.begin(NOTE_I2C_ADDR_DEFAULT, 0, Wire);
// Link the debug output to the USB serial port
notecard.setDebugOutputStream(USB_SERIAL);
// --- Configure Notehub with Error Handling ---
USB_SERIAL.println("Configuring Notehub...");
J *req = notecard.newRequest("hub.set");
if (req != NULL) {
JAddStringToObject(req, "product", NOTE_PRODUCT_UID);
JAddStringToObject(req, "mode", "continuous");
JAddBoolToObject(req, "sync", true); // for immediate inbound sync from notehub
// JAddNumberToObject(req, "inbound", 1); // Enable inbound sync
// JAddNumberToObject(req, "outbound", 1); // Enable outbound sync
notecard.sendRequest(req);
}
// Create new template
req = notecard.newRequest("note.template");
if (req != NULL) {
JAddStringToObject(req, "file", "sensors.qo");
JAddNumberToObject(req, "port", 1); // Added port number
// JAddBoolToObject(req, "sync", true);
J *body = JCreateObject();
if (body != NULL) {
JAddNumberToObject(body, "temp", 14.1);
JAddNumberToObject(body, "humidity", 14.1);
JAddItemToObject(req, "body", body);
notecard.sendRequest(req);
}
}
// delay(1000);
// USB_SERIAL.println("Veryfing template...");
// verifyTemplate();
}
void loop() {
float temperature = sensor.temp();
float humidity = sensor.humidity();
USB_SERIAL.print("Temperature = ");
USB_SERIAL.print(temperature);
USB_SERIAL.println(" *C");
USB_SERIAL.print("Humidity = ");
USB_SERIAL.print(humidity);
USB_SERIAL.println(" %");
J *req = notecard.newRequest("note.add");
if (req != NULL) {
JAddStringToObject(req, "file", "sensors.qo");
// JAddNumberToObject(req, "port", 1);
JAddBoolToObject(req, "sync", true);
J *body = JAddObjectToObject(req, "body");
if (body) {
JAddNumberToObject(body, "temp", temperature);
JAddNumberToObject(body, "humidity", humidity);
}
notecard.sendRequest(req);
}
// delay(60000); // Wait for 60 seconds
int sensorIntervalSeconds = getSensorInterval();
USB_SERIAL.print("Delaying ");
USB_SERIAL.print(sensorIntervalSeconds);
USB_SERIAL.println(" seconds");
delay(sensorIntervalSeconds * 1000);
}
I even tried forced hub.sync and card.restart with up to 10 sec delays with no success. I also include a screenshot of my device Environment:
Please advise if you know where the issue is.
Thanks.