Skip to content

Commit

Permalink
Added a second message carrying device & comms info.
Browse files Browse the repository at this point in the history
Removed support for sending MQTT messages from modem filesystem - that needs more work.
Added constants for CLI command results.
Fixed c1 pwr 0 command so it issues AT+CPWROFF before removing power.
Added sd rm, c1 rm, c1 ls commands.
Bumped version to 1.0.7.

Moved voltage readings to 2nd message, simplified network registration wait loop and extended wait time to 90s.
  • Loading branch information
dajtxx committed Oct 5, 2023
1 parent 55a9d13 commit 535d9f6
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 137 deletions.
2 changes: 1 addition & 1 deletion firmware/wombat/gen_version_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

# NOTE: This is the canonical definition of the firmware version. This is baked into the firmware
# and placed into the wombat.sha1 file.
_VERSION_NUM = '1.0.6'
_VERSION_NUM = '1.0.7'
_VERSION_H = 'include/version.h'

commit_const = 'const char* commit_id = "unknown";'
Expand Down
4 changes: 4 additions & 0 deletions firmware/wombat/include/cli/CLI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#define CLI_TAG "cli"

const char OK_RESPONSE[] = "\r\nOK\r\n";
const char ERROR_RESPONSE[] = "\r\nERROR\r\n";
const char INVALID_CMD_RESPONSE[] = "\r\nERROR: Invalid command\r\n";

/**
* @brief FreeRTOS command line interface.
*
Expand Down
2 changes: 1 addition & 1 deletion firmware/wombat/src/CAT_M1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool CAT_M1::make_ready() {
r5.autoTimeZoneForBegin(true);

r5.enableAtDebugging();
//r5.enableDebugging();
r5.enableDebugging();

// This is relatively benign - it enables the network indicator GPIO pin, set error message format, etc.
// It does close all open sockets, but there should not be any open sockets at this point so that is ok.
Expand Down
100 changes: 69 additions & 31 deletions firmware/wombat/src/SensorTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,55 +170,38 @@ void sensor_task(void) {

const char *timestamp = iso8601();
msg["timestamp"] = timestamp;

//---------------------
// source_ids section
//---------------------
JsonObject source_ids = msg.createNestedObject("source_ids");
source_ids["serial_no"] = DeviceConfig::get().node_id;

constexpr size_t buffer_sz = 48;
char buffer[buffer_sz + 1];
memset(buffer, 0, sizeof(buffer));
// This format helps keep the message under 1024 bytes when there are a lot of SDI-12 sensors attached.
snprintf(buffer, buffer_sz, "%d.%d.%d %s %s %s", ver_major, ver_minor, ver_update, repo_branch, commit_id, repo_status);
source_ids["firmware"] = buffer;
JsonArray timeseries_array = msg.createNestedArray("timeseries");

//
// Node sensors
//
JsonObject ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "battery (v)";
ts_entry["value"] = BatteryMonitor::get_voltage();

ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "solar (v)";
ts_entry["value"] = SolarMonitor::get_voltage();

if (r5_ok) {
signal_quality sq;
SARA_R5_error_t r5_err = r5.getExtSignalQuality(sq);

if (!r5_err) {
ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "rsrq";
ts_entry["value"] = sq.rsrq;

ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "rsrp";
ts_entry["value"] = sq.rsrp;
}

String ccid = r5.getCCID();
if (ccid.length() > 0) {
source_ids["ccid"] = ccid;
}
}

//---------------------
// timeseries section
//---------------------
JsonArray timeseries_array = msg.createNestedArray("timeseries");

//
// Pulse counter
//
uint32_t pc = get_pulse_count();
uint32_t sp = get_shortest_pulse();

ts_entry = timeseries_array.createNestedObject();
JsonObject ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "pulse_count";
ts_entry["value"] = pc;

Expand All @@ -240,6 +223,61 @@ void sensor_task(void) {

sdi12.end();

String str;
serializeJson(msg, str);
ESP_LOGI(TAG, "Msg:\r\n%s\r\n", str.c_str());

if (spiffs_ok) {
// Write the message to a file so it can be sent on the next uplink cycle.
static const size_t MAX_FNAME = 32;
static char filename[MAX_FNAME + 1];

snprintf(filename, MAX_FNAME, "/%s%s.json", DeviceConfig::getMsgFilePrefix(), timestamp);
ESP_LOGI(TAG, "Creating unsent msg file [%s]", filename);
File f = SPIFFS.open(filename, FILE_WRITE);
serializeJson(msg, f);
f.close();
} else {
log_to_sdcard("[E] spiffs_ok is false, no message stored");
}

// Append the message to a file on the SD card.
if (SDCardInterface::is_ready()) {
snprintf(g_buffer, MAX_G_BUFFER, "%s,\n", str.c_str());
SDCardInterface::append_to_file(sd_card_datafile_name, g_buffer);
}

//---------------------
// Wombat status/info message
//---------------------

timeseries_array.clear();
//
// Node sensors
//
ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "battery (v)";
ts_entry["value"] = BatteryMonitor::get_voltage();

ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "solar (v)";
ts_entry["value"] = SolarMonitor::get_voltage();

if (r5_ok) {
signal_quality sq;
SARA_R5_error_t r5_err = r5.getExtSignalQuality(sq);

if (!r5_err) {
ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "rsrq";
ts_entry["value"] = sq.rsrq;

ts_entry = timeseries_array.createNestedObject();
ts_entry["name"] = "rsrp";
ts_entry["value"] = sq.rsrp;
}
}

if (SDCardInterface::is_ready()) {
auto total_mb = SD.cardSize() / 1024 / 1024;
auto used_mb = SD.usedBytes() / 1024 / 1024;
Expand All @@ -253,7 +291,7 @@ void sensor_task(void) {
ts_entry["value"] = total_mb - used_mb;
}

String str;
str.clear();
serializeJson(msg, str);
ESP_LOGI(TAG, "Msg:\r\n%s\r\n", str.c_str());

Expand All @@ -262,7 +300,7 @@ void sensor_task(void) {
static const size_t MAX_FNAME = 32;
static char filename[MAX_FNAME + 1];

snprintf(filename, MAX_FNAME, "/%s%s.json", DeviceConfig::getMsgFilePrefix(), timestamp);
snprintf(filename, MAX_FNAME, "/%sinfo_%s.json", DeviceConfig::getMsgFilePrefix(), timestamp);
ESP_LOGI(TAG, "Creating unsent msg file [%s]", filename);
File f = SPIFFS.open(filename, FILE_WRITE);
serializeJson(msg, f);
Expand All @@ -273,7 +311,7 @@ void sensor_task(void) {

// Append the message to a file on the SD card.
if (SDCardInterface::is_ready()) {
snprintf(g_buffer, MAX_G_BUFFER, "%s,\n", str.c_str());
SDCardInterface::append_to_file(sd_card_datafile_name, g_buffer);
str += ",\n";
SDCardInterface::append_to_file(sd_card_datafile_name, str.c_str());
}
}
30 changes: 16 additions & 14 deletions firmware/wombat/src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,22 +402,24 @@ bool connect_to_internet(void) {

// Network registration takes 4 seconds at best.
ESP_LOGI(TAG, "Waiting for network registration");
for (int i = 0; i < 3; i++) {
int attempts = 0;
while (reg_status != SARA_R5_REGISTRATION_HOME && attempts < 4) {
reg_status = r5.registration();
delay(20);
if (reg_status == SARA_R5_REGISTRATION_INVALID) {
ESP_LOGI(TAG, "ESP registration query failed");
log_to_sdcard("[E] ESP registration query failed");
return false;
}
int attempts = 0;
while (reg_status != SARA_R5_REGISTRATION_HOME && attempts < 45) {
reg_status = r5.registration();
delay(20);
if (reg_status == SARA_R5_REGISTRATION_INVALID) {
ESP_LOGI(TAG, "ESP registration query failed");
log_to_sdcard("[E] ESP registration query failed");
return false;
}

ESP_LOGI(TAG, "ESP registration status = %d", reg_status);
r5.bufferedPoll();
delay(2000);
attempts++;
ESP_LOGI(TAG, "ESP registration status = %d", reg_status);
r5.bufferedPoll();
if (reg_status == SARA_R5_REGISTRATION_HOME) {
break;
}

delay(2000);
attempts++;
}

if (reg_status != SARA_R5_REGISTRATION_HOME) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "cli/device_config/acquisition_intervals.h"
#include "globals.h"
#include "Utils.h"
#include "cli/CLI.h"

#define TAG "acquisition_intervals"

Expand Down Expand Up @@ -126,7 +127,7 @@ BaseType_t CLIConfigIntervals::enter_cli(char *pcWriteBuffer,

config.setMeasureInterval(i);
if (i == config.getMeasureInterval()) {
strncpy(pcWriteBuffer, "OK\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, OK_RESPONSE, xWriteBufferLen - 1);
} else {
strncpy(pcWriteBuffer, "ERROR: set measure interval "
"failed\r\n", xWriteBufferLen - 1);
Expand All @@ -152,7 +153,7 @@ BaseType_t CLIConfigIntervals::enter_cli(char *pcWriteBuffer,

config.setUplinkInterval(i);
if (i == config.getUplinkInterval()) {
strncpy(pcWriteBuffer, "OK\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, OK_RESPONSE, xWriteBufferLen - 1);
} else {
strncpy(pcWriteBuffer, "ERROR: set uplink interval "
"failed\r\n", xWriteBufferLen - 1);
Expand All @@ -175,7 +176,7 @@ BaseType_t CLIConfigIntervals::enter_cli(char *pcWriteBuffer,

config.setSleepAdjustment(sleepMultiplier);
if (sleepMultiplier == config.getSleepAdjustment()) {
strncpy(pcWriteBuffer, "OK\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, OK_RESPONSE, xWriteBufferLen - 1);
} else {
ESP_LOGE(TAG, "%f != %f", sleepMultiplier, config.getSleepAdjustment());
strncpy(pcWriteBuffer, "ERROR: set clock multiplier failed\r\n", xWriteBufferLen - 1);
Expand All @@ -188,6 +189,6 @@ BaseType_t CLIConfigIntervals::enter_cli(char *pcWriteBuffer,
}
}

strncpy(pcWriteBuffer, "Syntax error\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, INVALID_CMD_RESPONSE, xWriteBufferLen - 1);
return pdFALSE;
}
3 changes: 2 additions & 1 deletion firmware/wombat/src/cli/device_config/cli_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <StreamString.h>

#include "cli/FreeRTOS_CLI.h"
#include "cli/CLI.h"

#include "power_monitoring/battery.h"
#include "power_monitoring/solar.h"
Expand Down Expand Up @@ -72,6 +73,6 @@ BaseType_t CLIPower::enter_cli(char *pcWriteBuffer, size_t xWriteBufferLen,
}
}

strncpy(pcWriteBuffer, "ERROR: Invalid command\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, INVALID_CMD_RESPONSE, xWriteBufferLen - 1);
return pdFALSE;
}
15 changes: 8 additions & 7 deletions firmware/wombat/src/cli/device_config/config_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <StreamString.h>

#include "cli/FreeRTOS_CLI.h"
#include "cli/CLI.h"
#include "cli/device_config/config_cli.h"
#include "CAT_M1.h"
#include "Utils.h"
Expand Down Expand Up @@ -74,14 +75,14 @@ BaseType_t CLIConfig::enter_cli(char *pcWriteBuffer, size_t xWriteBufferLen,
if (!strncmp("list", param, paramLen)) {
response_buffer_.clear();
config.dumpConfig(response_buffer_);
response_buffer_.println("\r\nOK");
response_buffer_.print(OK_RESPONSE);
return pdTRUE;
}

if (!strncmp("load", param, paramLen)) {
config.load();
config.dumpConfig(response_buffer_);
response_buffer_.println("\r\nOK");
response_buffer_.print(OK_RESPONSE);
return pdTRUE;
}

Expand Down Expand Up @@ -138,9 +139,9 @@ BaseType_t CLIConfig::enter_cli(char *pcWriteBuffer, size_t xWriteBufferLen,
}

if (success) {
strncpy(pcWriteBuffer, "\r\nOK\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, OK_RESPONSE, xWriteBufferLen - 1);
} else {
strncpy(pcWriteBuffer, "\r\nERROR\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, ERROR_RESPONSE, xWriteBufferLen - 1);
}
return pdFALSE;
}
Expand All @@ -157,9 +158,9 @@ BaseType_t CLIConfig::enter_cli(char *pcWriteBuffer, size_t xWriteBufferLen,
}

if (success) {
strncpy(pcWriteBuffer, "\r\nOK\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, OK_RESPONSE, xWriteBufferLen - 1);
} else {
strncpy(pcWriteBuffer, "\r\nERROR\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, ERROR_RESPONSE, xWriteBufferLen - 1);
}
return pdFALSE;
}
Expand All @@ -175,6 +176,6 @@ BaseType_t CLIConfig::enter_cli(char *pcWriteBuffer, size_t xWriteBufferLen,
return pdTRUE;
}

strncpy(pcWriteBuffer, "ERROR: Invalid command\r\n", xWriteBufferLen - 1);
strncpy(pcWriteBuffer, INVALID_CMD_RESPONSE, xWriteBufferLen - 1);
return pdFALSE;
}
Loading

0 comments on commit 535d9f6

Please sign in to comment.