json sending with mqtt
This commit is contained in:
parent
3aabf4f8c1
commit
a79fbb1ee5
4
prog.txt
Normal file
4
prog.txt
Normal file
@ -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
|
@ -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<bool>();
|
||||
|
||||
if (!loadMqttConfig())
|
||||
println("Cannot load MQTT config");
|
||||
|
||||
bool mqttConfigured = configureMqtt(
|
||||
root["enabled"].as<bool>(),
|
||||
root["host"].as<String>(),
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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
|
145
src/domain/stateMachineVizit.cpp.disabled
Normal file
145
src/domain/stateMachineVizit.cpp.disabled
Normal file
@ -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);
|
||||
}
|
25
src/domain/stateMachineVizit.h.disabled
Normal file
25
src/domain/stateMachineVizit.h.disabled
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef STATE_MACHINE_H
|
||||
#define STATE_MACHINE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#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
|
@ -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 <typename T>
|
||||
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) {
|
||||
|
@ -21,6 +21,7 @@ AsyncMqttClient& getMqttClient();
|
||||
AsyncMqttClientDisconnectReason& getMqttDisconnectReason();
|
||||
bool getMqttConnected();
|
||||
bool getMqttEnabled();
|
||||
bool loadMqttConfig();
|
||||
bool configureMqtt(
|
||||
bool enabled,
|
||||
String host,
|
||||
|
13
test.txt
Normal file
13
test.txt
Normal file
@ -0,0 +1,13 @@
|
||||
1 - 0101010101010101010
|
||||
1 - 0101010101010101010111111111111110
|
||||
|
||||
2 - 0101010101010101011111111111111010
|
||||
2 - 01010101010101010101111111111111010
|
||||
|
||||
3 - 0101010101010101010111111111111101010
|
||||
4 - 010101010101010000000000000000000000000000000000000000000001011111111111110101010
|
||||
5 - 01010101010101010101111111111111111111111111111111111111111111111111111111111010101010
|
||||
6 - 0101010101010101010111111111111101010101010
|
||||
7 - 010101010101010101011111111111111111111111111111111111111111111111111111111110101010101010
|
||||
|
||||
10 - 011111111111111111111111111111111111111111111111111111111110101010101010101010
|
Loading…
x
Reference in New Issue
Block a user