Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
namespace config {

// Debug control --------------------------------------------------------------
#ifdef DEBUG_SERIAL
constexpr bool EnableDebug = true;
#else
constexpr bool EnableDebug = false;
#endif
// Debug is now compile-time disabled for optimized builds
constexpr bool EnableDebug = false;

// SX127x / LMIC wiring -------------------------------------------------------
constexpr int PinLmicNss = 18;
Expand All @@ -30,12 +27,12 @@ constexpr int PinLmicDio1 = 33;
constexpr int PinLmicDio2 = 32;

// LoRaWAN / payload ----------------------------------------------------------
constexpr int MaxPayloadSize = 200; // Increased to accommodate OTA messages
constexpr int MaxPayloadSize = 200; // Increased to accommodate OTA messages

// Sensors --------------------------------------------------------------------
constexpr int SoilSensorPin = 34; // ADC1_CH6
constexpr int AirValue = 2200; // Dry calibration value
constexpr int WaterValue = 380; // Wet calibration value
constexpr int SoilSensorPin = 34; // ADC1_CH6
constexpr int AirValue = 2200; // Dry calibration value
constexpr int WaterValue = 380; // Wet calibration value

// Misc -----------------------------------------------------------------------
constexpr int MaxSensorRead = 1;
Expand All @@ -44,10 +41,9 @@ constexpr int MaxSensorRead = 1;
// The FIRMWARE_VERSION is set by the CI/CD pipeline during compilation
// Format: 3-digit integer (e.g., 100 for v1.0.0, 110 for v1.1.0, 112 for v1.1.2)
#ifdef FIRMWARE_VERSION
constexpr int FirmwareVersionInt = static_cast<int>(FIRMWARE_VERSION);
constexpr int FirmwareVersionInt = static_cast<int>(FIRMWARE_VERSION);
#else
constexpr int FirmwareVersionInt = 0; // Development build
constexpr int FirmwareVersionInt = 0; // Development build
#endif

} // namespace config

} // namespace config
28 changes: 19 additions & 9 deletions src/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

#include "config.hpp"

#ifdef DEBUG_SERIAL
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(fmt, ...) Serial.printf(fmt, __VA_ARGS__)
#define DEBUG_BEGIN(baud) Serial.begin(baud)
// Debug macros are now compile-time disabled for optimized builds
// These macros compile to nothing, ensuring zero runtime overhead
#if 0 // Always disabled for production builds
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(fmt, ...) Serial.printf(fmt, __VA_ARGS__)
#define DEBUG_BEGIN(baud) Serial.begin(baud)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTF(fmt, ...)
#define DEBUG_BEGIN(baud)
#define DEBUG_PRINT(x) \
do { \
} while (0)
#define DEBUG_PRINTLN(x) \
do { \
} while (0)
#define DEBUG_PRINTF(fmt, ...) \
do { \
} while (0)
#define DEBUG_BEGIN(baud) \
do { \
} while (0)
#endif
177 changes: 86 additions & 91 deletions src/lorawan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@
#include "lorawan_settings.hpp"
#include "utils.hpp"
#include "ota.hpp"
#include "debug.hpp"
#include <cstring>
#include <cctype>
#include "lorawan_settings.hpp"
#include <Adafruit_MAX1704X.h>
#include <array>

// PROGMEM string constants for LoRaWAN
const char PROGMEM msg_lmic_opmode[] = "LMIC.opmode: ";
const char PROGMEM msg_lmic_seqno_up[] = "LMIC.seqnoUp = ";
const char PROGMEM msg_lmic_global_duty_rate[] = "LMIC.globalDutyRate = ";
const char PROGMEM msg_lmic_opmode[] = "LMIC.opmode: ";
const char PROGMEM msg_lmic_seqno_up[] = "LMIC.seqnoUp = ";
const char PROGMEM msg_lmic_global_duty_rate[] = "LMIC.globalDutyRate = ";
const char PROGMEM msg_lmic_global_duty_avail[] = "LMIC.globalDutyAvail = ";
const char PROGMEM msg_lmic_band_plan_next_tx[] = "LMICbandplan_nextTx = ";
const char PROGMEM msg_os_get_time[] = "os_getTime = ";
const char PROGMEM msg_lmic_txend[] = "LMIC.txend = ";
const char PROGMEM msg_lmic_txchnl[] = "LMIC.txChnl = ";
const char PROGMEM msg_lmic_version[] = "LMIC: ";
const char PROGMEM msg_oxticks[] = " osTicks, ";
const char PROGMEM msg_sec[] = " sec";
const char PROGMEM msg_separator[] = "-----";
const char PROGMEM msg_do_send[] = "do_send";
const char PROGMEM msg_lmic_opmode_equals[] = "LMIC.opmode= ";
const char PROGMEM msg_app_eui[] = "app_eui: ";
const char PROGMEM msg_dev_eui[] = "dev_eui: ";
const char PROGMEM msg_app_key[] = "app_key: ";
const char PROGMEM msg_charge_rate[] = "----ChargeRate: ";
const char PROGMEM msg_x_format[] = "X: %f";
const char PROGMEM msg_moisture_format[] = "Moisture ADC: %f, Moisture Percentage: %f, vBat %f\n\n";
const char PROGMEM msg_error_app_eui[] = "ERROR: app_eui string missing or too short";
const char PROGMEM msg_error_app_eui_hex[] = "ERROR: app_eui contains non-hex digits";
const char PROGMEM msg_error_dev_eui[] = "ERROR: dev_eui string missing or too short";
const char PROGMEM msg_error_dev_eui_hex[] = "ERROR: dev_eui contains non-hex digits";
const char PROGMEM msg_error_app_key[] = "ERROR: app_key string missing or too short";
const char PROGMEM msg_error_app_key_hex[] = "ERROR: app_key contains non-hex digits";
const char PROGMEM msg_os_get_time[] = "os_getTime = ";
const char PROGMEM msg_lmic_txend[] = "LMIC.txend = ";
const char PROGMEM msg_lmic_txchnl[] = "LMIC.txChnl = ";
const char PROGMEM msg_lmic_version[] = "LMIC: ";
const char PROGMEM msg_oxticks[] = " osTicks, ";
const char PROGMEM msg_sec[] = " sec";
const char PROGMEM msg_separator[] = "-----";
const char PROGMEM msg_do_send[] = "do_send";
const char PROGMEM msg_lmic_opmode_equals[] = "LMIC.opmode= ";
const char PROGMEM msg_app_eui[] = "app_eui: ";
const char PROGMEM msg_dev_eui[] = "dev_eui: ";
const char PROGMEM msg_app_key[] = "app_key: ";
const char PROGMEM msg_charge_rate[] = "----ChargeRate: ";
const char PROGMEM msg_x_format[] = "X: %f";
const char PROGMEM msg_moisture_format[] = "Moisture ADC: %f, Moisture Percentage: %f, vBat %f\n\n";
const char PROGMEM msg_error_app_eui[] = "ERROR: app_eui string missing or too short";
const char PROGMEM msg_error_app_eui_hex[] = "ERROR: app_eui contains non-hex digits";
const char PROGMEM msg_error_dev_eui[] = "ERROR: dev_eui string missing or too short";
const char PROGMEM msg_error_dev_eui_hex[] = "ERROR: dev_eui contains non-hex digits";
const char PROGMEM msg_error_app_key[] = "ERROR: app_key string missing or too short";
const char PROGMEM msg_error_app_key_hex[] = "ERROR: app_key contains non-hex digits";

sensorData sd;

Expand Down Expand Up @@ -91,50 +92,48 @@ void LoraWANPrintLMICOpmode(void) {
}
}

#if !defined(UNIT_TEST)
void LoraWANDebug(const lmic_t& lmic_check) {
#ifdef DEBUG
LoraWANPrintLMICOpmode();
Serial.println("");
Serial.println("-----");

Serial.print(F("LMIC.seqnoUp = "));
Serial.println(lmic_check.seqnoUp);

Serial.print(F("LMIC.globalDutyRate = "));
Serial.print(lmic_check.globalDutyRate);
Serial.print(F(" osTicks, "));
Serial.print(osticks2ms(lmic_check.globalDutyRate) / 1000);
Serial.println(F(" sec"));

Serial.print(F("LMIC.globalDutyAvail = "));
Serial.print(lmic_check.globalDutyAvail);
Serial.print(F(" osTicks, "));
Serial.print(osticks2ms(lmic_check.globalDutyAvail) / 1000);
Serial.println(F(" sec"));

Serial.print(F("LMICbandplan_nextTx = "));
Serial.print(LMICbandplan_nextTx(os_getTime()));
Serial.print(F(" osTicks, "));
Serial.print(osticks2ms(LMICbandplan_nextTx(os_getTime())) / 1000);
Serial.println(F(" sec"));

Serial.print(F("os_getTime = "));
Serial.print(os_getTime());
Serial.print(F(" osTicks, "));
Serial.print(osticks2ms(os_getTime()) / 1000);
Serial.println(F(" sec"));

Serial.print(F("LMIC.txend = "));
Serial.println(lmic_check.txend);
Serial.print(F("LMIC.txChnl = "));
Serial.println(lmic_check.txChnl);

Serial.println("");
Serial.println("");
#endif
// Debug output now compile-time disabled for optimized builds
DEBUG_PRINT("LMIC.opmode: ");
// LoraWANPrintLMICOpmode() is now disabled via debug macros
DEBUG_PRINTLN("");
DEBUG_PRINTLN("-----");

DEBUG_PRINT("LMIC.seqnoUp = ");
DEBUG_PRINTLN(String(lmic_check.seqnoUp));

DEBUG_PRINT("LMIC.globalDutyRate = ");
DEBUG_PRINT(String(lmic_check.globalDutyRate));
DEBUG_PRINT(" osTicks, ");
DEBUG_PRINT(String(osticks2ms(lmic_check.globalDutyRate) / 1000));
DEBUG_PRINTLN(" sec");

DEBUG_PRINT("LMIC.globalDutyAvail = ");
DEBUG_PRINT(String(lmic_check.globalDutyAvail));
DEBUG_PRINT(" osTicks, ");
DEBUG_PRINT(String(osticks2ms(lmic_check.globalDutyAvail) / 1000));
DEBUG_PRINTLN(" sec");

DEBUG_PRINT("LMICbandplan_nextTx = ");
DEBUG_PRINT(String(LMICbandplan_nextTx(os_getTime())));
DEBUG_PRINT(" osTicks, ");
DEBUG_PRINT(String(osticks2ms(LMICbandplan_nextTx(os_getTime())) / 1000));
DEBUG_PRINTLN(" sec");

DEBUG_PRINT("os_getTime = ");
DEBUG_PRINT(String(os_getTime()));
DEBUG_PRINT(" osTicks, ");
DEBUG_PRINT(String(osticks2ms(os_getTime()) / 1000));
DEBUG_PRINTLN(" sec");

DEBUG_PRINT("LMIC.txend = ");
DEBUG_PRINTLN(String(lmic_check.txend));
DEBUG_PRINT("LMIC.txChnl = ");
DEBUG_PRINTLN(String(lmic_check.txChnl));

DEBUG_PRINTLN("");
DEBUG_PRINTLN("");
}
#endif // !UNIT_TEST

void PrintLMICVersion() {
Serial.print(F("LMIC: "));
Expand Down Expand Up @@ -215,11 +214,11 @@ void onEvent(ev_t ev) {
if (LMIC.txrxFlags & TXRX_PORT) {
fPort = LMIC.frame[LMIC.dataBeg - 1];
}

// Handle OTA update messages on port 1
if (fPort >= 1 && fPort <= OTA_MAX_CHUNKS) {
uint8_t* downlinkData = &LMIC.frame[LMIC.dataBeg];
uint8_t downlinkLen = LMIC.dataLen;
uint8_t downlinkLen = LMIC.dataLen;
handleDownlinkMessage(downlinkData, downlinkLen, fPort);
}
}
Expand Down Expand Up @@ -297,7 +296,7 @@ void do_send(osjob_t* /* j */) {
}

// ToDo: Refactor hex string to u1_t array conversion
void os_getArtEui(u1_t *buf) {
void os_getArtEui(u1_t* buf) {
const String cfg = settings_get_string("app_eui");

if (cfg.isEmpty() || cfg.length() < 16) {
Expand All @@ -315,8 +314,8 @@ void os_getArtEui(u1_t *buf) {
return;
}

std::string t = cfg.substring(i, i + 2).c_str();
app_eui[c] = static_cast<u1_t>(strtoul(t.c_str(), nullptr, 16));
std::string t = cfg.substring(i, i + 2).c_str();
app_eui[c] = static_cast<u1_t>(strtoul(t.c_str(), nullptr, 16));
}

Serial.print(FPSTR(msg_app_eui));
Expand All @@ -328,7 +327,7 @@ void os_getArtEui(u1_t *buf) {
memcpy_P(buf, app_eui.data(), 8);
}

void os_getDevEui(u1_t *buf) {
void os_getDevEui(u1_t* buf) {
const String cfg = settings_get_string("dev_eui");

if (cfg.isEmpty() || cfg.length() < 16) {
Expand All @@ -346,8 +345,8 @@ void os_getDevEui(u1_t *buf) {
return;
}

std::string t = cfg.substring(i, i + 2).c_str();
dev_eui[c] = static_cast<u1_t>(strtoul(t.c_str(), nullptr, 16));
std::string t = cfg.substring(i, i + 2).c_str();
dev_eui[c] = static_cast<u1_t>(strtoul(t.c_str(), nullptr, 16));
}

Serial.print("dev_eui: ");
Expand All @@ -359,7 +358,7 @@ void os_getDevEui(u1_t *buf) {
memcpy_P(buf, dev_eui.data(), 8);
}

void os_getDevKey(u1_t *buf) {
void os_getDevKey(u1_t* buf) {
const String cfg = settings_get_string("app_key");

// Validate the expected length (32 hex chars → 16 bytes)
Expand Down Expand Up @@ -401,10 +400,8 @@ void ReadSensors() {
sd.vBat = maxlipo.cellVoltage();
sd.batPercent = maxlipo.cellPercent();
sd.batRate = maxlipo.chargeRate();
#ifdef DEBUG
Serial.print(FPSTR(msg_charge_rate));
Serial.println(sd.batRate);
#endif
DEBUG_PRINT("----ChargeRate: ");
DEBUG_PRINTLN(String(sd.batRate));
}
for (int i = 0; i < MAX_SENSOR_READ; i++) {
float a = static_cast<float>(analogRead(config::SoilSensorPin));
Expand All @@ -413,19 +410,17 @@ void ReadSensors() {
}
float t = sd.soilMoistureValue / static_cast<float>(MAX_SENSOR_READ);
sd.soilMoistureValue = t;
float x = static_cast<float>(map(static_cast<long>(sd.soilMoistureValue),
float x = static_cast<float>(map(static_cast<long>(sd.soilMoistureValue),
get_calibration_air_value(),
get_calibration_water_value(), 0, 100));
sd.soilMoisturePercentage = abs(x);
#ifdef DEBUG
Serial.print(F("X: "));
Serial.println(x);
Serial.print(F("Moisture ADC: "));
Serial.print(sd.soilMoistureValue);
Serial.print(F(", Moisture Percentage: "));
Serial.print(sd.soilMoisturePercentage);
Serial.print(F(", vBat "));
Serial.println(sd.vBat);
Serial.println();
#endif
DEBUG_PRINT("X: ");
DEBUG_PRINTLN(String(x));
DEBUG_PRINT("Moisture ADC: ");
DEBUG_PRINT(String(sd.soilMoistureValue));
DEBUG_PRINT(", Moisture Percentage: ");
DEBUG_PRINT(String(sd.soilMoisturePercentage));
DEBUG_PRINT(", vBat ");
DEBUG_PRINTLN(String(sd.vBat));
DEBUG_PRINTLN("");
}
2 changes: 1 addition & 1 deletion src/lorawan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern bool maxLipoFound;
extern Adafruit_MAX17048 maxlipo;

void LoraWANPrintLMICOpmode(void);
void LoraWANDebug(const lmic_t& lmic_check);
void LoraWANDebug(const lmic_t &lmic_check);
void PrintLMICVersion();
void onEvent(ev_t ev);
void do_send(osjob_t *j);
Expand Down
4 changes: 2 additions & 2 deletions src/lorawan_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Preferences &prefs() {
static Preferences instance;
return instance;
}
} // namespace
} // namespace

void lorawan_preferences_init() {
prefs().begin(LMIC_PREF_NS_NAME, RW_MODE);
Expand All @@ -32,7 +32,7 @@ bool lmic_init_needed() {

void lmic_save() {
LMIC.globalDutyAvail = 0;
size_t a = prefs().putBytes(LMIC_BYTES_KEY_NAME, &LMIC, sizeof(LMIC));
size_t a = prefs().putBytes(LMIC_BYTES_KEY_NAME, &LMIC, sizeof(LMIC));
Serial.print("Saved: ");
Serial.println(a);
}
Expand Down
14 changes: 7 additions & 7 deletions src/lorawan_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

#include <hal/hal.h>

constexpr const char* LMIC_PREF_NS_NAME = "lmic";
constexpr const char* LMIC_INIT_NEEDED_KEY_NAME = "init";
constexpr const char* LMIC_BYTES_KEY_NAME = "lmic_struct";
constexpr const char* LORAWAN_CONFIG_PRESENT_KEY = "lorawan_config";
constexpr const char* APP_EUID_KEY = "euid";
constexpr const char* RUNTIME_KEY = "runtime";
constexpr int MAX_LORAWAN_CONF_CHAR_LEN = 100;
constexpr const char *LMIC_PREF_NS_NAME = "lmic";
constexpr const char *LMIC_INIT_NEEDED_KEY_NAME = "init";
constexpr const char *LMIC_BYTES_KEY_NAME = "lmic_struct";
constexpr const char *LORAWAN_CONFIG_PRESENT_KEY = "lorawan_config";
constexpr const char *APP_EUID_KEY = "euid";
constexpr const char *RUNTIME_KEY = "runtime";
constexpr int MAX_LORAWAN_CONF_CHAR_LEN = 100;

constexpr bool RW_MODE = false;
constexpr bool RO_MODE = true;
Expand Down
Loading