Browse Source

Merge branch 'feature_rfidwakeup'

master
Torsten Stauder 5 years ago
parent
commit
bcbef358bb
  1. 32
      README.md
  2. 2
      src/logmessages.h
  3. 2
      src/logmessages_EN.h
  4. 147
      src/main.cpp
  5. 2
      src/settings-lolin32.h
  6. 2
      src/settings-lolin_d32.h
  7. 2
      src/settings-lolin_d32_pro.h
  8. 3
      src/settings.h

32
README.md

@ -27,6 +27,7 @@ Finally, the long announced Tonuino-PCB for Wemos' Lolin32 is [there](https://gi
* 23.12.2020: User-config is now split into general part (settings.h) and develboard-specific part (e.g. settings-lolin32.h)
* 13.01.2020: Added fileexlorer to webgui (thanks @grch87 for contribution!). Now files and directories can be renamed, uploaded and deleted via webgui.
* 17.01.2020: Added directive `STATIC_IP_ENABLE`: (optional) static IPv4-configuration
* 18.01.2020: Added directive `PN5180_ENABLE_LPCD`: awake from deepsleep with PN5180 is now possible (but needs another GPIO)
## Known bugs
* Some webstreams don't run. Guess it's a combination of saturated connection-pool and lack of heap-memory. Works probably better if ESP32-WROVER (e.g. Lolin D32 pro) is used, as this chip has PSRAM. Advice: Don't enable modules (e.g. MQTT) if you don't need them as this could save memory (and trouble).
* English translation for webgui is currently outdated. This will be fixed soon when i18n-support will be integrated.
@ -101,7 +102,7 @@ Advantages SD-MMC (1 bit) over SPI:
So why using SPI if SD-MMC seems to be better? The primary problem of SD-MMC is: you cannot choose different GPIOs. That doesn't sound bad but this can (depending on the uSD-card-reader-module) be a problem because maybe GPIO2 is pulled HIGH to 3.3V by a 10k-resistor. For example this is the case when using the reader-module named above in hardware-setup. It's a problem because if GPIO2 is pulled high at boot, ESP32 doesn't enter flash-mode. As soon as flash-mode is entered, it's no longer a problem. However, this behaviour can be an issue if ESP32 is deeply "burried" in Tonuino's enclosure and you want to update its firmware. But fortunately there's a way to bypass this problem: remove the [pullup-resistor shown in the picture](https://raw.githubusercontent.com/biologist79/Tonuino-ESP32-I2S/master/pictures/Pullup-removal.jpg). It can be removed safely because if MMC-mode is set, pullup is done in software using `pinMode(2, INPUT_PULLUP);`.
## RFID: RC522 or PN5180?
RC522 is so to say the Tonuino-standard. It's cheap and works, but RFID-tag has to be placed near the reader. PN5180 instead has better RFID range/sensitivity and can read ISO-15693 / iCode SLIX2-tags aka 'Tonies' (you need a password to read Tonies). Disadvantages: is a bit more expensive and needs more GPIOs (6/7 instead of 4). Refer PN5180's wire-section below for further informations. Hint: if using 3.3V make sure to connect PN5180 to +5V AND 3.3V. Sounds weird but it's necessary.
RC522 is so to say the Tonuino-standard. It's cheap and works, but RFID-tag has to be placed near the reader. PN5180 instead has better RFID range/sensitivity and can read ISO-15693 / iCode SLIX2-tags aka 'Tonies' (you need a password to read Tonies). You can also wake-up the board with the card. Disadvantages: is a bit more expensive and needs more GPIOs (6/7 instead of 4). Refer PN5180's wire-section below for further informations. Hint: if using 3.3V make sure to connect PN5180 to +5V AND 3.3V. Sounds weird but it's necessary.
## 3.3 or 5V?
* Why 3.3V? Because: if you plan to use battery-mode with a LiPo, there's no 5 V available (unless USB is connected).
@ -215,19 +216,22 @@ In this case RFID-reader + SD-reader share SPI's SCK, MISO and MOSI. But make su
## Wiring (PN5180 instead of MFRC522) different to above
PN5180 reader needs two more pins, RESET and BUSY. Double check pin-conflicts! `RFID_READER_TYPE_PN5180` needs to be enabled to use this feature. Make sure to disable `RFID_READER_TYPE_MFRC522` if doing so!
| ESP32 (GPIO) | Hardware | Pin | Comment |
| ------------- | --------------------- | ------ | ------------------------------------------------------------ |
| 3.3 V | PN5180 RFID-reader | 3.3V | Connect directly to GPIO 17 for power-saving when µC is off |
| 5 / 3.3 V | PN5180 RFID-reader | 5V | Don't forget to connect this pin the same way as 3.3V |
| GND | PN5180 RFID-reader | GND | |
| 21 | PN5180 RFID-reader | CS/SDA | Same as MFRC522. Don't share with SD! |
| 23 | PN5180 RFID-reader | MOSI | Same as MFRC522 |
| 19 | PN5180 RFID-reader | MISO | Same as MFRC522 |
| 18 | PN5180 RFID-reader | SCK | Same as MFRC522 |
| 16 | PN5180 RFID-reader | BUSY | be aware of SD MISO if running in SPI mode |
| 22 | PN5180 RFID-reader | RST | be aware of Headphone jack PIN |
PN5180-reader needs at least two more GPIOs: RESET and BUSY. Double check pin-conflicts! `RFID_READER_TYPE_PN5180` needs to be enabled to use this feature. Make sure to disable `RFID_READER_TYPE_MFRC522` if doing so!
You can enable low power card-detection with `PN5180_ENABLE_LPCD`, but this needs another GPIO for IRQ. With low power card detection (LPCD) you can wake-up the ESP32 from deep-sleep just by applying a card to the reader. You need a PN5180-firmware >= 4.0. Most china-boards comes with older firmware. The latest firmware can be flashed with this [project](https://github.com/abidxraihan/PN5180_Updater_ESP32).
| ESP32 (GPIO) | Hardware | Pin | Comment |
| ------------- | --------------------- | ------ | ----------------------------------------------------------------- |
| 3.3 V | PN5180 RFID-reader | 3.3V | Connect directly to GPIO 17 for power-saving when µC is off |
| 3.3 V | | 3.3V | For low power card detection mode (LPCD) connect directly to 3.3V |
| 5 / 3.3 V | PN5180 RFID-reader | 5V | Don't forget to connect this pin the same way as 3.3V |
| GND | PN5180 RFID-reader | GND | |
| 21 | PN5180 RFID-reader | CS/SDA | Same as MFRC522. Don't share with SD! |
| 23 | PN5180 RFID-reader | MOSI | Same as MFRC522 |
| 19 | PN5180 RFID-reader | MISO | Same as MFRC522 |
| 18 | PN5180 RFID-reader | SCK | Same as MFRC522 |
| 16 | PN5180 RFID-reader | BUSY | be aware of SD MISO if running in SPI mode |
| 22 | PN5180 RFID-reader | RST | be aware of Headphone jack PIN |
| 39 | PN5180 RFID-reader | IRQ | optional, used for low power card detection (LPCD) |
## Wiring (custom) / different pinout
When using a develboard with SD-card-reader already integrated (Lolin D32 Pro, several TTGO-boards), the pinouts described above my not fit. Feel free to change them according your needs. Additionaly some boards may use one or some of the GPIOs I used for their internal purposes and that reason for are maybe not exposed via pin-headers. However, having them exposed doesn't mean they can be used without limits. This is because some GPIOs have to be logical LOW or HIGH at start/boot for example and this is probably not the case when connecting stuff to it. Feel free to adjust the GPIOs proposed by me (but be adviced it could take a while to get it running). If you encounter problems please refer the board's manual first. <br />

2
src/logmessages.h

@ -171,3 +171,5 @@ static const char freeHeapWithFtp[] PROGMEM = "Freier Heap-Speicher nach FTP-Ins
static const char freeHeapAfterSetup[] PROGMEM = "Freier Heap-Speicher nach Setup-Routine";
static const char tryStaticIpConfig[] PROGMEM = "Statische IP-Konfiguration wird durchgeführt...";
static const char staticIPConfigFailed[] PROGMEM = "Statische IP-Konfiguration fehlgeschlagen";
static const char wakeUpRfidNoIso14443[] PROGMEM = "ESP32 wurde vom Kartenleser aus dem Deepsleep aufgeweckt. Allerdings wurde keine ISO-14443-Karte gefunden. Gehe zurück in den Deepsleep...";
static const char lowPowerCardSuccess[] PROGMEM = "Kartenerkennung via 'low power' erfolgreich durchgeführt";

2
src/logmessages_EN.h

@ -171,3 +171,5 @@ static const char freeHeapWithFtp[] PROGMEM = "Free heap after FTP-allocation";
static const char freeHeapAfterSetup[] PROGMEM = "Free heap after setup";
static const char tryStaticIpConfig[] PROGMEM = "Performing static IP-configuration...";
static const char staticIPConfigFailed[] PROGMEM = "Static IP-configuration failed";
static const char wakeUpRfidNoIso14443[] PROGMEM = "Wakeup caused by low power card-detection. RF-field changed but no ISO-14443 card on reader was found. So I'll return back to sleep now...";
static const char lowPowerCardSuccess[] PROGMEM = "Switch to low power card-detection: success";

147
src/main.cpp

@ -228,6 +228,7 @@ char *currentRfidTagId = NULL;
unsigned long lastTimeActiveTimestamp = 0; // Timestamp of last user-interaction
unsigned long sleepTimerStartTimestamp = 0; // Flag if sleep-timer is active
bool gotoSleep = false; // Flag for turning uC immediately into deepsleep
bool sleeping = false; // Flag for turning into deepsleep is in progress
bool lockControls = false; // Flag if buttons and rotary encoder is locked
bool bootComplete = false;
// Rotary encoder-helper
@ -1879,6 +1880,8 @@ void rfidScanner(void *parameter) {
for (;;) {
esp_task_wdt_reset();
if (sleeping)
break;
vTaskDelay(10);
if ((millis() - lastRfidCheckTimestamp) >= RFID_SCAN_INTERVAL) {
// Reset the loop if no new card is present on the sensor/reader. This saves the entire process when idle.
@ -1964,6 +1967,7 @@ void rfidScanner(void *parameter) {
}
}
}
//Serial.println("deleted RFID scanner-task");
vTaskDelete(NULL);
}
#endif
@ -2304,9 +2308,8 @@ void showLed(void *parameter) {
for (uint8_t led = 0; led < numLedsToLight; led++) {
if (lockControls) {
leds[ledAddress(led)] = CRGB::Red;
} else if (!playProperties.pausePlay) {
// leds[ledAddress(led)].setHue((uint8_t) (85 - ((double) 95 / NUM_LEDS) * led)); // green to red
leds[ledAddress(led)].setHue((uint8_t) ((double) 255 / NUM_LEDS) * led); // Hue-rainbow
} else if (!playProperties.pausePlay) { // Hue-rainbow
leds[ledAddress(led)].setHue((uint8_t) (85 - ((double) 95 / NUM_LEDS) * led));
}
}
if (playProperties.pausePlay) {
@ -2365,12 +2368,51 @@ void sleepHandler(void) {
}
}
#ifdef PN5180_ENABLE_LPCD
// goto low power card detection mode
void gotoLPCD() {
static PN5180 nfc(RFID_CS, RFID_BUSY, RFID_RST);
nfc.begin();
// show PN5180 reader version
uint8_t firmwareVersion[2];
nfc.readEEprom(FIRMWARE_VERSION, firmwareVersion, sizeof(firmwareVersion));
Serial.print(F("Firmware version="));
Serial.print(firmwareVersion[1]);
Serial.print(".");
Serial.println(firmwareVersion[0]);
// check firmware version: PN5180 firmware < 4.0 has several bugs preventing the LPCD mode
// you can flash latest firmware with this project: https://github.com/abidxraihan/PN5180_Updater_ESP32
if (firmwareVersion[1] < 4) {
Serial.println(F("This PN5180 firmware does not work with LPCD! use firmware >= 4.0"));
return;
}
Serial.println(F("prepare low power card detection..."));
nfc.prepareLPCD();
nfc.clearIRQStatus(0xffffffff);
Serial.print(F("PN5180 IRQ PIN: ")); Serial.println(digitalRead(RFID_IRQ));
// turn on LPCD
uint16_t wakeupCounterInMs = 0x3FF; // must be in the range of 0x0 - 0xA82. max wake-up time is 2960 ms.
if (nfc.switchToLPCD(wakeupCounterInMs)) {
Serial.println(F("switch to low power card detection: success"));
// configure wakeup pin for deep-sleep wake-up, use ext1
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
// freeze pin states in deep sleep
gpio_hold_en(gpio_num_t(RFID_CS)); // CS/NSS
gpio_hold_en(gpio_num_t(RFID_RST)); // RST
gpio_deep_sleep_hold_en();
} else {
Serial.println(F("switchToLPCD failed"));
}
}
#endif
// Puts uC to deep-sleep if flag is set
void deepSleepManager(void) {
if (gotoSleep) {
if (sleeping)
return;
sleeping = true;
loggerNl((char *) FPSTR(goToSleepNow), LOGLEVEL_NOTICE);
Serial.flush();
#ifdef MQTT_ENABLE
publishMqtt((char *) FPSTR(topicState), "Offline", false);
publishMqtt((char *) FPSTR(topicTrackState), "---", false);
@ -2380,10 +2422,22 @@ void deepSleepManager(void) {
FastLED.clear();
FastLED.show();
#endif
/*SPI.end();
spiSD.end();*/
// SD card goto idle mode
#ifdef SD_MMC_1BIT_MODE
SD_MMC.end();
#else
/*SPI.end();
spiSD.end();*/
#endif
Serial.flush();
// switch off power
digitalWrite(POWER, LOW);
delay(200);
#ifdef PN5180_ENABLE_LPCD
// prepare and go to low power card detection mode
gotoLPCD();
#endif
Serial.println(F("deep-sleep, good night......."));
esp_deep_sleep_start();
}
}
@ -3242,8 +3296,6 @@ wl_status_t wifiManager(void) {
return WiFi.status();
}
const char mqttTab[] PROGMEM = "<a class=\"nav-item nav-link\" id=\"nav-mqtt-tab\" data-toggle=\"tab\" href=\"#nav-mqtt\" role=\"tab\" aria-controls=\"nav-mqtt\" aria-selected=\"false\"><i class=\"fas fa-network-wired\"></i> MQTT</a>";
const char ftpTab[] PROGMEM = "<a class=\"nav-item nav-link\" id=\"nav-ftp-tab\" data-toggle=\"tab\" href=\"#nav-ftp\" role=\"tab\" aria-controls=\"nav-ftp\" aria-selected=\"false\"><i class=\"fas fa-folder\"></i> FTP</a>";
// Used for substitution of some variables/templates of html-files. Is called by webserver's template-engine
String templateProcessor(const String& templ) {
@ -3255,12 +3307,6 @@ String templateProcessor(const String& templ) {
return String(ftpUserLength-1);
} else if (templ == "FTP_PWD_LENGTH") {
return String(ftpPasswordLength-1);
} else if (templ == "SHOW_FTP_TAB") { // Only show FTP-tab if FTP-support was compiled
#ifdef FTP_ENABLE
return (String) FPSTR(ftpTab);
#else
return String();
#endif
} else if (templ == "INIT_LED_BRIGHTNESS") {
return String(prefsSettings.getUChar("iLedBrightness", 0));
} else if (templ == "NIGHT_LED_BRIGHTNESS") {
@ -3283,12 +3329,6 @@ String templateProcessor(const String& templ) {
return String(prefsSettings.getUInt("vCheckIntv", voltageCheckInterval));
} else if (templ == "MQTT_SERVER") {
return prefsSettings.getString("mqttServer", "-1");
} else if (templ == "SHOW_MQTT_TAB") { // Only show MQTT-tab if MQTT-support was compiled
#ifdef MQTT_ENABLE
return (String) FPSTR(mqttTab);
#else
return String();
#endif
} else if (templ == "MQTT_ENABLE") {
if (enableMqtt) {
return String("checked=\"checked\"");
@ -4058,10 +4098,66 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
#endif
}
// Print the wake-up reason why ESP32 is awake now
void printWakeUpReason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println(F("Wakeup caused by push button")); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println(F("Wakeup caused by low power card detection")); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println(F("Wakeup caused by timer")); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println(F("Wakeup caused by touchpad")); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println(F("Wakeup caused by ULP program")); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
#ifdef PN5180_ENABLE_LPCD
// wake up from LPCD, check card is present. This works only for ISO-14443 compatible cards
void checkCardIsPresentLPCD() {
static PN5180ISO14443 nfc14443(RFID_CS, RFID_BUSY, RFID_RST);
nfc14443.begin();
nfc14443.reset();
nfc14443.setupRF();
if (!nfc14443.isCardPresent()) {
nfc14443.clearIRQStatus(0xffffffff);
Serial.print(F("Logic level at PN5180' IRQ-PIN: ")); Serial.println(digitalRead(RFID_IRQ));
// turn on LPCD
uint16_t wakeupCounterInMs = 0x3FF; // needs to be in the range of 0x0 - 0xA82. max wake-up time is 2960 ms.
if (nfc14443.switchToLPCD(wakeupCounterInMs)) {
loggerNl((char *) FPSTR(lowPowerCardSuccess), LOGLEVEL_INFO);
// configure wakeup pin for deep-sleep wake-up, use ext1
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
// freeze pin states in deep sleep
gpio_hold_en(gpio_num_t(RFID_CS)); // CS/NSS
gpio_hold_en(gpio_num_t(RFID_RST)); // RST
gpio_deep_sleep_hold_en();
loggerNl((char *) FPSTR(wakeUpRfidNoIso14443), LOGLEVEL_ERROR);
esp_deep_sleep_start();
} else {
Serial.println(F("switchToLPCD failed"));
}
}
}
#endif
void setup() {
Serial.begin(115200);
esp_sleep_enable_ext0_wakeup((gpio_num_t) DREHENCODER_BUTTON, 0);
#ifdef PN5180_ENABLE_LPCD
// disable pin hold from deep sleep (LPCD)
gpio_deep_sleep_hold_dis();
gpio_hold_dis(gpio_num_t(RFID_CS)); // NSS
gpio_hold_dis(gpio_num_t(RFID_RST)); // RST
pinMode(RFID_IRQ, INPUT);
// check wakeup reason is a card detection
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) {
checkCardIsPresentLPCD();
}
#endif
srand(esp_random());
pinMode(POWER, OUTPUT);
digitalWrite(POWER, HIGH);
@ -4179,8 +4275,15 @@ void setup() {
Serial.println(F(" |_| |___|_|_|_____|_____|_|___|_____| "));
Serial.println(F(" ESP32-version"));
Serial.println(F(""));
// show SD card type
// print wake-up reason
printWakeUpReason();
#ifdef PN5180_ENABLE_LPCD
// disable pin hold from deep sleep
gpio_deep_sleep_hold_dis();
gpio_hold_dis(gpio_num_t(RFID_CS)); // NSS
gpio_hold_dis(gpio_num_t(RFID_RST)); // RST
#endif
// show SD card type
#ifdef SD_MMC_1BIT_MODE
loggerNl((char *) FPSTR(sdMountedMmc1BitMode), LOGLEVEL_NOTICE);
uint8_t cardType = SD_MMC.cardType();

2
src/settings-lolin32.h

@ -42,6 +42,8 @@
#ifdef RFID_READER_TYPE_PN5180
#define RFID_BUSY 16 // PN5180 BUSY PIN
#define RFID_RST 22 // PN5180 RESET PIN
#define RFID_IRQ 39 // PN5180 IRQ PIN (only needed for low power card detection)
#define BUTTON_PIN_BITMASK 0x8000000000// 2^RFID_IRQ in hex
#endif
// I2S (DAC)
#define I2S_DOUT 25 // Digital out (I2S)

2
src/settings-lolin_d32.h

@ -42,6 +42,8 @@
#ifdef RFID_READER_TYPE_PN5180
#define RFID_BUSY 16 // PN5180 BUSY PIN
#define RFID_RST 22 // PN5180 RESET PIN
#define RFID_IRQ 39 // PN5180 IRQ PIN (only needed for low power card detection)
#define BUTTON_PIN_BITMASK 0x8000000000// 2^RFID_IRQ in hex
#endif
// I2S (DAC)
#define I2S_DOUT 25 // Digital out (I2S)

2
src/settings-lolin_d32_pro.h

@ -38,6 +38,8 @@
#ifdef RFID_READER_TYPE_PN5180
#define RFID_BUSY 33 // PN5180 BUSY PIN
#define RFID_RST 22 // PN5180 RESET PIN
#define RFID_IRQ 39 // PN5180 IRQ PIN (only needed for low power card detection)
#define BUTTON_PIN_BITMASK 0x8000000000// 2^RFID_IRQ in hex
#endif
// I2S (DAC)
#define I2S_DOUT 25 // Digital out (I2S)

3
src/settings.h

@ -39,7 +39,8 @@
//################## select RFID reader ##############################
#define RFID_READER_TYPE_MFRC522_SPI // use MFRC522 via SPI
//#define RFID_READER_TYPE_MFRC522_I2C // use MFRC522 via I2C
//#define RFID_READER_TYPE_PN5180
//#define RFID_READER_TYPE_PN5180 // use PN5180
//#define PN5180_ENABLE_LPCD // enable PN5180 low power card detection: wake up on card detection
//#################### Various settings ##############################

Loading…
Cancel
Save