diff --git a/prog.txt b/prog.txt new file mode 100644 index 0000000..3a36189 --- /dev/null +++ b/prog.txt @@ -0,0 +1,4 @@ +def replace_binary_string(input_string): + result_string = input_string.replace("11111111111111111111111111111111111111111111111", "1") + result_string = result_string.replace("00000000000000000000000000000000000000000000000", "0") + return result_string \ No newline at end of file diff --git a/src/app/routes.cpp b/src/app/routes.cpp index 19ed6ef..73a2c5c 100644 --- a/src/app/routes.cpp +++ b/src/app/routes.cpp @@ -159,6 +159,9 @@ uint8_t *data, size_t len, size_t index, size_t total) { bool fileLoaded = writeJsonVariantToFile(MQTT_SETTINGS_PATH, root); bool enabled = root["enabled"].as(); + if (!loadMqttConfig()) + println("Cannot load MQTT config"); + bool mqttConfigured = configureMqtt( root["enabled"].as(), root["host"].as(), diff --git a/src/config/config.h b/src/config/config.h index 661b8b8..a5ec99d 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -16,21 +16,25 @@ #define SERIAL_NUMBER "4823" -#define MAC_ADDRESS_MQTT_TOPIC "/digitum/intercom_bridge4823/out/mac" -#define IP_ADDRESS_MQTT_TOPIC "/digitum/intercom_bridge4823/out/ip" -#define SERIAL_NUMBER_MQTT_TOPIC "/digitum/intercom_bridge4823/out/sn" +#define DEFAULT_OUTPUT_TOPIC_PATH "/digitum/intercom_bridge4823/out/" +#define JSON_TOPIC_PATH "/digitum/intercom_bridge4823/out/json" -#define FLAT_NUMBER_MQTT_TOPIC "/digitum/intercom_bridge4823/out/flat_number" -#define STATE_MQTT_TOPIC "/digitum/intercom_bridge4823/out/state" +#define MAC_ADDRESS_MQTT_TOPIC "mac" +#define IP_ADDRESS_MQTT_TOPIC "ip" +#define SERIAL_NUMBER_MQTT_TOPIC "sn" + +#define FLAT_NUMBER_MQTT_TOPIC "flat_number" +#define STATE_MQTT_TOPIC "state" #define LED_PIN 32 #define DRY_CONT_PIN 15 #define DOOR_SENS_PIN 114 #define DATA_PIN 12 -#define DATA_PERIOD 120 // microseconds +#define DATA_PERIOD 1 // microseconds -#define PRINT_RAW_SIGNAL_FLAG 1 +#define PRINT_RAW_SIGNAL_FLAG 0 +#define PRINT_MQTT_DEBUG_FLAG 0 #define MAX_FEATURES_SIZE 256 #define MAX_NETWORK_STATUS_SIZE 1024 diff --git a/src/domain/stateMachine.cpp b/src/domain/stateMachine.cpp index 781fe77..f14bcc3 100644 --- a/src/domain/stateMachine.cpp +++ b/src/domain/stateMachine.cpp @@ -38,7 +38,7 @@ void receiveData(int data) { previousData = data; } -void changeState(State state, bool resetCountersFlag) { +void changeState(State state, bool resetCountersFlag=true) { currentState = state; switch (state) { diff --git a/src/domain/stateMachine.h b/src/domain/stateMachine.h index 7876d8e..09fa79a 100644 --- a/src/domain/stateMachine.h +++ b/src/domain/stateMachine.h @@ -20,6 +20,5 @@ enum State { void resetCounters(); void updateStateMachine(int data); void initStateMachine(); -void changeState(State state, bool resetCountersFlag=true); #endif // STATE_MACHINE_H \ No newline at end of file diff --git a/src/domain/stateMachineVizit.cpp.disabled b/src/domain/stateMachineVizit.cpp.disabled new file mode 100644 index 0000000..aaa6ebf --- /dev/null +++ b/src/domain/stateMachineVizit.cpp.disabled @@ -0,0 +1,145 @@ +#include "utils/print.h" +#include "config/config.h" +#include "infra/mqtt.h" +#include "infra/led.h" +#include "domain/stateMachineVizit.h" + +State currentState = NOT_CONNECTED; +int countZeros = 0; +int countOnes = 0; + +int previousData = 0; +int dataLength = 0; +int signalDuration = 0; + +void resetCounters() { + countZeros = 0; + countOnes = 0; + + previousData = 0; + dataLength = 0; + signalDuration = 0; +} + +void writeState(char* message) { + println(message); + publishToMQTT(STATE_MQTT_TOPIC, message); +} + +void receiveDigit(int data) { + if (data != previousData) { + if (previousData == HIGH) { + dataLength++; + } + signalDuration = 0; + } else { + signalDuration++; + } + previousData = data; +} + +void changeState(State state, bool resetCountersFlag=true) { + currentState = state; + + switch (state) { + case NOT_CONNECTED: + ledTurnOff(); + writeState("not connected"); + break; + case CONNECTED: + ledTurnOn(); + writeState("connected"); + break; + case RECEIVING_FIRST_DIGIT: + writeState("receiving data"); + break; + } + + if (resetCountersFlag) + resetCounters(); +} + +void flatReceived() { + int flat = dataLength/2; + + if (flat < 1) + return; + + println("| data length: ", dataLength); + println("| flat: ", flat); + publishToMQTT(FLAT_NUMBER_MQTT_TOPIC, flat); +} + +void updateStateMachine(int data) { + switch (currentState) { + case NOT_CONNECTED: + if (data == LOW) {} + else if (data == HIGH) { + changeState(CONNECTED); + } + break; + + case CONNECTED: + if (data == LOW) { + countZeros++; + if (countZeros >= NOT_CONNECTED_THRESHOLD) { + changeState(NOT_CONNECTED); + } + } else if (data == HIGH) { + if (countZeros >= INITIALIZING_CALL_THRESHOLD) { + changeState(RECEIVING_FIRST_DIGIT); + } + } + break; + + case RECEIVING_FIRST_DIGIT: + receiveDigit(data); + + if (data == LOW) { + countOnes = 0; + countZeros++; + } else if (data == HIGH) { + countZeros = 0; + countOnes++; + if (countOnes >= DATA_RECEIVED_THESHOLD) { + changeState(CONNECTED); + } + if (countOnes >= CONNECTED_THRESHOLD) { + changeState(CONNECTED); + } + } + break; + + case DATA_RECEIVED: + if (data == LOW) { + countZeros++; + if (countZeros >= CALL_ENDED_THRESHOLD) { + changeState(CALL_ENDED); + } + } else if (data == HIGH) { + countOnes++; + if (countOnes >= CONNECTED_THRESHOLD) { + changeState(CONNECTED); + } + break; + } + + case CALL_ENDED: + if (data == LOW) { + countZeros++; + if (countZeros >= NOT_CONNECTED_THRESHOLD) { + changeState(NOT_CONNECTED); + } + } else if (data == HIGH) { + countOnes++; + if (countOnes >= CONNECTED_THRESHOLD) { + changeState(CONNECTED); + } + break; + } + } +} + +void initStateMachine() { + changeState(CONNECTED); +} \ No newline at end of file diff --git a/src/domain/stateMachineVizit.h.disabled b/src/domain/stateMachineVizit.h.disabled new file mode 100644 index 0000000..cfd282b --- /dev/null +++ b/src/domain/stateMachineVizit.h.disabled @@ -0,0 +1,25 @@ +#ifndef STATE_MACHINE_H +#define STATE_MACHINE_H + +#include + +#define CONNECTED_THRESHOLD 50000 +#define NOT_CONNECTED_THRESHOLD 50000 +#define INITIALIZING_CALL_THRESHOLD 45 +#define DATA_RECEIVED_THESHOLD 250 +#define CALL_ENDED_THRESHOLD 10000 + +enum State { + NOT_CONNECTED, + CONNECTED, + RECEIVING_FIRST_DIGIT, + RECEIVING_SECOND_DIGIT, + DATA_RECEIVED, + CALL_ENDED +}; + +void resetCounters(); +void updateStateMachine(int data); +void initStateMachine(); + +#endif // STATE_MACHINE_H \ No newline at end of file diff --git a/src/infra/mqtt.cpp b/src/infra/mqtt.cpp index 2ed038b..f2d782d 100644 --- a/src/infra/mqtt.cpp +++ b/src/infra/mqtt.cpp @@ -9,6 +9,7 @@ TimerHandle_t mqttReconnectTimer; AsyncMqttClientDisconnectReason mqttDisconnectReason; DynamicJsonDocument mqttConf(1024); +String mqttOutputJson = ""; bool mqttConnected = false; bool mqttEnabled = true; @@ -19,25 +20,71 @@ char* retainedClientId; char* retainedUsername; char* retainedPassword; +template +void publishJSONToMQTT(const char* topic, T message) { + DynamicJsonDocument doc(8192); + DeserializationError error = deserializeJson(doc, mqttOutputJson); + + doc[topic] = message; + + mqttOutputJson = ""; + serializeJson(doc, mqttOutputJson); + + if (PRINT_MQTT_DEBUG_FLAG) + println("Sending to MQTT topic '", JSON_TOPIC_PATH, "': ", mqttOutputJson); + mqttClient.publish(JSON_TOPIC_PATH, 2, true, mqttOutputJson.c_str()); +} + +const char* getFullTopic(const char* topic) { + return (String(DEFAULT_OUTPUT_TOPIC_PATH) + String(topic)).c_str(); +} + void publishToMQTT(const char* topic, const char* message) { - mqttClient.publish(topic, 2, true, message); + const char* fullTopic = getFullTopic(topic); + + if (PRINT_MQTT_DEBUG_FLAG) + println("Sending to MQTT topic '", fullTopic, "': ", message); + + mqttClient.publish(fullTopic, 2, true, message); + publishJSONToMQTT(topic, message); } void publishToMQTT(const char* topic, int message) { + const char* fullTopic = getFullTopic(topic); + + if (PRINT_MQTT_DEBUG_FLAG) + println("Sending to MQTT topic '", fullTopic, "': ", message); + char num_char[10]; sprintf(num_char, "%d", message); mqttClient.publish(topic, 2, true, num_char); + + publishJSONToMQTT(topic, message); } void publishToMQTT(const char* topic, float message) { + const char* fullTopic = getFullTopic(topic); + + if (PRINT_MQTT_DEBUG_FLAG) + println("Sending to MQTT topic '", fullTopic, "': ", message); + char num_char[10]; dtostrf(message, 1, 2, num_char); // Convert float to string mqttClient.publish(topic, 2, true, num_char); + + publishJSONToMQTT(topic, message); } void publishToMQTT(const char* topic, bool message) { + const char* fullTopic = getFullTopic(topic); + + if (PRINT_MQTT_DEBUG_FLAG) + println("Sending to MQTT topic '", fullTopic, "': ", message); + const char* bool_str = message ? "true" : "false"; mqttClient.publish(topic, 2, true, bool_str); + + publishJSONToMQTT(topic, message); } void onMqttConnect(bool sessionPresent) { diff --git a/src/infra/mqtt.h b/src/infra/mqtt.h index bad56b6..cff7e4e 100644 --- a/src/infra/mqtt.h +++ b/src/infra/mqtt.h @@ -21,6 +21,7 @@ AsyncMqttClient& getMqttClient(); AsyncMqttClientDisconnectReason& getMqttDisconnectReason(); bool getMqttConnected(); bool getMqttEnabled(); +bool loadMqttConfig(); bool configureMqtt( bool enabled, String host, diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c90c6ec --- /dev/null +++ b/test.txt @@ -0,0 +1,13 @@ +1 - 0101010101010101010 +1 - 0101010101010101010111111111111110 + +2 - 0101010101010101011111111111111010 +2 - 01010101010101010101111111111111010 + +3 - 0101010101010101010111111111111101010 +4 - 010101010101010000000000000000000000000000000000000000000001011111111111110101010 +5 - 01010101010101010101111111111111111111111111111111111111111111111111111111111010101010 +6 - 0101010101010101010111111111111101010101010 +7 - 010101010101010101011111111111111111111111111111111111111111111111111111111110101010101010 + +10 - 011111111111111111111111111111111111111111111111111111111110101010101010101010 \ No newline at end of file