other sensing mechanism, schematic still needs to be adjusted, works so far

This commit is contained in:
Wolfgang Hottgenroth
2018-01-30 13:26:26 +01:00
parent 154ca8e91e
commit 1147656d47
6 changed files with 73 additions and 40 deletions

View File

@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.DTS
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.DTS/value=0 environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.DTS/value=0
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/value=1516881551 environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.LOCAL/value=1517307730
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/value=1516877951 environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.UTC/value=1517304130
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/value=3600 environment/project/io.sloeber.core.toolChain.release.547546691/A.EXTRA.TIME.ZONE/value=3600
@ -466,10 +466,10 @@ environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.BOARD_NAM
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module) environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module)
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/value=/dev/ttyUSB1 environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.COM_PORT/value=/dev/ttyUSB0
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}//////////////////////////////// environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}/////////////////////////////////////
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/operation=replace environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/operation=replace
environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/value= environment/project/io.sloeber.core.toolChain.release.547546691/JANTJE.EXTRA.ALL/value=

View File

@ -18,7 +18,8 @@ configItems = [
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}, {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Debug"}, {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Debug"},
{"label":"Period", "key":"period", "type":"I", "default":500}, {"label":"Period", "key":"period", "type":"I", "default":500},
{"label":"Threshold", "key":"threshold", "type":"I", "default":350} {"label":"CalibrationTimeout", "key":"calibrationTimeout", "type":"I", "default":20000},
{"label":"Threshold", "key":"threshold", "type":"I", "default":10}
] ]

View File

@ -29,7 +29,8 @@ void configServeIndex() {
configBlock.debugMode = 0; configBlock.debugMode = 0;
strcpy(configBlock.mqttDebugTopic, "IoT/TouchSwitch/Debug"); strcpy(configBlock.mqttDebugTopic, "IoT/TouchSwitch/Debug");
configBlock.period = 500; configBlock.period = 500;
configBlock.threshold = 350; configBlock.calibrationTimeout = 20000;
configBlock.threshold = 10;
} }
String buffer = String buffer =
@ -207,6 +208,20 @@ void configServeIndex() {
buffer += configBlock.period; buffer += configBlock.period;
buffer += "\""; buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"calibrationTimeout\">CalibrationTimeout</label>"
" </td><td>"
" <input type=\"text\" name=\"calibrationTimeout\" id=\"calibrationTimeout\" ";
buffer += " value=\"";
buffer += configBlock.calibrationTimeout;
buffer += "\"";
buffer += buffer +=
" />" " />"
" </td>" " </td>"
@ -268,6 +283,8 @@ void configServeGetConfiguration() {
strcpy(configBlock.mqttDebugTopic, arg.c_str()); strcpy(configBlock.mqttDebugTopic, arg.c_str());
arg = webServer.arg("period"); arg = webServer.arg("period");
configBlock.period = atoi(arg.c_str()); configBlock.period = atoi(arg.c_str());
arg = webServer.arg("calibrationTimeout");
configBlock.calibrationTimeout = atoi(arg.c_str());
arg = webServer.arg("threshold"); arg = webServer.arg("threshold");
configBlock.threshold = atoi(arg.c_str()); configBlock.threshold = atoi(arg.c_str());
@ -326,6 +343,9 @@ void showConfiguration() {
Serial.print("period = "); Serial.print("period = ");
Serial.println(configBlock.period); Serial.println(configBlock.period);
Serial.print("calibrationTimeout = ");
Serial.println(configBlock.calibrationTimeout);
Serial.print("threshold = "); Serial.print("threshold = ");
Serial.println(configBlock.threshold); Serial.println(configBlock.threshold);

View File

@ -11,6 +11,7 @@ typedef struct {
uint32_t debugMode; uint32_t debugMode;
char mqttDebugTopic[64]; char mqttDebugTopic[64];
uint32_t period; uint32_t period;
uint32_t calibrationTimeout;
uint32_t threshold; uint32_t threshold;
} tConfigBlock; } tConfigBlock;

View File

@ -16,8 +16,8 @@
#define CONFIG_SWITCH 4 #define CONFIG_SWITCH 4
#define LED_PIN 14 #define LED_PIN 14
#define TOUCH_PIN_HIGH 16 #define TOUCH_PIN_RECEIVE 16
#define TOUCH_PIN_LOW 5 #define TOUCH_PIN_SEND 5

View File

@ -18,10 +18,11 @@
#include "configuration.h" #include "configuration.h"
typedef enum { NOT_PRESSED, PRESSED, LONG_PRESSED_PREPARE, LONG_PRESSED } tPressedState; typedef enum { NOT_PRESSED, PRESSED, LONG_PRESSED_PREPARE, LONG_PRESSED } tPressedState;
WiFiClientSecure espClient; WiFiClientSecure espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
@ -98,61 +99,66 @@ void mqtt_connect() {
void setupProduction() { void setupProduction() {
setup_wifi(); setup_wifi();
client.setServer(configBlock.mqttBroker, configBlock.mqttPort); client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
pinMode(A0, INPUT);
} }
void loopProduction() { void loopProduction() {
static tPressedState state = NOT_PRESSED;
static uint32_t lastMillis = 0;
mqtt_connect(); mqtt_connect();
// discharge
digitalWrite(TOUCH_PIN_SEND, LOW);
pinMode(TOUCH_PIN_SEND, OUTPUT);
digitalWrite(TOUCH_PIN_RECEIVE, LOW);
pinMode(TOUCH_PIN_RECEIVE, OUTPUT);
// http://www.elektronik-labor.de/AVR/Touch.html // sense
// Phase 1 pinMode(TOUCH_PIN_RECEIVE, INPUT);
pinMode(TOUCH_PIN_HIGH, INPUT); digitalWrite(TOUCH_PIN_SEND, HIGH);
pinMode(TOUCH_PIN_LOW, INPUT);
// Phase 2 const uint32_t MAX_CHARGE_CNT = 1000;
digitalWrite(TOUCH_PIN_HIGH, HIGH); static uint32_t senseCnt = 0;
pinMode(TOUCH_PIN_HIGH, OUTPUT); static uint32_t chargeCntSum = 0;
// Phase 3 uint32_t chargeCnt = 0;
pinMode(TOUCH_PIN_HIGH, INPUT);
// Phase 4 noInterrupts();
digitalWrite(TOUCH_PIN_LOW, LOW); while ((! digitalRead(TOUCH_PIN_RECEIVE)) && (chargeCnt < MAX_CHARGE_CNT)) {
pinMode(TOUCH_PIN_LOW, OUTPUT); chargeCnt++;
}
interrupts();
chargeCntSum += chargeCnt;
senseCnt++;
static uint32_t lastMillis = 0;
uint32_t currentMillis = millis(); uint32_t currentMillis = millis();
static uint32_t lastCalibration = 0;
if (currentMillis - lastMillis > configBlock.period) { if (currentMillis - lastMillis > configBlock.period) {
lastMillis = currentMillis; lastMillis = currentMillis;
int v = analogRead(A0); uint32_t chargeCntAvg = chargeCntSum / senseCnt;
static uint32_t baseline = UINT32_MAX;
if (chargeCntAvg < baseline) {
baseline = chargeCntAvg;
}
uint32_t capNum = chargeCntAvg - baseline;
if (configBlock.debugMode) { if (configBlock.debugMode) {
char payload[128]; char payload[128];
snprintf(payload, 127, "%ld", v); snprintf(payload, 127, "%ld %ld %ld %ld", senseCnt, baseline, chargeCntAvg, capNum);
client.publish(configBlock.mqttDebugTopic, payload); client.publish(configBlock.mqttDebugTopic, payload);
#ifdef DEBUG
Serial.println(v);
#endif
} }
if (v > configBlock.threshold) { static tPressedState state = NOT_PRESSED;
if (capNum > configBlock.threshold) {
#ifdef DEBUG #ifdef DEBUG
Serial.println(v); Serial.println(capNum);
#endif #endif
// discharge
digitalWrite(TOUCH_PIN_HIGH, LOW);
pinMode(TOUCH_PIN_HIGH, OUTPUT);
digitalWrite(TOUCH_PIN_LOW, LOW);
pinMode(TOUCH_PIN_LOW, OUTPUT);
switch (state) { switch (state) {
case NOT_PRESSED: case NOT_PRESSED:
state = PRESSED; state = PRESSED;
@ -170,6 +176,11 @@ void loopProduction() {
break; break;
} }
} else { } else {
if (currentMillis - lastCalibration > configBlock.calibrationTimeout) {
baseline = UINT32_MAX;
lastCalibration = currentMillis;
}
switch (state) { switch (state) {
case NOT_PRESSED: case NOT_PRESSED:
break; break;
@ -184,9 +195,9 @@ void loopProduction() {
state = NOT_PRESSED; state = NOT_PRESSED;
} }
chargeCntSum = 0;
senseCnt = 0;
} }
} }