cleanup, debug-switch

This commit is contained in:
Joerg Lehmann 2019-06-21 15:42:32 +02:00
parent 441356f0ab
commit bac960c897
1 changed files with 71 additions and 46 deletions

View File

@ -28,6 +28,8 @@
using namespace McciCatena; using namespace McciCatena;
#define DEBUG
/****************************************************************************\ /****************************************************************************\
| |
| MANIFEST CONSTANTS & TYPEDEFS | MANIFEST CONSTANTS & TYPEDEFS
@ -267,25 +269,18 @@ void setup_platform(void)
); );
// read config_data from fram... // read config_data from fram...
#ifdef DEBUG
gCatena.SafePrintf("Reading Calibration Config from FRAM...\n"); gCatena.SafePrintf("Reading Calibration Config from FRAM...\n");
#endif
gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data)); gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data));
#ifdef DEBUG
gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0); gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0);
gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0); gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0);
gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000); gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000);
gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000); gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000);
gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data)); gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data));
#endif
// im Moment statisch...
//config_data.cal_w1_0 = 20000;
//config_data.cal_w2_0 = 20000;
//config_data.cal_w1_factor = 2.5;
//config_data.cal_w2_factor = 2.5;
// config_data speichern...
//gCatena.SafePrintf("Writing Calibration Config to FRAM...\n");
//gCatena.getFram()->saveField(cFramStorage::kBme680Cal, (const uint8_t *)&config_data, sizeof(config_data));
#ifdef USBCON #ifdef USBCON
// if running unattended, don't wait for USB connect. // if running unattended, don't wait for USB connect.
@ -298,7 +293,7 @@ void setup_platform(void)
gCatena.SafePrintf("\n"); gCatena.SafePrintf("\n");
gCatena.SafePrintf("-------------------------------------------------------------------------------\n"); gCatena.SafePrintf("-------------------------------------------------------------------------------\n");
gCatena.SafePrintf("BeieliScale Version %s.\n", sVersion); gCatena.SafePrintf("mini-beieli.ch - BeieliScale Version %s.\n", sVersion);
{ {
char sRegion[16]; char sRegion[16];
gCatena.SafePrintf("Target network: %s / %s\n", gCatena.SafePrintf("Target network: %s / %s\n",
@ -384,7 +379,9 @@ void setup_bme280(void)
void setup_scales(void) void setup_scales(void)
{ {
#ifdef DEBUG
gCatena.SafePrintf("Setup Scales...\n"); gCatena.SafePrintf("Setup Scales...\n");
#endif
// Enable Power // Enable Power
digitalWrite(D10, HIGH); digitalWrite(D10, HIGH);
@ -395,25 +392,27 @@ void setup_scales(void)
// - With a gain factor of 32, channel B is selected // - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library // By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here. // default "128" (Channel A) is used here.
#ifdef DEBUG
gCatena.SafePrintf("Setup Scale 1...\n"); gCatena.SafePrintf("Setup Scale 1...\n");
#endif
LoadCell_1.begin(A1, A0); LoadCell_1.begin(A1, A0);
if (!(LoadCell_1.wait_ready_timeout(1000))) { if (!(LoadCell_1.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 1 not ready.\n"); gCatena.SafePrintf("Scale 1 not ready.\n");
} }
//LoadCell_1.power_down(); #ifdef DEBUG
gCatena.SafePrintf("Setup Scale 2...\n"); gCatena.SafePrintf("Setup Scale 2...\n");
#endif
LoadCell_2.begin(D12, A2); LoadCell_2.begin(D12, A2);
if (!(LoadCell_2.wait_ready_timeout(1000))) { if (!(LoadCell_2.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 2 not ready.\n"); gCatena.SafePrintf("Scale 2 not ready.\n");
} }
//LoadCell_2.power_down(); #ifdef DEBUG
gCatena.SafePrintf("Setup Scales is complete\n"); gCatena.SafePrintf("Setup Scales is complete\n");
#endif
} }
void setup_flash(void) void setup_flash(void)
@ -421,7 +420,7 @@ void setup_flash(void)
if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS)) { if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS)) {
fFlash = true; fFlash = true;
gFlash.powerDown(); gFlash.powerDown();
gCatena.SafePrintf("FLASH found, put power down\n"); gCatena.SafePrintf("FLASH found, but power down\n");
} }
else { else {
fFlash = false; fFlash = false;
@ -558,6 +557,33 @@ uint8_t GetVBatValue(int millivolts)
return res; return res;
} }
void DoDeepSleep()
{
gCatena.SafePrintf("Now going to deep sleep: %d\n",millis());
// Prepare Deep Sleep
gLed.Set(LedPattern::Off);
Serial.end();
Wire.end();
SPI.end();
if (fFlash)
gSPI2.end();
// Now sleeping...
gCatena.Sleep(CATCFG_T_INTERVAL - 5);
// Recover from wakeup...
Serial.begin();
Wire.begin();
SPI.begin();
if (fFlash)
gSPI2.begin();
gCatena.SafePrintf("Done with deep sleep: %d\n",millis());
}
void ReadSensors(bool firstTime, bool readOnly) void ReadSensors(bool firstTime, bool readOnly)
{ {
int16_t temp_current; int16_t temp_current;
@ -576,81 +602,72 @@ void ReadSensors(bool firstTime, bool readOnly)
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C // temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10. // pressure is 2 bytes, hPa * 10.
// humidity is one byte, where 0 == 0/256 and 0xFF == 255/256. // humidity is one byte, where 0 == 0/256 and 0xFF == 255/256.
#ifdef DEBUG
gCatena.SafePrintf( gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n", "BME280: T: %d P: %d RH: %d\n",
(int)m.Temperature, (int)m.Temperature,
(int)m.Pressure, (int)m.Pressure,
(int)m.Humidity); (int)m.Humidity);
#endif
temp_current = (int16_t)((m.Temperature) * 10); temp_current = (int16_t)((m.Temperature) * 10);
humidity_current = (uint8_t)m.Humidity; humidity_current = (uint8_t)m.Humidity;
pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET); pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET);
#ifdef DEBUG
gCatena.SafePrintf("pressure_current: %d\n", pressure_current); gCatena.SafePrintf("pressure_current: %d\n", pressure_current);
#endif
} }
// vBat // vBat
float vBat = gCatena.ReadVbat(); float vBat = gCatena.ReadVbat();
#ifdef DEBUG
gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f)); gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f));
#endif
// vBus // vBus
float vBus = gCatena.ReadVbus(); float vBus = gCatena.ReadVbus();
#ifdef DEBUG
gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f)); gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f));
#endif
fUsbPower = (vBus > 3.0) ? true : false; fUsbPower = (vBus > 3.0) ? true : false;
// Setup Scales // Setup Scales
setup_scales(); setup_scales();
// Read Scales // Read Scales
gCatena.SafePrintf("Before Read Scales\n");
// Power-Up HX711
LoadCell_1.power_up();
if (!(LoadCell_1.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 1 not ready.\n");
}
gCatena.SafePrintf("Before Read Scales\n");
if (LoadCell_1.is_ready()) { if (LoadCell_1.is_ready()) {
#ifdef DEBUG
Serial.println("HX711 LoadCell_1 is ready."); Serial.println("HX711 LoadCell_1 is ready.");
#endif
long w1 = LoadCell_1.read_average(5); long w1 = LoadCell_1.read_average(5);
weight1_current = (int32_t)w1; weight1_current = (int32_t)w1;
#ifdef DEBUG
gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1); gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1);
gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current); gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current);
#endif
} }
else { else {
Serial.println("HX711 LoadCell_1 not ready."); Serial.println("HX711 LoadCell_1 not ready.");
} }
// Power-Down HX711
//LoadCell_1.power_down();
// Power-Up HX711
LoadCell_2.power_up();
if (!(LoadCell_2.wait_ready_timeout(1000))) {
gCatena.SafePrintf("Scale 2 not ready.\n");
}
if (LoadCell_2.is_ready()) { if (LoadCell_2.is_ready()) {
#ifdef DEBUG
Serial.println("HX711 LoadCell_2 is ready."); Serial.println("HX711 LoadCell_2 is ready.");
#endif
long w2 = LoadCell_2.read_average(5); long w2 = LoadCell_2.read_average(5);
weight2_current = (int32_t)w2; weight2_current = (int32_t)w2;
#ifdef DEBUG
gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2); gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2);
gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current); gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current);
#endif
} }
else { else {
Serial.println("HX711 LoadCell_2 not ready."); Serial.println("HX711 LoadCell_2 not ready.");
} }
// Power-Down HX711
//LoadCell_2.power_down();
// Disable Power // Disable Power
digitalWrite(D10, LOW); digitalWrite(D10, LOW);
gCatena.SafePrintf("After Read Scales\n");
// Gewicht berechnen // Gewicht berechnen
weight_current32 = (int32_t)((((weight1_current - config_data.cal_w1_0) / config_data.cal_w1_factor) + ((weight2_current - config_data.cal_w2_0) / config_data.cal_w2_factor)) / 5.0); weight_current32 = (int32_t)((((weight1_current - config_data.cal_w1_0) / config_data.cal_w1_factor) + ((weight2_current - config_data.cal_w2_0) / config_data.cal_w2_factor)) / 5.0);
if (weight_current32 < 0) { if (weight_current32 < 0) {
@ -725,7 +742,7 @@ void ReadSensors(bool firstTime, bool readOnly)
os_getTime() + sec2osticks(CATCFG_T_INTERVAL), os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb); sleepDoneCb);
if (!fUsbPower) { if (!fUsbPower) {
gCatena.Sleep(CATCFG_T_INTERVAL - 5); DoDeepSleep();
} }
} }
} }
@ -751,15 +768,21 @@ void startSendingUplink(bool firstTime)
bool fConfirmed = false; bool fConfirmed = false;
if (gCatena.GetOperatingFlags() & (1 << 16)) { if (gCatena.GetOperatingFlags() & (1 << 16)) {
#ifdef DEBUG
gCatena.SafePrintf("requesting confirmed tx\n"); gCatena.SafePrintf("requesting confirmed tx\n");
#endif
fConfirmed = true; fConfirmed = true;
} }
if (firstTime) { if (firstTime) {
#ifdef DEBUG
gCatena.SafePrintf("SendBuffer firstTime\n"); gCatena.SafePrintf("SendBuffer firstTime\n");
#endif
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed); gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed);
} else { } else {
#ifdef DEBUG
gCatena.SafePrintf("SendBuffer not firstTime\n"); gCatena.SafePrintf("SendBuffer not firstTime\n");
#endif
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed); gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed);
} }
ClearLoraData(); ClearLoraData();
@ -779,12 +802,10 @@ static void sendBufferDoneCb(
else { else {
pFn = settleDoneCb; pFn = settleDoneCb;
} }
gCatena.SafePrintf("sendBufferDoneCB before TimedCallback\n");
os_setTimedCallback( os_setTimedCallback(
&sensorJob, &sensorJob,
os_getTime() + sec2osticks(CATCFG_T_SETTLE), os_getTime() + sec2osticks(CATCFG_T_SETTLE),
pFn); pFn);
gCatena.SafePrintf("sendBufferDoneCB after TimedCallback\n");
} }
static void txFailedDoneCb( static void txFailedDoneCb(
@ -800,7 +821,9 @@ static void txFailedDoneCb(
static void settleDoneCb( static void settleDoneCb(
osjob_t* pSendJob) osjob_t* pSendJob)
{ {
#ifdef DEBUG
gCatena.SafePrintf("settleDoneCb\n"); gCatena.SafePrintf("settleDoneCb\n");
#endif
sleepDoneCb(pSendJob); sleepDoneCb(pSendJob);
} }
@ -824,8 +847,10 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
unsigned txCycle; unsigned txCycle;
unsigned txCount; unsigned txCount;
#ifdef DEBUG
gCatena.SafePrintf("receiveMessage was called!!!\n"); gCatena.SafePrintf("receiveMessage was called!!!\n");
#endif
if (! (port == 1 && 2 <= nMessage && nMessage <= 3)) if (! (port == 1 && 2 <= nMessage && nMessage <= 3))
{ {
gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n", gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n",