Skip to content

Commit

Permalink
Merge pull request #855 from cyberman54/development
Browse files Browse the repository at this point in the history
v3.2.0
  • Loading branch information
cyberman54 authored Feb 16, 2022
2 parents d21d478 + 64eaf53 commit 19ade61
Show file tree
Hide file tree
Showing 52 changed files with 578 additions and 636 deletions.
59 changes: 21 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Output of sensor and peripheral data is internally switched by a bitmask registe

# Power saving mode

Paxcounter supports a battery friendly power saving mode. In this mode the device enters deep sleep, after all data is polled from all sensors and the dataset is completeley sent through all user configured channels (LORAWAN / SPI / MQTT). Set *#define SLEEPCYCLE* in paxcounter.conf to enable power saving mode and to specify the duration of a sleep cycle. Power consumption in deep sleep mode depends on your hardware, i.e. if on board peripherals can be switched off or set to a chip specific sleep mode either by MCU or by power management unit (PMU) as found on TTGO T-BEAM v1.0/V1.1. See *power.cpp* for power management, and *reset.cpp* for sleep and wakeup logic.
Paxcounter supports a battery friendly power saving mode. In this mode the device enters deep sleep, after all data is polled from all sensors and the dataset is completeley sent through all user configured channels (LORAWAN / SPI / MQTT / SD-Card). Set *#define SLEEPCYCLE* in paxcounter.conf to enable power saving mode and to specify the duration of a sleep cycle. Power consumption in deep sleep mode depends on your hardware, i.e. if on board peripherals can be switched off or set to a chip specific sleep mode either by MCU or by power management unit (PMU) as found on TTGO T-BEAM v1.0/V1.1. See *power.cpp* for power management, and *reset.cpp* for sleep and wakeup logic.

# Time sync

Expand All @@ -216,54 +216,38 @@ Paxcounter can be used to sync a wall clock which has a DCF77 or IF482 time tele

This describes how to set up a mobile PaxCounter:<br> Follow all steps so far for preparing the device, selecting the packed payload format. In `paxcounter.conf` set PAYLOAD_OPENSENSEBOX to 1. Register a new sensebox on https://opensensemap.org/. In the sensor configuration select "TheThingsNetwork" and set decoding profile to "LoRa serialization". Enter your TTN Application and Device ID. Setup decoding option using `[{"decoder":"latLng"},{"decoder":"uint16",sensor_id":"yoursensorid"}]`

# Covid-19 Exposure Notification System beacon detection (currently NOT working with v3.0.x, use v2.4.x for this feature)

Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this <A HREF="https://linux-fuer-wi.blogspot.com/2020/10/suche-die-zahl-64879.html">gives an indication</A> how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best results with this funcion, use following settings in `paxcounter.conf`:

#define COUNT_ENS 1 // enable ENS monitoring function
#define BLECOUNTER 1 // enable bluetooth sniffing
#define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed)
#define HAS_SENSOR_1 1 // optional, in board's hal file: transmit ENS counter data to server

# SD-card

Data can be stored on an SD-card if one is availabe. Simply choose the file in src/hal and add the following lines to your hal-file:
Data can be stored on SD-card if the board provides an SD card interface, either with SPI or MMC mode. To enable this feature, specify interface mode and hardware pins in board's hal file (src/hal/<board.h>):

#define HAS_SDCARD 1 // SD-card-reader/writer, using SPI interface
#define HAS_SDCARD 1 // SD-card interface, using SPI mode
OR
#define HAS_SDCARD 2 // SD-card-reader/writer, using SDMMC interface
#define HAS_SDCARD 2 // SD-card interface, using MMC mode

// Pins for SPI interface
#define SDCARD_CS (13) // fill in the correct numbers for your board
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)

Please choose the correct number for the connection of the reader/writer.

This is an example of a board with SD-card: https://www.aliexpress.com/item/32990008126.html
In this case you take the file src/hal/ttgov21new.h and add the lines given above (numbers given are for this board).
This is an example of a board with MMC SD-card interface: https://www.aliexpress.com/item/32915894264.html. For this board use file src/hal/ttgov21new.h and add the lines given above.

Another approach would be this tiny board: https://www.aliexpress.com/item/32424558182.html (needs 5V).
In this case you choose the correct file for your ESP32-board in the src/hal-directory and add the lines given above to the correct h-file. Please correct the numbers given in the example to the numbers used corresponding to your wiring.
In this case you choose the correct file for your ESP32-board in the src/hal-directory and add the lines given above. Edit the pin numbers given in the example, according to your wiring.

Some hints:
These cheap devices often handle SD-cards up to 32GB, not bigger ones. They can handle files in the old DOS-way, to say the filenames are in the 8.3-format. And they often cannot handle subdirectories.
Data is written on SD-card to a single file. After 3 write operations the data is flushed to the disk to minimize flash write cycles. Thus, up to the last 3 records of data will get lost when the PAXCOUNTER looses power during operation.

The software included here writes data in a file named PAXCOUNT.xx, where xx can range from 00 to 99. The software starts with 00, checks to see if such a file already exists and if yes it will continue with the next number (up to 99 - in this case it will return no sd-card). So an existing file will not be overwritten.
Format of the resulting file is CSV, thus easy import in LibreOffice, Excel, Influx, etc. Each record contains timestamp (in ISO8601 format), paxcount (wifi and ble) and battery voltage (optional). Voltage is logged if the device has a battery voltage sensor (to be configured in board hal file).

The data is written to the card and after 3 write-operations the data is flushed to the disk. So maybe the last 3 minutes of data get lost when you disconnect the PAXCOUNTER from power.
File contents example:

And finally: this is the data written to the disk:

date, time, wifi, bluet
00.00.1970,00:01:09,2,0
00.00.1970,00:02:09,1,0
00.00.1970,00:03:09,2,0

Format of the data is CSV, which can easily imported into LibreOffice, Excel, .....

If you want to change this please look into src/sdcard.cpp and include/sdcard.h.
timestamp,wifi,ble[,voltage]
2022-01-30T21:12:41Z,11,25[,4100]
2022-01-30T21:14:24Z,10,21[,4070]
2022-01-30T21:16:08Z,12,26[,4102]
2022-01-30T21:17:52Z,11,26[,4076]

If you want to change this, modify src/sdcard.cpp and include/sdcard.h.

# Integration into "The Things Stack Community Edition" aka "The Things Stack V3"

Expand Down Expand Up @@ -383,7 +367,7 @@ Hereafter described is the default *plain* format, which uses MSB bit numbering.

**Ports #10, #11, #12:** User sensor data

Format is specified by user in function `sensor_read(uint8_t sensor)`, see `src/sensor.cpp`. Port #10 is also used for ENS counter (2 bytes = 16 bit), if ENS is compiled AND ENS data transfer is enabled
Format is specified by user in function `sensor_read(uint8_t sensor)`, see `src/sensor.cpp`.

# Remote control

Expand Down Expand Up @@ -503,7 +487,7 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/
0x02 = RESERVED_DATA
0x04 = MEMS_DATA
0x08 = GPS_DATA
0x10 = SENSOR_1_DATA (also ENS counter)
0x10 = SENSOR_1_DATA
0x20 = SENSOR_2_DATA
0x40 = SENSOR_3_DATA
0x80 = BATT_DATA
Expand All @@ -524,10 +508,9 @@ Send for example `83` `86` as Downlink on Port 2 to get battery status and time/
0 = disabled
1 = enabled [default]

0x18 set ENS counter on/off
0x18 reserved

0 = disabled [default]
1 = enabled
unused, does nothing

0x19 set sleep cycle

Expand Down Expand Up @@ -621,4 +604,4 @@ Thanks to
- [terrillmoore](https://github.com/mcci-catena) for maintaining the LMIC for arduino LoRaWAN stack
- [sbamueller](https://github.com/sbamueller) for writing the tutorial in Make Magazine
- [Stefan](https://github.com/nerdyscout) for paxcounter opensensebox integration
- [August Quint](https://github.com/AugustQu) for adding SD card data logger, SDS011 and ENS support
- [August Quint](https://github.com/AugustQu) for adding SD card data logger and SDS011 support
16 changes: 0 additions & 16 deletions include/corona.h

This file was deleted.

5 changes: 1 addition & 4 deletions include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
#include "cyclic.h"
#include "qrcode.h"
#include "power.h"

#if (COUNT_ENS)
#include "corona.h"
#endif
#include "timekeeper.h"

#if (HAS_DISPLAY) == 1
#include <OneBitDisplay.h>
Expand Down
2 changes: 0 additions & 2 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ typedef struct __attribute__((packed)) {
uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4)
uint8_t rgblum; // RGB Led luminosity (0..100%)
uint8_t payloadmask; // bitswitches for payload data
uint8_t enscount; // 0=disabled 1= enabled

#ifdef HAS_BME680
uint8_t
Expand Down Expand Up @@ -111,6 +110,5 @@ typedef struct {
} sdsStatus_t;

extern char clientId[20]; // unique clientID
extern time_t _COMPILETIME; // epoch build time

#endif
7 changes: 2 additions & 5 deletions include/gpsread.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
#include <RtcDateTime.h>
#include "timekeeper.h"

#ifdef GPS_I2C // Needed for reading from I2C Bus
#include <Wire.h>
#ifndef GPS_BAUDRATE
#define GPS_BAUDRATE 115200UL
#endif

#define NMEA_FRAME_SIZE 82 // NEMA has a maxium of 82 bytes per record
#define NMEA_COMPENSATION_FACTOR 480 // empiric for Ublox Neo 6M

extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
extern TaskHandle_t GpsTask;

Expand Down
2 changes: 1 addition & 1 deletion include/irqhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
void irqHandler(void *pvParameters);
void mask_user_IRQ();
void unmask_user_IRQ();
void doIRQ(int irq);
void IRAM_ATTR doIRQ(int irq);

extern TaskHandle_t irqHandlerTask;

Expand Down
2 changes: 1 addition & 1 deletion include/libpax_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

void init_libpax(void);

extern uint16_t volatile libpax_macs_ble, libpax_macs_wifi; // libpax values
extern struct count_payload_t count_from_libpax; // libpax count storage

#endif
4 changes: 2 additions & 2 deletions include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <esp_coexist.h> // needed for coex version display
#include <esp_wifi.h> // needed for wifi init / deinit

#include <libpax_helpers.h>

#include "globals.h"
#include "reset.h"
#include "i2c.h"
Expand All @@ -18,9 +20,7 @@
#include "sensor.h"
#include "lorawan.h"
#include "timekeeper.h"
#include "corona.h"
#include "boot.h"
#include "libpax_helpers.h"
#include "power.h"
#include "antenna.h"

Expand Down
1 change: 1 addition & 0 deletions include/rcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <rom/rtc.h>

#include "libpax_helpers.h"
#include "senddata.h"
#include "cyclic.h"
#include "configmanager.h"
Expand Down
2 changes: 2 additions & 0 deletions include/reset.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "lorawan.h"
#include "display.h"
#include "power.h"
#include "sdcard.h"
#include "sds011read.h"

void reset_rtc_vars(void);
void do_reset(bool warmstart);
Expand Down
2 changes: 1 addition & 1 deletion include/rtctime.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
uint8_t rtc_init(void);
uint8_t set_rtctime(time_t t);
void sync_rtctime(void);
time_t get_rtctime(void);
time_t get_rtctime(uint16_t *msec);
float get_rtctemp(void);

#endif // _RTCTIME_H
15 changes: 8 additions & 7 deletions include/sdcard.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef _SDCARD_H
#define _SDCARD_H

#include <globals.h>
#include "globals.h"
#include <stdio.h>
#include <SPI.h>

#if (HAS_SDCARD)
#if HAS_SDCARD == 1
#include <mySD.h>
#include <SD.h>
#elif HAS_SDCARD == 2
#include <SD_MMC.h>
#else
Expand Down Expand Up @@ -54,14 +54,15 @@
#define SDCARD_DATA3 13
#endif

#define SDCARD_FILE_NAME "/paxcount.%02d"
#define SDCARD_FILE_HEADER "date, time, wifi, bluet"
#define SDCARD_FILE_NAME clientId
#define SDCARD_FILE_HEADER "timestamp,wifi,ble"

#if (COUNT_ENS)
#define SDCARD_FILE_HEADER_CWA ",cwa"
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
#define SDCARD_FILE_HEADER_VOLTAGE ",voltage"
#endif

bool sdcard_init(void);
bool sdcard_init(bool create = true);
void sdcard_close(void);
void sdcardWriteData(uint16_t, uint16_t, uint16_t = 0);

#endif // _SDCARD_H
13 changes: 11 additions & 2 deletions include/sds011read.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#ifndef _SDS011READ_H
#define _SDS011READ_H

#include <SDS011.h>
#include <SdsDustSensor.h>
#include "globals.h"

#define SDCARD_FILE_HEADER_SDS011 ", PM10,PM25"

// use original pins from HardwareSerial if none defined
#ifndef SDS_TX
#define SDS_TX -1
#endif
#ifndef SDS_RX
#define SDS_RX -1
#endif

extern bool isSDS011Active;

bool sds011_init();
void sds011_loop();
void sds011_sleep(void);
void sds011_wakeup(void);
void sds011_store(sdsStatus_t *sds_store);

#endif // _SDS011READ_H
10 changes: 2 additions & 8 deletions include/senddata.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _SENDDATA_H
#define _SENDDATA_H

#include "libpax_helpers.h"
#include "spislave.h"
#include "mqttclient.h"
#include "cyclic.h"
Expand All @@ -10,18 +9,13 @@
#include "display.h"
#include "sdcard.h"

#if (COUNT_ENS)
#include "corona.h"
#endif

extern struct count_payload_t count_from_libpax;

void SendPayload(uint8_t port);
void sendData(void);
void checkSendQueues(void);
void flushQueues(void);
bool allQueuesEmtpy(void);
void setSendIRQ(TimerHandle_t xTimer = NULL);
void setSendIRQ(TimerHandle_t xTimer);
void setSendIRQ(void);
void initSendDataTimer(uint8_t sendcycle);

#endif // _SENDDATA_H_
12 changes: 6 additions & 6 deletions include/timekeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "dcf77.h"
#include "esp_sntp.h"

#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch
#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch
#define LEAP_SECS_SINCE_GPSEPOCH 18UL // state of 2021

enum timesource_t { _gps, _rtc, _lora, _unsynced, _set };
Expand All @@ -20,22 +20,22 @@ extern const char timeSetSymbols[];
extern Ticker timesyncer;
extern timesource_t timeSource;
extern TaskHandle_t ClockTask;
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern DRAM_ATTR bool TimePulseTick; // 1sec pps flag set by GPS or RTC
extern DRAM_ATTR unsigned long lastPPS;
extern hw_timer_t *ppsIRQ;

void IRAM_ATTR CLOCKIRQ(void);
void IRAM_ATTR GPSIRQ(void);
void clock_init(void);
void clock_loop(void *pvParameters);
void timepulse_start(void);
void setTimeSyncIRQ(void);
uint8_t timepulse_init(void);
bool timeIsValid(time_t const t);
void calibrateTime(void);
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
timesource_t mytimesource);
time_t compileTime(const String compile_date);
time_t compileTime(void);
time_t mkgmtime(const struct tm *ptm);
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
int8_t rxPin, int8_t txPins);

#endif // _timekeeper_H
Loading

0 comments on commit 19ade61

Please sign in to comment.