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.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/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/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/operation=replace
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.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/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/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/operation=replace
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":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/TouchSwitch/Debug"},
{"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;
strcpy(configBlock.mqttDebugTopic, "IoT/TouchSwitch/Debug");
configBlock.period = 500;
configBlock.threshold = 350;
configBlock.calibrationTimeout = 20000;
configBlock.threshold = 10;
}
String buffer =
@ -207,6 +208,20 @@ void configServeIndex() {
buffer += configBlock.period;
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 +=
" />"
" </td>"
@ -268,6 +283,8 @@ void configServeGetConfiguration() {
strcpy(configBlock.mqttDebugTopic, arg.c_str());
arg = webServer.arg("period");
configBlock.period = atoi(arg.c_str());
arg = webServer.arg("calibrationTimeout");
configBlock.calibrationTimeout = atoi(arg.c_str());
arg = webServer.arg("threshold");
configBlock.threshold = atoi(arg.c_str());
@ -326,6 +343,9 @@ void showConfiguration() {
Serial.print("period = ");
Serial.println(configBlock.period);
Serial.print("calibrationTimeout = ");
Serial.println(configBlock.calibrationTimeout);
Serial.print("threshold = ");
Serial.println(configBlock.threshold);

View File

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

View File

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

View File

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