MySwitch/productionMode.cpp

343 lines
7.0 KiB
C++

/*
* productionMode.cpp
*
* Created on: Jan 24, 2018
* Author: wn
*/
#include "defines.h"
#define MQTT_MAX_PACKET_SIZE 256
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include "configuration.h"
#define BUF_SIZE 64
volatile uint32_t cnt = 0;
uint32_t uptime = 0;
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);
WiFi.mode(WIFI_STA);
// We start by connecting to a WiFi network
#ifdef DEBUG
Serial.println();
Serial.print("Connecting to ");
Serial.println(configBlock.wifiSsid);
#endif
WiFi.begin(configBlock.wifiSsid, configBlock.wifiKey);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
#ifdef DEBUG
Serial.print(".");
#endif
}
Serial.println("!");
#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
#ifdef DEBUG
Serial.print("Attempting MQTT connection...");
#endif
// Attempt to connect
//char clientId[128];
//snprintf(clientId, 127, "esp%s", WiFi.macAddress().c_str());
if (client.connect(configBlock.mqttClientId, configBlock.mqttUser, configBlock.mqttPass)) {
#ifdef DEBUG
Serial.println("connected");
#endif
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=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
#endif
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool mqtt_connect() {
bool reconnected = false;
if (!client.connected()) {
reconnect();
reconnected = true;
}
client.loop();
return reconnected;
}
void setupProduction() {
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);
}
static void sendMsg(uint8_t idx, char *msg) {
char buf[BUF_SIZE];
snprintf(buf, BUF_SIZE-1, "%s/%d", configBlock.mqttSwitchTopic, idx);
client.publish(buf, msg);
}
static void buttonHandler(tButton *button) {
uint32_t currentMicros = micros();
uint8_t buttonState = digitalRead(button->buttonPin);
#ifdef DEBUG
if (button->oldPressedState != button->pressedState) {
Serial.print("Index ");
Serial.print(button->index);
Serial.print(", state changed from ");
Serial.print(button->oldPressedState);
Serial.print(" to ");
Serial.print(button->pressedState);
Serial.println();
button->oldPressedState = button->pressedState;
}
#endif
switch (button->pressedState) {
case psHIGH:
if (buttonState == LOW) {
button->pressedState = psLOW;
button->lastStateChange = currentMicros;
}
break;
case psLOW:
if (buttonState == HIGH) {
button->pressedState = psHIGH;
button->lastStateChange = currentMicros;
} else {
if (currentMicros > (button->lastStateChange + configBlock.debounce)) {
button->pressedState = psACCEPTED_LOW;
button->lastStateChange = currentMicros;
}
}
break;
case psACCEPTED_LOW:
if (buttonState == HIGH) {
button->pressedState = psSHORT;
button->lastStateChange = currentMicros;
}
if (currentMicros > (button->lastStateChange + (configBlock.longPress * 1000))) {
button->pressedState = psLONG_START;
button->lastStateChange = currentMicros;
}
break;
case psSHORT:
sendMsg(button->index, "PRESS_SHORT");
button->pressedState = psHIGH;
button->lastStateChange = currentMicros;
break;
case psLONG_START:
sendMsg(button->index, "PRESS_LONG_START");
button->pressedState = psLONG_CONT;
button->lastStateChange = currentMicros;
break;
case psLONG_CONT:
if (buttonState == HIGH) {
button->pressedState = psLONG_END;
button->lastStateChange = currentMicros;
}
if (currentMicros > (button->lastStateChange + (configBlock.longPressRepeat * 1000))) {
button->pressedState = psLONG_CONT_SEND;
button->lastStateChange = currentMicros;
}
break;
case psLONG_CONT_SEND:
sendMsg(button->index, "PRESS_LONG_CONT");
button->pressedState = psLONG_CONT;
button->lastStateChange = currentMicros;
break;
case psLONG_END:
sendMsg(button->index, "PRESS_LONG_END");
button->pressedState = psHIGH;
button->lastStateChange = currentMicros;
break;
default:
button->pressedState = psHIGH;
button->lastStateChange = currentMicros;
}
}
void loopProduction() {
bool reconnected = mqtt_connect();
static uint32_t reconnectTime = 0;
if (reconnected) {
reconnectTime = millis();
}
uint32_t currentMillis = millis();
static uint32_t lastUptimeMillis = 0;
if (currentMillis > (lastUptimeMillis + 1000)) {
lastUptimeMillis = currentMillis;
uptime++;
}
static uint8_t i = 0;
tButton *button = &(buttons[i]);
i++;
if (button->index != 0) {
buttonHandler(button);
} else {
i = 0;
}
}