sdcard support (experimental)

This commit is contained in:
Verkehrsrot 2019-12-25 23:07:34 +01:00
parent 92c4401db4
commit 86c3685f6c
12 changed files with 145 additions and 7 deletions

View File

@ -20,7 +20,7 @@ Paxcounter is a proof-of-concept device for metering passenger flows in realtime
Intention of this project is to do this without intrusion in privacy: You don't need to track people owned devices, if you just want to count them. Therefore, Paxcounter does not persistenly store MAC adresses and does no kind of fingerprinting the scanned devices.
Data is transferred to a server via a LoRaWAN network, and/or a wired SPI slave interface.
Data is transferred to a server via a LoRaWAN network, and/or a wired SPI slave interface. It can also be stored on a local SD-card.
You can build this project battery powered and reach a full day uptime with a single 18650 Li-Ion cell.
@ -33,7 +33,7 @@ This can all be done with a single small and cheap ESP32 board for less than $20
*LoRa & SPI*:
- Heltec: LoRa-32 v1 and v2
- TTGO: T1, T2, T3, T-Beam, T-Fox
- TTGO: T1*, T2*, T3*, T-Beam, T-Fox (* supports microSD-card)
- Pycom: LoPy, LoPy4, FiPy
- Radioshuttle.de: [ECO Power Board](https://www.radioshuttle.de/esp32-eco-power/esp32-eco-power-board/)
- WeMos: LoLin32 + [LoraNode32 shield](https://github.com/hallard/LoLin32-Lora),

16
include/sdcard.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _SDCARD_H
#define _SDCARD_H
#include <globals.h>
#include <stdio.h>
#include <SPI.h>
#include <mySD.h>
#define SDCARD_FILE_NAME "paxcount.%02d"
#define SDCARD_FILE_HEADER "date, time, wifi, bluet"
bool sdcardInit( void );
void sdcardWriteData( uint16_t, uint16_t);
#endif

View File

@ -12,6 +12,10 @@
#include "display.h"
#endif
#ifdef HAS_SDCARD
#include "sdcard.h"
#endif
extern Ticker sendcycler;
void SendPayload(uint8_t port, sendprio_t prio);

View File

@ -7,7 +7,7 @@
; ---> SELECT THE TARGET PLATFORM HERE! <---
[board]
halfile = generic.h
;halfile = generic.h
;halfile = ebox.h
;halfile = eboxtube.h
;halfile = ecopower.h
@ -16,7 +16,7 @@ halfile = generic.h
;halfile = ttgov1.h
;halfile = ttgov2.h
;halfile = ttgov21old.h
;halfile = ttgov21new.h
halfile = ttgov21new.h
;halfile = ttgofox.h
;halfile = ttgobeam.h
;halfile = ttgobeam10.h
@ -78,6 +78,7 @@ lib_deps_basic =
274@>=2.3.3 ; #274 RTC by Michael Miller
SimpleButton
AXP202X_Library@>=1.1.0 ; AXP202 PMU lib by Lewis He
esp32-micro-sdcard
lib_deps_all =
${common.lib_deps_basic}
${common.lib_deps_lora}

View File

@ -18,6 +18,14 @@
#define SPI_SCLK GPIO_NUM_18
#define SPI_CS GPIO_NUM_5
// enable only if you want to store a local paxcount table on the device
#define HAS_SDCARD 1 // this board has an SD-card-reader/writer
// Pins for SD-card
#define SDCARD_CS (13)
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)
// enable only if device has these sensors, otherwise comment these lines
// tutorial to connect BME sensor see here:
// https://sbamueller.wordpress.com/2019/02/26/paxcounter-mit-umweltsensor/

View File

@ -18,6 +18,14 @@
#define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW
#define HAS_BUTTON KEY_BUILTIN
// enable only if you want to store a local paxcount table on the device
#define HAS_SDCARD 1 // this board has an SD-card-reader/writer
// Pins for SD-card
#define SDCARD_CS (13)
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)
// Pins for I2C interface of OLED Display
#define MY_OLED_SDA (4)
#define MY_OLED_SCL (15)

View File

@ -19,6 +19,14 @@
// disable brownout detection (needed on TTGOv2 for battery powered operation)
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature
// enable only if you want to store a local paxcount table on the device
#define HAS_SDCARD 1 // this board has an SD-card-reader/writer
// Pins for SD-card
#define SDCARD_CS (13)
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)
// Pins for I2C interface of OLED Display
#define MY_OLED_SDA (21)
#define MY_OLED_SCL (22)

View File

@ -15,6 +15,14 @@
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
// enable only if you want to store a local paxcount table on the device
#define HAS_SDCARD 1 // this board has an SD-card-reader/writer
// Pins for SD-card
#define SDCARD_CS (13)
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)
// enable only if device has these sensors, otherwise comment these lines
// BME280 sensor on I2C bus
//#define HAS_BME 1 // Enable BME sensors in general

View File

@ -19,6 +19,14 @@
#define HAS_LED NOT_A_PIN // no usable LED on board
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature
// enable only if you want to store a local paxcount table on the device
#define HAS_SDCARD 1 // this board has an SD-card-reader/writer
// Pins for SD-card
#define SDCARD_CS (13)
#define SDCARD_MOSI (15)
#define SDCARD_MISO (2)
#define SDCARD_SCLK (14)
#define HAS_DISPLAY 1
#define DISPLAY_FLIP 1 // rotated display
//#define BAT_MEASURE_ADC ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7

View File

@ -323,6 +323,11 @@ void setup() {
assert(spi_init() == ESP_OK);
#endif
#if (HAS_SDCARD)
if ( sdcardInit() )
strcat_P(features, " SD");
#endif
#if (VENDORFILTER)
strcat_P(features, " FILTER");
#endif

67
src/sdcard.cpp Normal file
View File

@ -0,0 +1,67 @@
// routines for writing data to an SD-card, if present
// Local logging tag
static const char TAG[] = __FILE__;
#include "sdcard.h"
static bool useSDCard;
static void createFile(void);
File fileSDCard;
bool sdcardInit() {
ESP_LOGD(TAG, "looking for SD-card...");
useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
if (useSDCard)
createFile();
return useSDCard;
}
void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
static int counterWrites = 0;
char tempBuffer[12 + 1];
time_t t = now();
if (!useSDCard)
return;
ESP_LOGD(TAG, "writing to SD-card");
sprintf(tempBuffer, "%02d.%02d.%4d,", day(t), month(t), year(t));
fileSDCard.print(tempBuffer);
sprintf(tempBuffer, "%02d:%02d:%02d,", hour(t), minute(t), second(t));
fileSDCard.print(tempBuffer);
sprintf(tempBuffer, "%d,%d", noWifi, noBle);
fileSDCard.println(tempBuffer);
if (++counterWrites > 2) {
// force writing to SD-card
ESP_LOGD(TAG, "flushing data to card");
fileSDCard.flush();
counterWrites = 0;
}
}
void createFile(void) {
char bufferFilename[8 + 1 + 3 + 1];
useSDCard = false;
for (int i = 0; i < 100; i++) {
sprintf(bufferFilename, SDCARD_FILE_NAME, i);
ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
bool fileExists = SD.exists(bufferFilename);
if (!fileExists) {
ESP_LOGD(TAG, "SD: file does not exist: opening");
fileSDCard = SD.open(bufferFilename, FILE_WRITE);
if (fileSDCard) {
ESP_LOGD(TAG, "SD: name opended: <%s>", bufferFilename);
fileSDCard.println(SDCARD_FILE_HEADER);
useSDCard = true;
break;
}
}
}
return;
}

View File

@ -51,6 +51,11 @@ void SendPayload(uint8_t port, sendprio_t prio) {
spi_enqueuedata(&SendBuffer);
#endif
// write data to sdcard, if present
#ifdef HAS_SDCARD
sdcardWriteData(macs_wifi, macs_ble);
#endif
} // SendPayload
// interrupt triggered function to prepare payload to send
@ -72,7 +77,7 @@ void sendData() {
if (cfg.blescan)
payload.addCount(macs_ble, MAC_SNIFF_BLE);
#endif
#if (HAS_GPS)
#if (HAS_GPS)
if (GPSPORT == COUNTERPORT) {
// send GPS position only if we have a fix
if (gps.location.isValid()) {
@ -82,7 +87,7 @@ void sendData() {
ESP_LOGD(TAG, "No valid GPS position");
}
#endif
#if (PAYLOAD_OPENSENSEBOX)
#if (PAYLOAD_OPENSENSEBOX)
if (cfg.wifiscan)
payload.addCount(macs_wifi, MAC_SNIFF_WIFI);
if (cfg.blescan)
@ -110,7 +115,7 @@ void sendData() {
break;
#endif
#if (HAS_GPS)
#if (HAS_GPS)
case GPS_DATA:
if (GPSPORT != COUNTERPORT) {
// send GPS position only if we have a fix