2018-03-18 19:45:17 +01:00
|
|
|
/*
|
2018-03-21 18:03:14 +01:00
|
|
|
|
|
|
|
Copyright 2018 Oliver Brandmueller <ob@sysadm.in>
|
|
|
|
Copyright 2018 Klaus Wilting <verkehrsrot@arcor.de>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
NOTICE:
|
|
|
|
Parts of the source files in this repository are made available under different licenses.
|
|
|
|
Refer to LICENSE.txt file in repository for more details.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Basic Config
|
|
|
|
#include "globals.h"
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-02 03:00:27 +02:00
|
|
|
// std::set for unified array functions
|
|
|
|
#include <set>
|
|
|
|
|
2018-04-16 20:56:29 +02:00
|
|
|
// Does nothing and avoid any compilation error with I2C
|
|
|
|
#include <Wire.h>
|
2018-03-18 19:45:17 +01:00
|
|
|
|
|
|
|
// LMIC-Arduino LoRaWAN Stack
|
2018-03-21 18:03:14 +01:00
|
|
|
#include "loraconf.h"
|
2018-03-18 19:45:17 +01:00
|
|
|
#include <lmic.h>
|
|
|
|
#include <hal/hal.h>
|
|
|
|
|
2018-03-21 21:53:42 +01:00
|
|
|
// ESP32 Functions
|
|
|
|
#include <esp_event_loop.h> // needed for Wifi event handler
|
|
|
|
#include <esp_spi_flash.h> // needed for reading ESP32 chip attributes
|
|
|
|
#include <esp32-hal-log.h> // needed for ESP_LOGx on arduino framework
|
2018-03-18 19:45:17 +01:00
|
|
|
|
|
|
|
configData_t cfg; // struct holds current device configuration
|
|
|
|
osjob_t sendjob, initjob; // LMIC
|
|
|
|
|
|
|
|
// Initialize global variables
|
2018-04-17 18:46:01 +02:00
|
|
|
char display_lora[16], display_lmic[16]; // display buffers
|
2018-04-17 19:20:54 +02:00
|
|
|
uint64_t uptimecounter = 0;
|
2018-04-17 22:42:06 +02:00
|
|
|
uint32_t currentMillis = 0, previousDisplaymillis = 0;
|
|
|
|
uint8_t DisplayState, LEDState = 0, LEDcount = 0;
|
|
|
|
uint16_t LEDBlinkduration = 0, LEDInterval = 0, color=COLOR_NONE;
|
2018-04-17 18:46:01 +02:00
|
|
|
uint8_t channel = 0; // wifi channel counter
|
|
|
|
bool joinstate = false;
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-02 14:34:16 +02:00
|
|
|
std::set<uint16_t> macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE)
|
|
|
|
std::set<uint16_t> wifis; // associative container holds unique Wifi MAC adress hashes
|
2018-04-02 01:36:14 +02:00
|
|
|
|
|
|
|
#ifdef BLECOUNTER
|
2018-04-02 14:34:16 +02:00
|
|
|
std::set<uint16_t> bles; // associative container holds unique BLE MAC adresses hashes
|
2018-04-02 01:36:14 +02:00
|
|
|
int scanTime;
|
|
|
|
#endif
|
2018-03-18 19:45:17 +01:00
|
|
|
|
|
|
|
// this variable will be changed in the ISR, and read in main loop
|
|
|
|
static volatile bool ButtonTriggered = false;
|
|
|
|
|
|
|
|
// local Tag for logging
|
|
|
|
static const char *TAG = "paxcnt";
|
2018-03-21 17:34:11 +01:00
|
|
|
// Note: Log level control seems not working during runtime,
|
|
|
|
// so we need to switch loglevel by compiler build option in platformio.ini
|
2018-03-18 19:45:17 +01:00
|
|
|
#ifndef VERBOSE
|
|
|
|
int redirect_log(const char * fmt, va_list args) {
|
|
|
|
//do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// defined in configmanager.cpp
|
|
|
|
void eraseConfig(void);
|
|
|
|
void saveConfig(void);
|
|
|
|
void loadConfig(void);
|
|
|
|
|
2018-04-17 22:42:06 +02:00
|
|
|
void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) {
|
|
|
|
color = set_color; // set color for RGB LED
|
|
|
|
LEDBlinkduration = set_blinkduration; // duration on
|
|
|
|
LEDInterval = set_interval; // duration off - on - off
|
|
|
|
LEDcount = set_count * 2; // number of blinks before LED off
|
|
|
|
LEDState = set_count ? 1 : 0; // sets LED to off if 0 blinks
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:00:27 +02:00
|
|
|
/* begin LMIC specific parts ------------------------------------------------------------ */
|
2018-03-30 22:10:28 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
// defined in lorawan.cpp
|
|
|
|
void gen_lora_deveui(uint8_t * pdeveui);
|
2018-03-21 22:32:59 +01:00
|
|
|
void RevBytes(unsigned char* b, size_t c);
|
2018-04-04 01:26:05 +02:00
|
|
|
void get_hard_deveui(uint8_t *pdeveui);
|
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
#ifdef VERBOSE
|
|
|
|
void printKeys(void);
|
2018-04-02 03:00:27 +02:00
|
|
|
#endif // VERBOSE
|
2018-03-18 19:45:17 +01:00
|
|
|
|
|
|
|
// LMIC callback functions
|
2018-03-23 11:57:19 +01:00
|
|
|
void os_getDevKey (u1_t *buf) {
|
|
|
|
memcpy(buf, APPKEY, 16);
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:50:49 +01:00
|
|
|
void os_getArtEui (u1_t *buf) {
|
|
|
|
memcpy(buf, APPEUI, 8);
|
|
|
|
RevBytes(buf, 8); // TTN requires it in LSB First order, so we swap bytes
|
|
|
|
}
|
2018-03-23 11:57:19 +01:00
|
|
|
|
2018-03-21 22:32:59 +01:00
|
|
|
void os_getDevEui (u1_t* buf) {
|
2018-03-23 11:57:19 +01:00
|
|
|
int i=0, k=0;
|
|
|
|
memcpy(buf, DEVEUI, 8); // get fixed DEVEUI from loraconf.h
|
2018-04-04 01:26:05 +02:00
|
|
|
for (i=0; i<8 ; i++) {
|
2018-03-23 11:57:19 +01:00
|
|
|
k += buf[i];
|
2018-04-04 01:26:05 +02:00
|
|
|
}
|
|
|
|
if (k) {
|
2018-03-23 11:57:19 +01:00
|
|
|
RevBytes(buf, 8); // use fixed DEVEUI and swap bytes to LSB format
|
2018-04-04 01:26:05 +02:00
|
|
|
} else {
|
2018-03-23 11:57:19 +01:00
|
|
|
gen_lora_deveui(buf); // generate DEVEUI from device's MAC
|
2018-04-04 01:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get MCP 24AA02E64 hardware DEVEUI (override default settings if found)
|
|
|
|
#ifdef MCP_24AA02E64_I2C_ADDRESS
|
|
|
|
get_hard_deveui(buf);
|
|
|
|
RevBytes(buf, 8); // swap bytes to LSB format
|
|
|
|
#endif
|
2018-03-21 22:32:59 +01:00
|
|
|
}
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-02 03:00:27 +02:00
|
|
|
// LMIC enhanced Pin mapping
|
|
|
|
const lmic_pinmap lmic_pins = {
|
|
|
|
.mosi = PIN_SPI_MOSI,
|
|
|
|
.miso = PIN_SPI_MISO,
|
|
|
|
.sck = PIN_SPI_SCK,
|
|
|
|
.nss = PIN_SPI_SS,
|
|
|
|
.rxtx = LMIC_UNUSED_PIN,
|
|
|
|
.rst = RST,
|
|
|
|
.dio = {DIO0, DIO1, DIO2}
|
|
|
|
};
|
|
|
|
|
|
|
|
// LMIC functions
|
|
|
|
void onEvent(ev_t ev);
|
|
|
|
void do_send(osjob_t* j);
|
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
// LoRaWAN Initjob
|
|
|
|
static void lora_init (osjob_t* j) {
|
|
|
|
// reset MAC state
|
|
|
|
LMIC_reset();
|
|
|
|
// This tells LMIC to make the receive windows bigger, in case your clock is 1% faster or slower.
|
|
|
|
LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
|
|
|
|
// start joining
|
|
|
|
LMIC_startJoining();
|
|
|
|
}
|
|
|
|
|
2018-04-02 23:27:38 +02:00
|
|
|
// LMIC FreeRTos Task
|
2018-03-18 19:45:17 +01:00
|
|
|
void lorawan_loop(void * pvParameters) {
|
|
|
|
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
|
2018-04-02 03:00:27 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
while(1) {
|
2018-04-17 18:08:47 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
os_runloop_once();
|
2018-04-02 03:00:27 +02:00
|
|
|
|
2018-04-17 18:46:01 +02:00
|
|
|
// LED indicators for viusalizing LoRaWAN state
|
2018-04-02 03:00:27 +02:00
|
|
|
if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) {
|
2018-04-17 22:42:06 +02:00
|
|
|
// 5 quick blinks 20ms on each 1/5 second while joining
|
|
|
|
set_LED(COLOR_YELLOW, 20, 200, 5);
|
2018-04-02 23:27:38 +02:00
|
|
|
// TX data pending
|
2018-04-02 03:00:27 +02:00
|
|
|
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
|
2018-04-17 22:42:06 +02:00
|
|
|
// 3 small blink 10ms on each 1/2sec (not when joining)
|
|
|
|
set_LED(COLOR_BLUE, 10, 500, 3);
|
2018-04-02 23:27:38 +02:00
|
|
|
// This should not happen so indicate a problem
|
2018-04-02 03:13:01 +02:00
|
|
|
} else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) {
|
2018-04-17 22:42:06 +02:00
|
|
|
// 5 heartbeat long blink 200ms on each 2 seconds
|
|
|
|
set_LED(COLOR_RED, 200, 2000, 5);
|
2018-04-02 03:13:01 +02:00
|
|
|
} else {
|
2018-04-02 23:27:38 +02:00
|
|
|
// led off
|
2018-04-17 18:46:01 +02:00
|
|
|
set_LED(COLOR_NONE, 0, 0, 0);
|
2018-04-02 03:00:27 +02:00
|
|
|
}
|
2018-04-17 18:08:47 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
vTaskDelay(10/portTICK_PERIOD_MS);
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end LMIC specific parts --------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* beginn hardware specific parts -------------------------------------------------------- */
|
|
|
|
|
2018-03-21 17:34:11 +01:00
|
|
|
#ifdef HAS_DISPLAY
|
|
|
|
HAS_DISPLAY u8x8(OLED_RST, OLED_SCL, OLED_SDA);
|
|
|
|
#endif
|
|
|
|
|
2018-03-24 13:38:43 +01:00
|
|
|
#ifdef HAS_ANTENNA_SWITCH
|
2018-03-18 19:45:17 +01:00
|
|
|
// defined in antenna.cpp
|
2018-03-24 13:38:43 +01:00
|
|
|
void antenna_init();
|
|
|
|
void antenna_select(const int8_t _ant);
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-14 20:22:58 +02:00
|
|
|
#ifndef BLECOUNTER
|
2018-04-07 12:02:13 +02:00
|
|
|
bool btstop = btStop();
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_BUTTON
|
2018-03-25 23:38:54 +02:00
|
|
|
// Button Handling, board dependent -> perhaps to be moved to hal/<$board.h>
|
2018-03-18 19:45:17 +01:00
|
|
|
// IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855
|
|
|
|
void IRAM_ATTR isr_button_pressed(void) {
|
2018-03-25 23:38:54 +02:00
|
|
|
ButtonTriggered = true; }
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* end hardware specific parts -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/* begin wifi specific parts ---------------------------------------------------------- */
|
|
|
|
|
|
|
|
// defined in wifisniffer.cpp
|
|
|
|
void wifi_sniffer_init(void);
|
|
|
|
void wifi_sniffer_set_channel(uint8_t channel);
|
|
|
|
void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);
|
|
|
|
|
2018-04-14 19:22:29 +02:00
|
|
|
// defined in blescan.cpp
|
|
|
|
void bt_loop(void *ignore);
|
|
|
|
|
2018-04-05 08:48:24 +02:00
|
|
|
// Sniffer Task
|
|
|
|
void sniffer_loop(void * pvParameters) {
|
2018-03-18 19:45:17 +01:00
|
|
|
|
|
|
|
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
|
2018-04-15 22:21:30 +02:00
|
|
|
channel=0;
|
2018-04-05 08:48:24 +02:00
|
|
|
char buff[16];
|
2018-03-31 23:22:43 +02:00
|
|
|
int nloop=0, lorawait=0;
|
2018-04-05 08:48:24 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
while (true) {
|
2018-04-02 19:04:10 +02:00
|
|
|
|
2018-04-05 21:47:43 +02:00
|
|
|
nloop++; // actual number of wifi loops, controls cycle when data is sent
|
2018-04-02 19:04:10 +02:00
|
|
|
|
2018-04-02 01:36:14 +02:00
|
|
|
vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS);
|
2018-03-18 19:45:17 +01:00
|
|
|
yield();
|
2018-04-02 19:04:10 +02:00
|
|
|
channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotates variable channel 1..WIFI_CHANNEL_MAX
|
2018-04-02 01:36:14 +02:00
|
|
|
wifi_sniffer_set_channel(channel);
|
2018-04-15 12:12:06 +02:00
|
|
|
ESP_LOGD(TAG, "Wifi set channel %d", channel);
|
2018-04-15 22:31:19 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
// duration of one wifi scan loop reached? then send data and begin new scan cycle
|
2018-04-07 12:02:13 +02:00
|
|
|
if ( nloop >= ( (100 / cfg.wifichancycle) * (cfg.wifiscancycle * 2)) +1 ) {
|
2018-04-02 21:02:22 +02:00
|
|
|
nloop=0; channel=0; // reset wifi scan + channel loop counter
|
2018-04-02 19:04:10 +02:00
|
|
|
do_send(&sendjob); // Prepare and execute LoRaWAN data upload
|
2018-03-18 19:45:17 +01:00
|
|
|
vTaskDelay(500/portTICK_PERIOD_MS);
|
|
|
|
yield();
|
|
|
|
|
|
|
|
// clear counter if not in cumulative counter mode
|
2018-03-30 20:41:08 +02:00
|
|
|
if (cfg.countermode != 1) {
|
2018-04-02 19:04:10 +02:00
|
|
|
macs.clear(); // clear all macs container
|
|
|
|
wifis.clear(); // clear Wifi macs couner
|
2018-04-02 01:36:14 +02:00
|
|
|
#ifdef BLECOUNTER
|
2018-04-07 12:02:13 +02:00
|
|
|
bles.clear(); // clear BLE macs counter
|
2018-04-02 14:34:16 +02:00
|
|
|
#endif
|
2018-04-04 12:39:40 +02:00
|
|
|
salt_reset(); // get new salt for salting hashes
|
2018-03-18 19:45:17 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 00:05:11 +02:00
|
|
|
// check if payload is sent
|
2018-03-18 19:45:17 +01:00
|
|
|
lorawait = 0;
|
|
|
|
while(LMIC.opmode & OP_TXRXPEND) {
|
2018-04-03 18:03:05 +02:00
|
|
|
if(!lorawait)
|
2018-04-15 23:50:53 +02:00
|
|
|
sprintf(display_lora, "LoRa wait");
|
2018-03-18 19:45:17 +01:00
|
|
|
lorawait++;
|
|
|
|
// in case sending really fails: reset and rejoin network
|
|
|
|
if( (lorawait % MAXLORARETRY ) == 0) {
|
|
|
|
ESP_LOGI(TAG, "Payload not sent, trying reset and rejoin");
|
|
|
|
esp_restart();
|
|
|
|
};
|
|
|
|
vTaskDelay(1000/portTICK_PERIOD_MS);
|
|
|
|
yield();
|
|
|
|
}
|
2018-04-17 22:42:06 +02:00
|
|
|
sprintf(display_lora, ""); // clear LoRa wait message fromd display
|
2018-04-16 11:03:12 +02:00
|
|
|
|
2018-04-02 19:04:10 +02:00
|
|
|
} // end of send data cycle
|
2018-04-03 18:13:39 +02:00
|
|
|
|
2018-04-14 20:33:33 +02:00
|
|
|
} // end of infinite wifi channel rotation loop
|
2018-03-18 19:45:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* end wifi specific parts ------------------------------------------------------------ */
|
|
|
|
|
|
|
|
// uptime counter 64bit to prevent millis() rollover after 49 days
|
|
|
|
uint64_t uptime() {
|
|
|
|
static uint32_t low32, high32;
|
|
|
|
uint32_t new_low32 = millis();
|
|
|
|
if (new_low32 < low32) high32++;
|
|
|
|
low32 = new_low32;
|
|
|
|
return (uint64_t) high32 << 32 | low32;
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:56:29 +02:00
|
|
|
#ifdef HAS_DISPLAY
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-17 18:08:47 +02:00
|
|
|
// Print a key on display
|
|
|
|
void DisplayKey(const uint8_t * key, uint8_t len, bool lsb) {
|
|
|
|
uint8_t start=lsb?len:0;
|
|
|
|
uint8_t end = lsb?0:len;
|
|
|
|
const uint8_t * p ;
|
|
|
|
for (uint8_t i=0; i<len ; i++) {
|
|
|
|
p = lsb ? key+len-i-1 : key+i;
|
|
|
|
u8x8.printf("%02X", *p);
|
|
|
|
}
|
|
|
|
u8x8.printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_display(const char *Productname, const char *Version) {
|
|
|
|
uint8_t buf[32];
|
|
|
|
u8x8.begin();
|
|
|
|
u8x8.setFont(u8x8_font_chroma48medium8_r);
|
|
|
|
u8x8.clear();
|
|
|
|
u8x8.setFlipMode(0);
|
|
|
|
u8x8.setInverseFont(1);
|
|
|
|
u8x8.draw2x2String(0, 0, Productname);
|
|
|
|
u8x8.setInverseFont(0);
|
|
|
|
u8x8.draw2x2String(2, 2, Productname);
|
|
|
|
delay(1500);
|
|
|
|
u8x8.clear();
|
2018-04-03 23:42:43 +02:00
|
|
|
u8x8.setFlipMode(1);
|
2018-04-17 18:08:47 +02:00
|
|
|
u8x8.setInverseFont(1);
|
|
|
|
u8x8.draw2x2String(0, 0, Productname);
|
|
|
|
u8x8.setInverseFont(0);
|
|
|
|
u8x8.draw2x2String(2, 2, Productname);
|
|
|
|
delay(1500);
|
|
|
|
|
|
|
|
u8x8.setFlipMode(0);
|
|
|
|
u8x8.clear();
|
|
|
|
|
|
|
|
#ifdef DISPLAY_FLIP
|
|
|
|
u8x8.setFlipMode(1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Display chip information
|
|
|
|
#ifdef VERBOSE
|
|
|
|
esp_chip_info_t chip_info;
|
|
|
|
esp_chip_info(&chip_info);
|
|
|
|
u8x8.printf("ESP32 %d cores\nWiFi%s%s\n",
|
|
|
|
chip_info.cores,
|
|
|
|
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
|
|
|
|
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
|
|
|
|
u8x8.printf("ESP Rev.%d\n", chip_info.revision);
|
|
|
|
u8x8.printf("%dMB %s Flash\n", spi_flash_get_chip_size() / (1024 * 1024),
|
|
|
|
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "int." : "ext.");
|
|
|
|
#endif // VERBOSE
|
|
|
|
|
|
|
|
u8x8.print(Productname);
|
|
|
|
u8x8.print(" v");
|
|
|
|
u8x8.println(PROGVERSION);
|
|
|
|
u8x8.println("DEVEUI:");
|
|
|
|
os_getDevEui((u1_t*) buf);
|
|
|
|
DisplayKey(buf, 8, true);
|
|
|
|
delay(5000);
|
|
|
|
u8x8.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void refreshDisplay() {
|
|
|
|
// update counter display (lines 0-4)
|
|
|
|
char buff[16];
|
|
|
|
snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value
|
|
|
|
u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE
|
|
|
|
u8x8.setCursor(0,4);
|
|
|
|
u8x8.printf("WIFI: %-4d", (int) wifis.size());
|
|
|
|
|
|
|
|
#ifdef BLECOUNTER
|
|
|
|
u8x8.setCursor(0,3);
|
|
|
|
if (cfg.blescan)
|
|
|
|
u8x8.printf("BLTH: %-4d", (int) bles.size());
|
|
|
|
else
|
|
|
|
u8x8.printf("%-16s", "BLTH: off");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// update wifi channel display (line 4)
|
|
|
|
u8x8.setCursor(11,4);
|
|
|
|
u8x8.printf("ch:%02i", channel);
|
|
|
|
|
|
|
|
// update RSSI limiter status display (line 5)
|
|
|
|
u8x8.setCursor(0,5);
|
|
|
|
u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %-4d", cfg.rssilimit);
|
|
|
|
|
|
|
|
// update LoRa status display (line 6)
|
|
|
|
u8x8.setCursor(0,6);
|
|
|
|
u8x8.printf("%-16s", display_lora);
|
|
|
|
|
|
|
|
// update LMiC event display (line 7)
|
|
|
|
u8x8.setCursor(0,7);
|
|
|
|
u8x8.printf("%-16s", display_lmic);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateDisplay() {
|
2018-04-17 19:28:58 +02:00
|
|
|
// timed display refresh according to refresh cycle setting
|
2018-04-17 18:46:01 +02:00
|
|
|
if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) {
|
2018-04-17 18:08:47 +02:00
|
|
|
refreshDisplay();
|
2018-04-17 18:46:01 +02:00
|
|
|
previousDisplaymillis += DISPLAYREFRESH_MS;
|
2018-04-17 18:08:47 +02:00
|
|
|
}
|
|
|
|
// set display on/off according to current device configuration
|
|
|
|
if (DisplayState != cfg.screenon) {
|
|
|
|
DisplayState = cfg.screenon;
|
|
|
|
u8x8.setPowerSave(!cfg.screenon);
|
|
|
|
}
|
|
|
|
} // updateDisplay()
|
|
|
|
|
2018-04-03 23:42:43 +02:00
|
|
|
|
2018-04-16 20:56:29 +02:00
|
|
|
#endif // HAS_DISPLAY
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-17 18:08:47 +02:00
|
|
|
#ifdef HAS_BUTTON
|
|
|
|
void readButton() {
|
|
|
|
if (ButtonTriggered) {
|
|
|
|
ButtonTriggered = false;
|
|
|
|
ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults");
|
|
|
|
eraseConfig();
|
|
|
|
esp_restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_LED
|
2018-04-17 22:42:06 +02:00
|
|
|
void switchLED() {
|
2018-04-17 18:08:47 +02:00
|
|
|
static bool previousLEDState;
|
|
|
|
// led need to change state? avoid digitalWrite() for nothing
|
|
|
|
if (LEDState != previousLEDState) {
|
|
|
|
#ifdef LED_ACTIVE_LOW
|
|
|
|
digitalWrite(HAS_LED, !LEDState);
|
2018-04-17 22:42:06 +02:00
|
|
|
#else
|
|
|
|
digitalWrite(HAS_LED, LEDState);
|
2018-04-17 18:08:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_RGB_LED
|
|
|
|
rgb_set_color(color);
|
|
|
|
#endif
|
|
|
|
previousLEDState = LEDState;
|
2018-04-17 22:42:06 +02:00
|
|
|
LEDcount--; // decrement blink counter
|
2018-04-17 18:08:47 +02:00
|
|
|
}
|
2018-04-17 22:42:06 +02:00
|
|
|
}; // switchLED()
|
|
|
|
|
|
|
|
void switchLEDstate() {
|
|
|
|
if (!LEDcount) // no more blinks? -> switch off LED
|
|
|
|
LEDState = 0;
|
|
|
|
else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks
|
|
|
|
LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? 1 : 0;
|
|
|
|
} // switchLEDstate()
|
2018-04-17 18:08:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
/* begin Aruino SETUP ------------------------------------------------------------ */
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// disable brownout detection
|
2018-03-27 11:05:06 +02:00
|
|
|
#ifdef DISABLE_BROWNOUT
|
2018-04-03 17:47:11 +02:00
|
|
|
// register with brownout is at address DR_REG_RTCCNTL_BASE + 0xd4
|
2018-03-27 11:05:06 +02:00
|
|
|
(*((volatile uint32_t *)ETS_UNCACHED_ADDR((DR_REG_RTCCNTL_BASE+0xd4)))) = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
// setup debug output or silence device
|
|
|
|
#ifdef VERBOSE
|
|
|
|
Serial.begin(115200);
|
|
|
|
esp_log_level_set("*", ESP_LOG_VERBOSE);
|
|
|
|
#else
|
|
|
|
// mute logs completely by redirecting them to silence function
|
|
|
|
esp_log_level_set("*", ESP_LOG_NONE);
|
|
|
|
esp_log_set_vprintf(redirect_log);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION);
|
2018-04-04 01:26:05 +02:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// initialize system event handler for wifi task, needed for wifi_sniffer_init()
|
2018-03-21 17:34:11 +01:00
|
|
|
esp_event_loop_init(NULL, NULL);
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// print chip information on startup if in verbose mode
|
2018-03-18 19:45:17 +01:00
|
|
|
#ifdef VERBOSE
|
|
|
|
esp_chip_info_t chip_info;
|
|
|
|
esp_chip_info(&chip_info);
|
|
|
|
ESP_LOGI(TAG, "This is ESP32 chip with %d CPU cores, WiFi%s%s, silicon revision %d, %dMB %s Flash",
|
|
|
|
chip_info.cores,
|
|
|
|
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
|
|
|
|
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "",
|
|
|
|
chip_info.revision, spi_flash_get_chip_size() / (1024 * 1024),
|
|
|
|
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
|
2018-03-21 14:33:55 +01:00
|
|
|
ESP_LOGI(TAG, "ESP32 SDK: %s", ESP.getSdkVersion());
|
2018-03-25 23:38:54 +02:00
|
|
|
#endif
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// read settings from NVRAM
|
2018-03-18 19:45:17 +01:00
|
|
|
loadConfig(); // includes initialize if necessary
|
2018-04-02 03:00:27 +02:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// initialize led if needed
|
2018-03-18 19:45:17 +01:00
|
|
|
#ifdef HAS_LED
|
2018-03-21 17:34:11 +01:00
|
|
|
pinMode(HAS_LED, OUTPUT);
|
2018-04-17 22:42:06 +02:00
|
|
|
set_LED(COLOR_NONE, 0, 0, 0); // LED off
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// initialize button handling if needed
|
2018-03-25 23:38:54 +02:00
|
|
|
#ifdef HAS_BUTTON
|
2018-03-27 12:45:09 +02:00
|
|
|
#ifdef BUTTON_PULLUP
|
|
|
|
// install button interrupt (pullup mode)
|
|
|
|
pinMode(HAS_BUTTON, INPUT_PULLUP);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, RISING);
|
|
|
|
#else
|
|
|
|
// install button interrupt (pulldown mode)
|
|
|
|
pinMode(HAS_BUTTON, INPUT_PULLDOWN);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, FALLING);
|
|
|
|
#endif
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// initialize wifi antenna if needed
|
2018-03-24 13:38:43 +01:00
|
|
|
#ifdef HAS_ANTENNA_SWITCH
|
2018-03-18 19:45:17 +01:00
|
|
|
antenna_init();
|
|
|
|
#endif
|
|
|
|
|
2018-04-16 20:56:29 +02:00
|
|
|
#ifdef HAS_DISPLAY
|
2018-04-03 18:03:05 +02:00
|
|
|
// initialize display
|
2018-04-17 18:08:47 +02:00
|
|
|
init_display(PROGNAME, PROGVERSION);
|
|
|
|
DisplayState = cfg.screenon;
|
2018-04-03 17:47:11 +02:00
|
|
|
u8x8.setPowerSave(!cfg.screenon); // set display off if disabled
|
2018-04-14 20:22:58 +02:00
|
|
|
u8x8.draw2x2String(0, 0, "PAX:0");
|
|
|
|
u8x8.setCursor(0,4);
|
|
|
|
u8x8.printf("WIFI: 0");
|
|
|
|
#ifdef BLECOUNTER
|
|
|
|
u8x8.setCursor(0,3);
|
2018-04-15 12:12:06 +02:00
|
|
|
u8x8.printf("BLTH: 0");
|
2018-04-14 20:22:58 +02:00
|
|
|
#endif
|
2018-04-03 17:47:11 +02:00
|
|
|
u8x8.setCursor(0,5);
|
2018-04-04 14:22:15 +02:00
|
|
|
u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %d", cfg.rssilimit);
|
2018-04-15 23:50:53 +02:00
|
|
|
sprintf(display_lora, "Join wait");
|
2018-04-16 20:56:29 +02:00
|
|
|
#endif
|
2018-04-04 01:26:05 +02:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// output LoRaWAN keys to console
|
2018-03-18 19:45:17 +01:00
|
|
|
#ifdef VERBOSE
|
|
|
|
printKeys();
|
2018-04-03 17:47:11 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
os_init(); // setup LMIC
|
|
|
|
os_setCallback(&initjob, lora_init); // setup initial job & join network
|
|
|
|
wifi_sniffer_init(); // setup wifi in monitor mode and start MAC counting
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// initialize salt value using esp_random() called by random() in arduino-esp32 core
|
|
|
|
// note: do this *after* wifi has started, since gets it's seed from RF noise
|
2018-04-04 12:39:40 +02:00
|
|
|
salt_reset(); // get new 16bit for salting hashes
|
2018-03-18 19:45:17 +01:00
|
|
|
|
2018-04-15 14:32:26 +02:00
|
|
|
// run wifi task on core 0 and lora task on core 1 and bt task on core 0
|
|
|
|
ESP_LOGI(TAG, "Starting Lora task on core 1");
|
|
|
|
xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1);
|
|
|
|
ESP_LOGI(TAG, "Starting Wifi task on core 0");
|
2018-04-15 22:21:30 +02:00
|
|
|
xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 16384, ( void * ) 1, 1, NULL, 0);
|
2018-04-15 14:32:26 +02:00
|
|
|
#ifdef BLECOUNTER
|
|
|
|
if (cfg.blescan) { // start BLE task only if BLE function is enabled in NVRAM configuration
|
|
|
|
ESP_LOGI(TAG, "Starting Bluetooth task on core 0");
|
2018-04-15 22:21:30 +02:00
|
|
|
xTaskCreatePinnedToCore(bt_loop, "btscan", 16384, NULL, 5, NULL, 0);
|
2018-04-15 14:32:26 +02:00
|
|
|
}
|
2018-03-18 19:45:17 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-03 17:47:11 +02:00
|
|
|
// Finally: kickoff first sendjob and join, then send initial payload "0000"
|
|
|
|
uint8_t mydata[] = "0000";
|
|
|
|
do_send(&sendjob);
|
2018-03-18 19:45:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* end Aruino SETUP ------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
/* begin Aruino LOOP ------------------------------------------------------------ */
|
|
|
|
|
|
|
|
// Arduino main moop, runs on core 1
|
|
|
|
// https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/
|
|
|
|
void loop() {
|
2018-04-16 00:28:55 +02:00
|
|
|
|
2018-04-17 19:20:54 +02:00
|
|
|
uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit)
|
|
|
|
currentMillis = millis(); // timebase for state machine in milliseconds (32bit)
|
2018-04-17 19:28:58 +02:00
|
|
|
|
|
|
|
// simple state machine for controlling display, LED, button, etc.
|
2018-04-15 18:54:23 +02:00
|
|
|
|
2018-04-17 18:08:47 +02:00
|
|
|
#ifdef HAS_BUTTON
|
|
|
|
readButton();
|
2018-04-15 22:21:30 +02:00
|
|
|
#endif
|
2018-04-15 23:50:53 +02:00
|
|
|
|
2018-04-17 18:08:47 +02:00
|
|
|
#ifdef HAS_DISPLAY
|
|
|
|
updateDisplay();
|
|
|
|
#endif
|
2018-04-15 23:50:53 +02:00
|
|
|
|
2018-04-17 18:08:47 +02:00
|
|
|
#ifdef HAS_LED
|
2018-04-17 22:42:06 +02:00
|
|
|
switchLEDstate();
|
|
|
|
switchLED();
|
2018-04-15 22:21:30 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-17 19:28:58 +02:00
|
|
|
//sendPayload();
|
2018-04-15 18:54:23 +02:00
|
|
|
|
2018-03-18 19:45:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* end Aruino LOOP ------------------------------------------------------------ */
|