led works too

This commit is contained in:
Wolfgang Hottgenroth 2018-04-28 16:46:09 +02:00
parent 96710787d3
commit 1c4dc6ad71
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4
5 changed files with 152 additions and 40 deletions

View File

@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DT
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.DTS/value=3600
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524929038
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.LOCAL/value=1524933614
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524921838
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.UTC/value=1524926414
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/A.EXTRA.TIME.ZONE/value=3600
@ -466,10 +466,10 @@ environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.BOARD_NA
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module)
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/value=/dev/ttyUSB4
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.COM_PORT/value=/dev/ttyUSB5
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}//////////////////////////////////////////////////////////////////////////////////////////////////////
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}///////////////////////////////////////////////////////////////////////////////////////////////////////////
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/operation=replace
environment/project/io.sloeber.core.toolChain.release.1856809793/JANTJE.EXTRA.ALL/value=

View File

@ -16,7 +16,8 @@ configItems = [
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"MySwitch1"},
{"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
{"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/MySwitch1/Value"},
{"label":"MQTT Switch Topic", "key":"mqttSwitchTopic", "type":"C", "length":64, "default":"IoT/MySwitch1/Value"},
{"label":"MQTT LED Topic", "key":"mqttLedTopic", "type":"C", "length":64, "default":"IoT/MySwitch1/Led"},
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/MySwitch1/Debug"},
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
{"label":"Debounce (us)", "key":"debounce", "type":"I", "default":300},
@ -25,7 +26,7 @@ configItems = [
]
magic = 0xC0DE0004
magic = 0xC0DE0005
appName = "ESP8266 based MySwitch"
confWifiSsid = "espconfig"

View File

@ -36,7 +36,8 @@ void configServeIndex() {
strcpy(configBlock.mqttPass, "geheim123");
strcpy(configBlock.mqttClientId, "MySwitch1");
configBlock.mqttPort = 8883;
strcpy(configBlock.mqttTopic, "IoT/MySwitch1/Value");
strcpy(configBlock.mqttSwitchTopic, "IoT/MySwitch1/Value");
strcpy(configBlock.mqttLedTopic, "IoT/MySwitch1/Led");
strcpy(configBlock.mqttDebugTopic, "IoT/MySwitch1/Debug");
configBlock.debugMode = 0;
configBlock.debounce = 300;
@ -202,13 +203,28 @@ void configServeIndex() {
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttTopic\">MQTT Topic</label>"
" <label for\"mqttSwitchTopic\">MQTT Switch Topic</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttTopic\" id=\"mqttTopic\" ";
" <input type=\"text\" name=\"mqttSwitchTopic\" id=\"mqttSwitchTopic\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttTopic;
buffer += configBlock.mqttSwitchTopic;
buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttLedTopic\">MQTT LED Topic</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttLedTopic\" id=\"mqttLedTopic\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttLedTopic;
buffer += "\"";
buffer +=
@ -329,8 +345,10 @@ void configServeGetConfiguration() {
strcpy(configBlock.mqttClientId, arg.c_str());
arg = webServer.arg("mqttPort");
configBlock.mqttPort = atoi(arg.c_str());
arg = webServer.arg("mqttTopic");
strcpy(configBlock.mqttTopic, arg.c_str());
arg = webServer.arg("mqttSwitchTopic");
strcpy(configBlock.mqttSwitchTopic, arg.c_str());
arg = webServer.arg("mqttLedTopic");
strcpy(configBlock.mqttLedTopic, arg.c_str());
arg = webServer.arg("mqttDebugTopic");
strcpy(configBlock.mqttDebugTopic, arg.c_str());
arg = webServer.arg("debugMode");
@ -401,8 +419,12 @@ void showConfiguration() {
Serial.print(configBlock.mqttPort);
Serial.println(">");
Serial.print("mqttTopic = <");
Serial.print(configBlock.mqttTopic);
Serial.print("mqttSwitchTopic = <");
Serial.print(configBlock.mqttSwitchTopic);
Serial.println(">");
Serial.print("mqttLedTopic = <");
Serial.print(configBlock.mqttLedTopic);
Serial.println(">");
Serial.print("mqttDebugTopic = <");

View File

@ -9,7 +9,8 @@ typedef struct {
char mqttPass[32];
char mqttClientId[32];
uint32_t mqttPort;
char mqttTopic[64];
char mqttSwitchTopic[64];
char mqttLedTopic[64];
char mqttDebugTopic[64];
uint32_t debugMode;
uint32_t debounce;

View File

@ -28,6 +28,82 @@ WiFiClientSecure espClient;
PubSubClient client(espClient);
typedef struct {
uint8_t index;
uint8_t ledPin;
} tLed;
enum tPressedState { psHIGH, psLOW, psACCEPTED_LOW, psLONG_START, psLONG_CONT, psLONG_CONT_SEND, psLONG_END, psSHORT, psINVALID };
typedef struct {
uint8_t index;
uint8_t buttonPin;
tPressedState pressedState;
tPressedState oldPressedState;
uint32_t lastStateChange;
} tButton;
tLed leds[] = {
{ 1, LED_1 },
{ 2, LED_2 },
{ 3, LED_3 },
{ 0, 0 }
};
tButton buttons[] = {
{ 1, SWITCH_1, psHIGH, psINVALID, 0 },
{ 2, SWITCH_2, psHIGH, psINVALID, 0 },
{ 3, SWITCH_3, psHIGH, psINVALID, 0 },
{ 0, 0, psINVALID, psINVALID, 0 } // END MARKER
};
void callback(char* topic, byte* payload, unsigned int length) {
const uint8_t BUFSIZE = 128;
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
#ifdef DEBUG
Serial.println("Received message too long, ignore it");
#endif
} else {
char buffer[BUFSIZE];
memcpy(buffer, payload, length);
*(buffer + length) = 0;
#ifdef DEBUG
Serial.print("Received message: ");
Serial.print(length);
Serial.print(" ");
Serial.print(topic);
Serial.print(" ");
Serial.println(buffer);
#endif
uint8_t i = 0;
while (true) {
tLed *led = &(leds[i]);
i++;
if (led->index != 0) {
char buf[BUF_SIZE];
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttLedTopic, led->index);
if (! strcmp(buf, topic)) {
if (! strcmp(buffer, "ON")) {
digitalWrite(led->ledPin, HIGH);
} else {
digitalWrite(led->ledPin, LOW);
}
}
} else {
break;
}
}
}
}
void setup_wifi() {
delay(10);
@ -75,11 +151,25 @@ void reconnect() {
#ifdef DEBUG
Serial.println("connected");
#endif
// client.setCallback(callback);
client.setCallback(callback);
// Once connected, publish an announcement...
client.publish(configBlock.mqttDebugTopic, "hello world");
client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str());
uint8_t i = 0;
while (true) {
tLed *led = &(leds[i]);
i++;
if (led->index != 0) {
char buf[BUF_SIZE];
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttLedTopic, led->index);
client.subscribe(buf);
} else {
break;
}
}
} else {
#ifdef DEBUG
Serial.print("failed, rc=");
@ -105,12 +195,27 @@ bool mqtt_connect() {
void setupProduction() {
pinMode(SWITCH_1, INPUT_PULLUP);
pinMode(LED_1, OUTPUT);
pinMode(SWITCH_2, INPUT_PULLUP);
pinMode(LED_2, OUTPUT);
pinMode(SWITCH_3, INPUT_PULLUP);
pinMode(LED_3, OUTPUT);
uint8_t i = 0;
while (true) {
tButton *button = &(buttons[i]);
i++;
if (button->index != 0) {
pinMode(button->buttonPin, INPUT_PULLUP);
} else {
break;
}
}
i = 0;
while (true) {
tLed *led = &(leds[i]);
i++;
if (led->index != 0) {
pinMode(led->ledPin, OUTPUT);
} else {
break;
}
}
setup_wifi();
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
@ -119,22 +224,12 @@ void setupProduction() {
static void sendMsg(uint8_t idx, char *msg) {
char buf[BUF_SIZE];
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttTopic, idx);
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttSwitchTopic, idx);
client.publish(buf, msg);
}
enum tPressedState { psHIGH, psLOW, psACCEPTED_LOW, psLONG_START, psLONG_CONT, psLONG_CONT_SEND, psLONG_END, psSHORT, psINVALID };
typedef struct {
uint8_t index;
uint8_t buttonPin;
tPressedState pressedState;
tPressedState oldPressedState;
uint32_t lastStateChange;
} tButton;
static void buttonHandler(tButton *button) {
uint32_t currentMicros = micros();
uint8_t buttonState = digitalRead(button->buttonPin);
@ -217,13 +312,6 @@ static void buttonHandler(tButton *button) {
}
tButton buttons[] = {
{ 1, SWITCH_1, psHIGH, psINVALID, 0 },
{ 2, SWITCH_2, psHIGH, psINVALID, 0 },
{ 3, SWITCH_3, psHIGH, psINVALID, 0 },
{ 0, 0, psINVALID, psINVALID, 0 } // END MARKER
};
void loopProduction() {
bool reconnected = mqtt_connect();
static uint32_t reconnectTime = 0;