senddata restructured

This commit is contained in:
Klaus K Wilting 2018-07-14 23:13:25 +02:00
parent b708524baa
commit bb3f06bfc4
4 changed files with 75 additions and 69 deletions

View File

@ -11,11 +11,11 @@
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
;env_default = heltec env_default = heltec
;env_default = ttgov1 ;env_default = ttgov1
;env_default = ttgov2 ;env_default = ttgov2
;env_default = ttgov21 ;env_default = ttgov21
env_default = ttgobeam ;env_default = ttgobeam
;env_default = lopy ;env_default = lopy
;env_default = lopy4 ;env_default = lopy4
;env_default = fipy ;env_default = fipy

View File

@ -54,10 +54,8 @@ led_states previousLEDState =
unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started) unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started)
uint16_t LEDBlinkDuration = 0; // How long the blink need to be uint16_t LEDBlinkDuration = 0; // How long the blink need to be
uint16_t LEDColor = COLOR_NONE; // state machine variable to set RGB LED color uint16_t LEDColor = COLOR_NONE; // state machine variable to set RGB LED color
hw_timer_t *displaytimer = hw_timer_t *channelSwitch = NULL, *displaytimer = NULL,
NULL; // configure hardware timer used for cyclic display refresh *sendCycle = NULL; // configure hardware timer for cyclic tasks
hw_timer_t *channelSwitch =
NULL; // configure hardware timer used for wifi channel switching
#ifdef HAS_GPS #ifdef HAS_GPS
gpsStatus_t gps_status; // struct for storing gps data gpsStatus_t gps_status; // struct for storing gps data
@ -84,7 +82,7 @@ CayenneLPP payload(PAYLOAD_BUFFER_SIZE);
// this variables will be changed in the ISR, and read in main loop // this variables will be changed in the ISR, and read in main loop
static volatile int ButtonPressedIRQ = 0, DisplayTimerIRQ = 0, static volatile int ButtonPressedIRQ = 0, DisplayTimerIRQ = 0,
ChannelTimerIRQ = 0; ChannelTimerIRQ = 0, SendCycleTimerIRQ = 0;
// local Tag for logging // local Tag for logging
static const char TAG[] = "main"; static const char TAG[] = "main";
@ -168,7 +166,9 @@ void lorawan_loop(void *pvParameters) {
* -------------------------------------------------------- */ * -------------------------------------------------------- */
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
HAS_DISPLAY u8x8(OLED_RST, OLED_SCL, OLED_SDA); HAS_DISPLAY u8x8(OLED_RST, OLED_SCL, OLED_SDA);
// Display Refresh IRQ // Display Refresh IRQ
void IRAM_ATTR DisplayIRQ() { void IRAM_ATTR DisplayIRQ() {
portENTER_CRITICAL_ISR(&timerMux); portENTER_CRITICAL_ISR(&timerMux);
@ -200,6 +200,13 @@ void IRAM_ATTR ChannelSwitchIRQ() {
portEXIT_CRITICAL(&timerMux); portEXIT_CRITICAL(&timerMux);
} }
// Send Cycle Timer IRQ Handler Routine
void IRAM_ATTR SendCycleIRQ() {
portENTER_CRITICAL(&timerMux);
SendCycleTimerIRQ++;
portEXIT_CRITICAL(&timerMux);
}
/* end hardware specific parts /* end hardware specific parts
* -------------------------------------------------------- */ * -------------------------------------------------------- */
@ -215,7 +222,7 @@ void sniffer_loop(void *pvParameters) {
if (ChannelTimerIRQ) { if (ChannelTimerIRQ) {
portENTER_CRITICAL(&timerMux); portENTER_CRITICAL(&timerMux);
ChannelTimerIRQ--; ChannelTimerIRQ = 0;
portEXIT_CRITICAL(&timerMux); portEXIT_CRITICAL(&timerMux);
// rotates variable channel 1..WIFI_CHANNEL_MAX // rotates variable channel 1..WIFI_CHANNEL_MAX
channel = (channel % WIFI_CHANNEL_MAX) + 1; channel = (channel % WIFI_CHANNEL_MAX) + 1;
@ -375,7 +382,7 @@ void updateDisplay() {
// refresh display according to refresh cycle setting // refresh display according to refresh cycle setting
if (DisplayTimerIRQ) { if (DisplayTimerIRQ) {
portENTER_CRITICAL(&timerMux); portENTER_CRITICAL(&timerMux);
DisplayTimerIRQ--; DisplayTimerIRQ = 0;
portEXIT_CRITICAL(&timerMux); portEXIT_CRITICAL(&timerMux);
refreshDisplay(); refreshDisplay();
@ -393,7 +400,7 @@ void updateDisplay() {
void readButton() { void readButton() {
if (ButtonPressedIRQ) { if (ButtonPressedIRQ) {
portENTER_CRITICAL(&timerMux); portENTER_CRITICAL(&timerMux);
ButtonPressedIRQ--; ButtonPressedIRQ = 0;
portEXIT_CRITICAL(&timerMux); portEXIT_CRITICAL(&timerMux);
ESP_LOGI(TAG, "Button pressed"); ESP_LOGI(TAG, "Button pressed");
ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults"); ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults");
@ -415,9 +422,6 @@ void blink_LED(uint16_t set_color, uint16_t set_blinkduration) {
void led_loop() { void led_loop() {
// Custom blink running always have priority other LoRaWAN led management // Custom blink running always have priority other LoRaWAN led management
if (LEDBlinkStarted && LEDBlinkDuration) { if (LEDBlinkStarted && LEDBlinkDuration) {
// ESP_LOGI(TAG, "Start=%ld for %g",LEDBlinkStarted, LEDBlinkDuration );
// Custom blink is finished, let this order, avoid millis() overflow // Custom blink is finished, let this order, avoid millis() overflow
if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) { if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
// Led becomes off, and stop blink // Led becomes off, and stop blink
@ -429,7 +433,6 @@ void led_loop() {
// In case of LoRaWAN led management blinked off // In case of LoRaWAN led management blinked off
LEDState = LED_ON; LEDState = LED_ON;
} }
// No custom blink, check LoRaWAN state // No custom blink, check LoRaWAN state
} else { } else {
@ -449,17 +452,14 @@ void led_loop() {
LEDColor = COLOR_RED; LEDColor = COLOR_RED;
// heartbeat long blink 200ms on each 2 seconds // heartbeat long blink 200ms on each 2 seconds
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF; LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
} else { } else
#endif // HAS_LORA #endif // HAS_LORA
{
// led off // led off
LEDColor = COLOR_NONE; LEDColor = COLOR_NONE;
LEDState = LED_OFF; LEDState = LED_OFF;
} }
} }
// ESP_LOGI(TAG, "state=%d previous=%d Color=%d",LEDState, previousLEDState,
// LEDColor );
// led need to change state? avoid digitalWrite() for nothing // led need to change state? avoid digitalWrite() for nothing
if (LEDState != previousLEDState) { if (LEDState != previousLEDState) {
if (LEDState == LED_ON) { if (LEDState == LED_ON) {
@ -468,7 +468,7 @@ void led_loop() {
#ifdef LED_ACTIVE_LOW #ifdef LED_ACTIVE_LOW
digitalWrite(HAS_LED, LOW); digitalWrite(HAS_LED, LOW);
#else #else
digitalWrite(HAS_LED, HIGH); digitalWrite(HAS_LED, HIGH);
#endif #endif
} else { } else {
@ -477,7 +477,7 @@ void led_loop() {
#ifdef LED_ACTIVE_LOW #ifdef LED_ACTIVE_LOW
digitalWrite(HAS_LED, HIGH); digitalWrite(HAS_LED, HIGH);
#else #else
digitalWrite(HAS_LED, LOW); digitalWrite(HAS_LED, LOW);
#endif #endif
} }
previousLEDState = LEDState; previousLEDState = LEDState;
@ -486,19 +486,42 @@ void led_loop() {
#endif // #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) #endif // #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
void sendpayload() { void updatePayload() {
// append counter data to payload
payload.reset(); if (SendCycleTimerIRQ) {
payload.addCount(macs_wifi, cfg.blescan ? macs_ble : 0); portENTER_CRITICAL(&timerMux);
// append GPS data, if present SendCycleTimerIRQ = 0;
portEXIT_CRITICAL(&timerMux);
// append counter data to payload
payload.reset();
payload.addCount(macs_wifi, cfg.blescan ? macs_ble : 0);
// append GPS data, if present
#ifdef HAS_GPS #ifdef HAS_GPS
if ((cfg.gpsmode) && (gps.location.isValid())) { if ((cfg.gpsmode) && (gps.location.isValid())) {
gps_read(); gps_read();
payload.addGPS(gps_status); payload.addGPS(gps_status);
} }
// log NMEA status, useful for debugging GPS connection
ESP_LOGD(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d",
gps.passedChecksum(), gps.failedChecksum(),
gps.sentencesWithFix());
// log GPS position if we have a fix
if ((cfg.gpsmode) && (gps.location.isValid())) {
gps_read();
ESP_LOGI(TAG, "lat=%.6f | lon=%.6f | %u Sats | HDOP=%.1f | Altitude=%um",
gps_status.latitude / (float)1e6,
gps_status.longitude / (float)1e6, gps_status.satellites,
gps_status.hdop / (float)100, gps_status.altitude);
} else {
ESP_LOGI(TAG, "No valid GPS position or GPS disabled");
}
#endif #endif
senddata(PAYLOADPORT);
} senddata(PAYLOADPORT);
}
} // updatePayload()
/* begin Aruino SETUP /* begin Aruino SETUP
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
@ -625,11 +648,17 @@ void setup() {
#endif #endif
// setup channel rotation trigger IRQ using esp32 hardware timer 1 // setup channel rotation trigger IRQ using esp32 hardware timer 1
channelSwitch = timerBegin(1, 80, true); channelSwitch = timerBegin(1, 800, true);
timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true);
timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true); timerAlarmWrite(channelSwitch, cfg.wifichancycle * 1000, true);
timerAlarmEnable(channelSwitch); timerAlarmEnable(channelSwitch);
// setup send cycle trigger IRQ using esp32 hardware timer 2
sendCycle = timerBegin(2, 8000, true);
timerAttachInterrupt(sendCycle, &SendCycleIRQ, true);
timerAlarmWrite(sendCycle, cfg.sendcycle * 2 * 10000, true);
timerAlarmEnable(sendCycle);
// show payload encoder // show payload encoder
#if PAYLOAD_ENCODER == 1 #if PAYLOAD_ENCODER == 1
strcat_P(features, " PAYLOAD_PLAIN"); strcat_P(features, " PAYLOAD_PLAIN");
@ -656,9 +685,11 @@ void setup() {
// This tells LMIC to make the receive windows bigger, in case your clock is // This tells LMIC to make the receive windows bigger, in case your clock is
// 1% faster or slower. // 1% faster or slower.
LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100); LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
// join network
LMIC_startJoining();
// start lmic runloop in rtos task on core 1 (note: arduino main loop runs on // start lmic runloop in rtos task on core 1 (note: arduino main loop runs
// core 1, too) // on core 1, too)
// https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/
ESP_LOGI(TAG, "Starting Lora task on core 1"); ESP_LOGI(TAG, "Starting Lora task on core 1");
@ -692,9 +723,6 @@ void setup() {
xTaskCreatePinnedToCore(gps_loop, "gpsfeed", 2048, (void *)1, 2, NULL, 0); xTaskCreatePinnedToCore(gps_loop, "gpsfeed", 2048, (void *)1, 2, NULL, 0);
} }
#endif #endif
// send initial payload to open transfer interfaces
sendpayload();
} }
/* end Arduino SETUP /* end Arduino SETUP
@ -707,15 +735,10 @@ void loop() {
while (1) { while (1) {
// simple state machine for controlling uptime, display, LED, button, // state machine for uptime, display, LED, button, lowmemory, senddata
// memory.
uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit)
// send data every x seconds, x/2 is configured in cfg.sendcycle
if ((uptime() % (cfg.sendcycle * 2000)) < 1)
sendpayload();
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
led_loop(); led_loop();
#endif #endif
@ -739,24 +762,8 @@ void loop() {
reset_salt(); // get new salt for salting hashes reset_salt(); // get new salt for salting hashes
} }
#ifdef HAS_GPS // check send cycle and send payload if cycle is expired
// log NMEA status every 60 seconds, useful for debugging GPS connection updatePayload();
if ((uptime() % 60000) < 1) {
ESP_LOGD(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d",
gps.passedChecksum(), gps.failedChecksum(),
gps.sentencesWithFix());
if ((cfg.gpsmode) && (gps.location.isValid())) {
gps_read();
ESP_LOGI(TAG,
"lat=%.6f | lon=%.6f | %u Sats | HDOP=%.1f | Altitude=%um",
gps_status.latitude / (float)1e6,
gps_status.longitude / (float)1e6, gps_status.satellites,
gps_status.hdop / (float)100, gps_status.altitude);
} else {
ESP_LOGI(TAG, "No valid GPS position or GPS disabled");
}
}
#endif
vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog

View File

@ -1,9 +1,12 @@
#include "configmanager.h" #include "configmanager.h"
#include "lorawan.h"
#include "macsniff.h" #include "macsniff.h"
#include "senddata.h" #include "senddata.h"
#ifdef HAS_LORA
#include "lorawan.h"
#endif
// program version - note: increment version after modifications to configData_t // program version - note: increment version after modifications to configData_t
// struct!! // struct!!
#define PROGVERSION "1.3.9" // use max 10 chars here! #define PROGVERSION "1.3.9" // use max 10 chars here!

View File

@ -6,10 +6,6 @@
// Basic Config // Basic Config
#include "globals.h" #include "globals.h"
// LMIC-Arduino LoRaWAN Stack
//#include <lmic.h>
//#include <hal/hal.h>
// Local logging tag // Local logging tag
static const char TAG[] = "main"; static const char TAG[] = "main";
@ -213,10 +209,10 @@ void set_loraadr(uint8_t val) {
break; break;
} }
LMIC_setAdrMode(cfg.adrmode); LMIC_setAdrMode(cfg.adrmode);
};
#else #else
ESP_LOGW(TAG, "Remote command: LoRa not implemented"); ESP_LOGW(TAG, "Remote command: LoRa not implemented");
#endif // HAS_LORA #endif // HAS_LORA
};
void set_blescan(uint8_t val) { void set_blescan(uint8_t val) {
ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val ? "on" : "off"); ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val ? "on" : "off");