Skip to content

Commit

Permalink
Modify debounce logic. Remove sleep support (I don't know how to modi…
Browse files Browse the repository at this point in the history
…fy it to work with the new logic).
  • Loading branch information
anyuta1166 committed Apr 19, 2020
1 parent 07131bb commit db22838
Show file tree
Hide file tree
Showing 4 changed files with 1,069 additions and 1,107 deletions.
397 changes: 196 additions & 201 deletions watermeter/core.ino
Original file line number Diff line number Diff line change
@@ -1,201 +1,196 @@
String makeMacAddress() {

String mac = "";

for (int i = 0; i < macAddress.length(); i++) {
if (macAddress[i] != ':') mac += macAddress[i];
}

return mac;
}

/* For read and write to EEPROM */
unsigned long crc_update(unsigned long crc, byte data) {
byte tbl_idx;
tbl_idx = crc ^ (data >> (0 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
tbl_idx = crc ^ (data >> (1 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
return crc;
}


unsigned long crc_byte(byte *b, int len) {
unsigned long crc = ~0L;
int i;

for (i = 0 ; i < len ; i++) {
crc = crc_update(crc, *b++);
}
crc = ~crc;
return crc;
}

/* Vcc or Battery in volt */
String returnVccStr() {
String v = "";
String Vcc;
int volt;
int voltInt;

if (!EXT_POWER_CONTROL) {
Vcc = "Vcc: ";
voltInt = ESP.getVcc();
volt = (voltInt*117+5000)/100;
} else {
Vcc = "Battery: ";
voltInt = analogRead(BAT_VOLT_PIN);
volt = 5000/1024*voltInt*1.19;
}
/* Serial.printf("voltInt: %d\n", voltInt);*/

v += volt;

Vcc += v.substring(0, 1);
Vcc += ',';
Vcc += v.substring(1, 3);
Vcc += 'V';

return Vcc;

}

/* Received signal strength indicator in dBm */
String returnRssiStr() {
String rssi = "WiFi: ";
rssi += WiFi.RSSI();
rssi += " dBm";
return rssi;
}

/* Init PIN */
void initPin() {
pinMode(HOT_PIN, INPUT_PULLUP);
pinMode(COLD_PIN, INPUT_PULLUP);
if (EXT_POWER_CONTROL) {
pinMode(EXT_POWER_PIN, INPUT_PULLDOWN_16);
}

}

void startApMsg() {
Serial.printf("WiFi network Name: %s, Password: %s\n", wmConfig.apSsid, wmConfig.apPassword);
Serial.print("Go to: "); Serial.print(WiFi.softAPIP()); Serial.println(" please");
}

/* Init external interrupt */
void initInterrupt() {

attachInterrupt(digitalPinToInterrupt(HOT_PIN), hotInterrupt, RISING);
attachInterrupt(digitalPinToInterrupt(COLD_PIN), coldInterrupt, RISING);

hotInt = coldInt = 0;
}

/* External interrupt for hot water */
ICACHE_RAM_ATTR void hotInterrupt() {
/* First interrupt if hotInt == 0 */
if (hotInt == 0) {
hotInt++;
hotTimeBounce = millis();
} else os_timer_disarm(&hotTimer);
os_timer_arm(&hotTimer, TIME_BOUNCE, true);
}

/* External interrupt for cold water */
ICACHE_RAM_ATTR void coldInterrupt() {
/* First interrupt if coldInt == 0 */
if (coldInt == 0) {
coldInt++;
coldTimeBounce = millis();
} else os_timer_disarm(&coldTimer);
os_timer_arm(&coldTimer, TIME_BOUNCE, true);
}

void hotTimerCallback(void *pArg) {
/* If a long low level, then retiming hotTimeBounce */
if (!digitalRead(HOT_PIN)) {
hotTimeBounce = millis();
return;
}

if (digitalRead(HOT_PIN) && millis() - hotTimeBounce < TIME_BOUNCE) return;

os_timer_disarm(&hotTimer);

hotInt = 0;

counterHotWater++;
}

void coldTimerCallback(void *pArg) {

if (!digitalRead(COLD_PIN)) {
coldTimeBounce = millis();
return;
}

if (digitalRead(COLD_PIN) && millis() - coldTimeBounce < TIME_BOUNCE) return;

os_timer_disarm(&coldTimer);

coldInt = 0;

counterColdWater++;
}

bool checkExtPower() {

if (!EXT_POWER_CONTROL) return true;

int val = digitalRead(EXT_POWER_PIN);

if (val) {
powerLow = false;
sleepDelay = 0;
if (sleepNow) {
if (DEBUG) Serial.println("External power high.");
sleepNow = false;
}
return true;
}
else {
powerLow = true;
delay(1);
if (!sleepNow) {
if (sleepDelay > SLEEP_DELAY) {
if (DEBUG) Serial.println("External power low.");
sleepNow = true;
sleepDelay = 0;
return false;
} else {
sleepDelay++;
}
}
return false;
}
}

void sleepOnNow() {
if (DEBUG) Serial.println("Light sleep now ...");
apModeNow=staModeNow=false;
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); //light sleep mode
gpio_pin_wakeup_enable(GPIO_ID_PIN(HOT_PIN), GPIO_PIN_INTR_LOLEVEL); /* Set the interrupt to look for LOW pulses on HOT_PIN */
gpio_pin_wakeup_enable(GPIO_ID_PIN(COLD_PIN), GPIO_PIN_INTR_LOLEVEL); /* Set the interrupt to look for LOW pulses on COLD_PIN */
wifi_fpm_open();
delay(100);
wifi_fpm_set_wakeup_cb(wakeupFromMotion); //wakeup callback
wifi_fpm_do_sleep(0xFFFFFFF);
delay(100);
}

void wakeupFromMotion(void) {
ESP.wdtFeed();
initInterrupt();
wifi_fpm_close();
if (DEBUG) Serial.println("Wake up from sleep.");
sleepNow = false;
}

String makeMacAddress() {

String mac = "";

for (int i = 0; i < macAddress.length(); i++) {
if (macAddress[i] != ':') mac += macAddress[i];
}

return mac;
}

/* For read and write to EEPROM */
unsigned long crc_update(unsigned long crc, byte data) {
byte tbl_idx;
tbl_idx = crc ^ (data >> (0 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
tbl_idx = crc ^ (data >> (1 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
return crc;
}


unsigned long crc_byte(byte *b, int len) {
unsigned long crc = ~0L;
int i;

for (i = 0 ; i < len ; i++) {
crc = crc_update(crc, *b++);
}
crc = ~crc;
return crc;
}

/* Vcc or Battery in volt */
String returnVccStr() {
String v = "";
String Vcc;
int volt;
int voltInt;

Vcc = "Vcc: ";
voltInt = ESP.getVcc();
volt = (voltInt*117+5000)/100;
/* Serial.printf("voltInt: %d\n", voltInt);*/

v += volt;

Vcc += v.substring(0, 1);
Vcc += ',';
Vcc += v.substring(1, 3);
Vcc += 'V';

return Vcc;

}

/* Received signal strength indicator in dBm */
String returnRssiStr() {
String rssi = "WiFi: ";
rssi += WiFi.RSSI();
rssi += " dBm";
return rssi;
}

String returnColdCircuitStr() {
String c = "Cold circuit: ";
if (coldState == LOW) c += "closed";
else c+= "open";
return c;
}

String returnHotCircuitStr() {
String c = "Hot circuit: ";
if (hotState == LOW) c += "closed";
else c+= "open";
return c;
}

/* Init PIN */
void initPin() {
pinMode(HOT_PIN, INPUT_PULLUP);
pinMode(COLD_PIN, INPUT_PULLUP);

}

void startApMsg() {
Serial.printf("WiFi network Name: %s, Password: %s\n", wmConfig.apSsid, wmConfig.apPassword);
Serial.print("Go to: "); Serial.print(WiFi.softAPIP()); Serial.println(" please");
}

/* Init external interrupt */
void initInterrupt() {

attachInterrupt(digitalPinToInterrupt(HOT_PIN), hotInterrupt, CHANGE);
attachInterrupt(digitalPinToInterrupt(COLD_PIN), coldInterrupt, CHANGE);

hotInt = coldInt = 0;
hotState = digitalRead(HOT_PIN);
coldState = digitalRead(COLD_PIN);
}

/* External interrupt for hot water */
ICACHE_RAM_ATTR void hotInterrupt() {
if (hotState == LOW) {
if (digitalRead(HOT_PIN)) {
/* First interrupt if hotInt == 0 */
if (hotInt == 0) {
hotInt++;
hotTimeBounce = millis();
} else os_timer_disarm(&hotTimer);
os_timer_arm(&hotTimer, TIME_BOUNCE, true);
}
} else {
if (!digitalRead(HOT_PIN)) {
/* First interrupt if hotInt == 0 */
if (hotInt == 0) {
hotInt++;
hotTimeBounce = millis();
} else os_timer_disarm(&hotTimer);
os_timer_arm(&hotTimer, TIME_BOUNCE, true);
}
}
}

/* External interrupt for cold water */
ICACHE_RAM_ATTR void coldInterrupt() {
if (coldState == LOW) {
if (digitalRead(COLD_PIN)) {
/* First interrupt if coldInt == 0 */
if (coldInt == 0) {
coldInt++;
coldTimeBounce = millis();
} else os_timer_disarm(&coldTimer);
os_timer_arm(&coldTimer, TIME_BOUNCE, true);
}
} else {
if (!digitalRead(COLD_PIN)) {
/* First interrupt if coldInt == 0 */
if (coldInt == 0) {
coldInt++;
coldTimeBounce = millis();
} else os_timer_disarm(&coldTimer);
os_timer_arm(&coldTimer, TIME_BOUNCE, true);
}
}
}

void hotTimerCallback(void *pArg) {
if (hotState == LOW) {
if (!digitalRead(HOT_PIN)) {
os_timer_disarm(&hotTimer);
hotInt = 0;
return;
}
if (digitalRead(HOT_PIN) && millis() - hotTimeBounce < TIME_BOUNCE) return;
os_timer_disarm(&hotTimer);
hotInt = 0;
counterHotWater++;
hotState = HIGH;
} else {
if (digitalRead(HOT_PIN)) {
os_timer_disarm(&hotTimer);
hotInt = 0;
return;
}
if (!digitalRead(HOT_PIN) && millis() - hotTimeBounce < TIME_BOUNCE) return;
os_timer_disarm(&hotTimer);
hotInt = 0;
hotState = LOW;
}
}

void coldTimerCallback(void *pArg) {
if (coldState == LOW) {
if (!digitalRead(COLD_PIN)) {
os_timer_disarm(&coldTimer);
coldInt = 0;
return;
}
if (digitalRead(COLD_PIN) && millis() - coldTimeBounce < TIME_BOUNCE) return;
os_timer_disarm(&coldTimer);
coldInt = 0;
counterColdWater++;
coldState = HIGH;
} else {
if (digitalRead(COLD_PIN)) {
os_timer_disarm(&coldTimer);
coldInt = 0;
return;
}
if (!digitalRead(COLD_PIN) && millis() - coldTimeBounce < TIME_BOUNCE) return;
os_timer_disarm(&coldTimer);
coldInt = 0;
coldState = LOW;
}
}
Loading

0 comments on commit db22838

Please sign in to comment.