early implementation of MQTT. Deactivate it to avoid intensive usage
This commit is contained in:
parent
43ff899cde
commit
6f41843cd5
@ -41,10 +41,10 @@ void setup_wifi() {
|
||||
// Connexion au réseau Wi-Fi
|
||||
DebugPort.println();
|
||||
DebugPort.print("Connecting to ");
|
||||
DebugPort.println(ssid);
|
||||
DebugPort.println(STASSID);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, passPhrase);
|
||||
WiFi.begin(STASSID, STAPSK);
|
||||
|
||||
int c = 0;
|
||||
// Attendre la connexion Wi-Fi
|
||||
@ -165,10 +165,13 @@ void loop() {
|
||||
server.handleClient();
|
||||
readTicPort();
|
||||
|
||||
|
||||
//Interface MQTT
|
||||
if (!mqttclient.connected()) {
|
||||
mqttConnect();
|
||||
}
|
||||
|
||||
//mqttPublish(&mqttclient);
|
||||
mqttclient.loop();
|
||||
|
||||
|
||||
|
173
tic.cpp
173
tic.cpp
@ -1,7 +1,9 @@
|
||||
#include "lwip/ip.h"
|
||||
#include "tic.h"
|
||||
#include "serial.h"
|
||||
#include "secret.h"
|
||||
#include <Arduino.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// #define DEBUG 1
|
||||
|
||||
@ -19,8 +21,7 @@ RelaisStatus relaisStatus; // definition du relais status
|
||||
Action actionJp1[11]; // actions définie pour jour +1
|
||||
int nbActions;
|
||||
|
||||
static struct GroupDetail processGroup(String group)
|
||||
{
|
||||
static struct GroupDetail processGroup(String group) {
|
||||
struct GroupDetail gd;
|
||||
int indexgrp = group.indexOf(HT);
|
||||
gd.name = group.substring(0, indexgrp);
|
||||
@ -43,8 +44,7 @@ static struct GroupDetail processGroup(String group)
|
||||
return gd;
|
||||
}
|
||||
|
||||
static void processStge(RegistreStatus *rs, String value)
|
||||
{
|
||||
static void processStge(RegistreStatus *rs, String value) {
|
||||
char stge[9] = "";
|
||||
// copy in the char array
|
||||
strncpy(stge, value.c_str(), 8);
|
||||
@ -53,8 +53,7 @@ static void processStge(RegistreStatus *rs, String value)
|
||||
rs->uli = l;
|
||||
}
|
||||
|
||||
static void processRelais(RelaisStatus *rs, String value)
|
||||
{
|
||||
static void processRelais(RelaisStatus *rs, String value) {
|
||||
char stge[4] = "";
|
||||
// copy in the char array
|
||||
strncpy(stge, value.c_str(), 3);
|
||||
@ -62,24 +61,19 @@ static void processRelais(RelaisStatus *rs, String value)
|
||||
rs->ui = strtoul(stge, NULL, 16);
|
||||
}
|
||||
|
||||
static void processActionsCalendrier(String value)
|
||||
{
|
||||
static void processActionsCalendrier(String value) {
|
||||
nbActions = 0;
|
||||
String s = value;
|
||||
while (s.length() > 0)
|
||||
{
|
||||
while (s.length() > 0) {
|
||||
int index = s.indexOf(SP);
|
||||
if (index == -1) // No space found
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
char data[9] = "";
|
||||
data[8] = '\0';
|
||||
strncpy(data, s.substring(0, index).c_str(), 8);
|
||||
if (strncmp(data, NONUTILE, 8) != 0)
|
||||
{
|
||||
if (strncmp(data, NONUTILE, 8) != 0) {
|
||||
char stge[5] = "";
|
||||
// copy ssss field
|
||||
memcpy(stge, &data[4], 4);
|
||||
@ -102,19 +96,14 @@ static void processActionsCalendrier(String value)
|
||||
*
|
||||
* @param data A reference to a String containing the data frame to be processed.
|
||||
*/
|
||||
static void processTrame(String &data)
|
||||
{
|
||||
while (data.length() > 0)
|
||||
{
|
||||
static void processTrame(String &data) {
|
||||
while (data.length() > 0) {
|
||||
// Find the position of the next carriage return (CR) character
|
||||
int index = data.indexOf(CR);
|
||||
// If no CR is found, exit the loop
|
||||
if (index == -1)
|
||||
{
|
||||
if (index == -1) {
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Extract the group string between the start and the CR character
|
||||
String group = data.substring(1, index);
|
||||
// Process the group to extract detailed information
|
||||
@ -122,25 +111,18 @@ static void processTrame(String &data)
|
||||
|
||||
// Check if the extracted group name matches any user-selected etiquette
|
||||
int t = 0;
|
||||
while ((SelectedEtiquette[t] != gd.name) && (t < NB_ETIQUETTE))
|
||||
{
|
||||
while ((SelectedEtiquette[t] != gd.name) && (t < NB_ETIQUETTE)) {
|
||||
++t;
|
||||
}
|
||||
// If a match is found, update the corresponding TicValues entry
|
||||
if (t < NB_ETIQUETTE)
|
||||
{
|
||||
if (t < NB_ETIQUETTE) {
|
||||
TicValues[t] = gd;
|
||||
// Depending on the group name, call the appropriate processing function
|
||||
if (gd.name == "STGE")
|
||||
{
|
||||
if (gd.name == "STGE") {
|
||||
processStge(®Status, gd.value);
|
||||
}
|
||||
else if (gd.name == "RELAIS")
|
||||
{
|
||||
} else if (gd.name == "RELAIS") {
|
||||
processRelais(&relaisStatus, gd.value);
|
||||
}
|
||||
else if (gd.name == "PJOURF+1")
|
||||
{
|
||||
} else if (gd.name == "PJOURF+1") {
|
||||
processActionsCalendrier(gd.value);
|
||||
}
|
||||
}
|
||||
@ -149,19 +131,16 @@ static void processTrame(String &data)
|
||||
}
|
||||
}
|
||||
|
||||
static char *actionJp1AsJson()
|
||||
{
|
||||
static char *actionJp1AsJson() {
|
||||
const int bufferSize = 1000;
|
||||
static char jsonBuffer[bufferSize]; // Adjust size as needed
|
||||
snprintf(jsonBuffer, bufferSize, "\"PJOURF+1\": [");
|
||||
|
||||
for (int i = 0; i < nbActions; i++)
|
||||
{
|
||||
for (int i = 0; i < nbActions; i++) {
|
||||
// Format each action
|
||||
char actionJson[256]; // To store individual action JSON string
|
||||
String relaisSec = "";
|
||||
switch ((unsigned int)actionJp1[i].action.bits.relaisSec)
|
||||
{
|
||||
switch ((unsigned int)actionJp1[i].action.bits.relaisSec) {
|
||||
case 0:
|
||||
relaisSec = "no change";
|
||||
break;
|
||||
@ -189,12 +168,9 @@ static char *actionJp1AsJson()
|
||||
actionJp1[i].action.bits.relais1, actionJp1[i].action.bits.index);
|
||||
|
||||
// Append the current action's JSON to the overall JSON buffer
|
||||
if (i == (nbActions - 1))
|
||||
{ // Last item, no comma at the end
|
||||
if (i == (nbActions - 1)) { // Last item, no comma at the end
|
||||
strncat(jsonBuffer, actionJson, bufferSize - strlen(jsonBuffer) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
strncat(jsonBuffer, actionJson, bufferSize - strlen(jsonBuffer) - 1);
|
||||
strncat(jsonBuffer, ",", bufferSize - strlen(jsonBuffer) - 1);
|
||||
}
|
||||
@ -205,8 +181,7 @@ static char *actionJp1AsJson()
|
||||
return jsonBuffer;
|
||||
}
|
||||
|
||||
static char *relaisStatusAsJson(RelaisStatusBits *status, String rawValue)
|
||||
{
|
||||
static char *relaisStatusAsJson(RelaisStatusBits *status, String rawValue) {
|
||||
// Pre-allocate buffer large enough to hold the JSON string
|
||||
static char response[150]; // Adjust size as needed
|
||||
// Use snprintf to construct the JSON string efficiently
|
||||
@ -235,8 +210,7 @@ static char *relaisStatusAsJson(RelaisStatusBits *status, String rawValue)
|
||||
return response;
|
||||
}
|
||||
|
||||
static char *registreStatusAsJson(RegistreStatusBits *status, String rawValue)
|
||||
{
|
||||
static char *registreStatusAsJson(RegistreStatusBits *status, String rawValue) {
|
||||
// Pre-allocate buffer large enough to hold the JSON string
|
||||
static char response[1000]; // Adjust size as needed
|
||||
|
||||
@ -286,27 +260,18 @@ static char *registreStatusAsJson(RegistreStatusBits *status, String rawValue)
|
||||
return response;
|
||||
}
|
||||
|
||||
String ticValuesAsJson()
|
||||
{
|
||||
String ticValuesAsJson() {
|
||||
String response = "{";
|
||||
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i)
|
||||
{
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i) {
|
||||
|
||||
if (SelectedEtiquette[i] == "STGE")
|
||||
{
|
||||
if (SelectedEtiquette[i] == "STGE") {
|
||||
response += registreStatusAsJson(®Status.bits, TicValues[i].value);
|
||||
}
|
||||
else if (SelectedEtiquette[i] == "RELAIS")
|
||||
{
|
||||
} else if (SelectedEtiquette[i] == "RELAIS") {
|
||||
response += relaisStatusAsJson(&relaisStatus.bits, TicValues[i].value);
|
||||
}
|
||||
else if (SelectedEtiquette[i] == "PJOURF+1")
|
||||
{
|
||||
} else if (SelectedEtiquette[i] == "PJOURF+1") {
|
||||
response += actionJp1AsJson();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
static char jres[150]; // Adjust size as needed
|
||||
|
||||
// Use snprintf to construct the JSON string efficiently
|
||||
@ -317,8 +282,7 @@ String ticValuesAsJson()
|
||||
response += jres;
|
||||
}
|
||||
|
||||
if (i < (NB_ETIQUETTE - 1))
|
||||
{
|
||||
if (i < (NB_ETIQUETTE - 1)) {
|
||||
response += ',';
|
||||
}
|
||||
}
|
||||
@ -326,20 +290,16 @@ String ticValuesAsJson()
|
||||
return response;
|
||||
}
|
||||
|
||||
String ticBasicValuesAsJson()
|
||||
{
|
||||
String ticBasicValuesAsJson() {
|
||||
String response = "{";
|
||||
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i)
|
||||
{
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i) {
|
||||
|
||||
if (SelectedEtiquette[i] == "LTARF" || SelectedEtiquette[i] == "EAST" || SelectedEtiquette[i] == "EASF01" || SelectedEtiquette[i] == "EASF02" || SelectedEtiquette[i] == "EASF03" || SelectedEtiquette[i] == "EASF04" || SelectedEtiquette[i] == "EASD01" || SelectedEtiquette[i] == "EASD02" || SelectedEtiquette[i] == "EASD03" || SelectedEtiquette[i] == "EASD04" || SelectedEtiquette[i] == "EAIT" || SelectedEtiquette[i] == "ERQ1" || SelectedEtiquette[i] == "ERQ2" || SelectedEtiquette[i] == "ERQ3" || SelectedEtiquette[i] == "ERQ4" || SelectedEtiquette[i] == "IRMS1" || SelectedEtiquette[i] == "IRMS2" || SelectedEtiquette[i] == "IRMS3" || SelectedEtiquette[i] == "URMS1" || SelectedEtiquette[i] == "URMS2" || SelectedEtiquette[i] == "URMS3" || SelectedEtiquette[i] == "SINSTS" || SelectedEtiquette[i] == "SINSTSI" || SelectedEtiquette[i] == "SINSTS1" || SelectedEtiquette[i] == "SINSTS2" || SelectedEtiquette[i] == "SINSTS3" || SelectedEtiquette[i] == "SINSTSI")
|
||||
{
|
||||
if (SelectedEtiquette[i] == "LTARF" || SelectedEtiquette[i] == "EAST" || SelectedEtiquette[i] == "EASF01" || SelectedEtiquette[i] == "EASF02" || SelectedEtiquette[i] == "EASF03" || SelectedEtiquette[i] == "EASF04" || SelectedEtiquette[i] == "EASD01" || SelectedEtiquette[i] == "EASD02" || SelectedEtiquette[i] == "EASD03" || SelectedEtiquette[i] == "EASD04" || SelectedEtiquette[i] == "EAIT" || SelectedEtiquette[i] == "ERQ1" || SelectedEtiquette[i] == "ERQ2" || SelectedEtiquette[i] == "ERQ3" || SelectedEtiquette[i] == "ERQ4" || SelectedEtiquette[i] == "IRMS1" || SelectedEtiquette[i] == "IRMS2" || SelectedEtiquette[i] == "IRMS3" || SelectedEtiquette[i] == "URMS1" || SelectedEtiquette[i] == "URMS2" || SelectedEtiquette[i] == "URMS3" || SelectedEtiquette[i] == "SINSTS" || SelectedEtiquette[i] == "SINSTSI" || SelectedEtiquette[i] == "SINSTS1" || SelectedEtiquette[i] == "SINSTS2" || SelectedEtiquette[i] == "SINSTS3" || SelectedEtiquette[i] == "SINSTSI") {
|
||||
|
||||
static char jres[150]; // Adjust size as needed
|
||||
|
||||
if (response != "{")
|
||||
{
|
||||
if (response != "{") {
|
||||
response += ",";
|
||||
}
|
||||
|
||||
@ -355,6 +315,17 @@ String ticBasicValuesAsJson()
|
||||
return response;
|
||||
}
|
||||
|
||||
void mqttPublish(PubSubClient *mqttclient) {
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i) {
|
||||
if (SelectedEtiquette[i] == "LTARF" || SelectedEtiquette[i] == "EAST" || SelectedEtiquette[i] == "EASF01" || SelectedEtiquette[i] == "EASF02" || SelectedEtiquette[i] == "EASF03" || SelectedEtiquette[i] == "EASF04" || SelectedEtiquette[i] == "EASD01" || SelectedEtiquette[i] == "EASD02" || SelectedEtiquette[i] == "EASD03" || SelectedEtiquette[i] == "EASD04" || SelectedEtiquette[i] == "EAIT" || SelectedEtiquette[i] == "ERQ1" || SelectedEtiquette[i] == "ERQ2" || SelectedEtiquette[i] == "ERQ3" || SelectedEtiquette[i] == "ERQ4" || SelectedEtiquette[i] == "IRMS1" || SelectedEtiquette[i] == "IRMS2" || SelectedEtiquette[i] == "IRMS3" || SelectedEtiquette[i] == "URMS1" || SelectedEtiquette[i] == "URMS2" || SelectedEtiquette[i] == "URMS3" || SelectedEtiquette[i] == "SINSTS" || SelectedEtiquette[i] == "SINSTSI" || SelectedEtiquette[i] == "SINSTS1" || SelectedEtiquette[i] == "SINSTS2" || SelectedEtiquette[i] == "SINSTS3" || SelectedEtiquette[i] == "SINSTSI") {
|
||||
String topic= MQTT_TOPIC;
|
||||
topic += "/",
|
||||
topic += SelectedEtiquette[i];
|
||||
mqttclient->publish(topic.c_str(), TicValues[i].value.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data from the TicPort and processes it according to specific control characters.
|
||||
*
|
||||
@ -369,15 +340,12 @@ String ticBasicValuesAsJson()
|
||||
*
|
||||
* The built-in LED is used to indicate the state of data reception.
|
||||
*/
|
||||
void readTicPort()
|
||||
{
|
||||
void readTicPort() {
|
||||
// Check TicPort availability
|
||||
if (TicPort.available())
|
||||
{
|
||||
if (TicPort.available()) {
|
||||
byte incomingByte = TicPort.read(); // Read a byte from the TicPort
|
||||
// Check if the incoming byte is the End Of Transmission (EOT) character
|
||||
if (incomingByte == EOT)
|
||||
{
|
||||
if (incomingByte == EOT) {
|
||||
// Force the end of transmission
|
||||
// Reject everything
|
||||
isReceiving = false;
|
||||
@ -385,18 +353,13 @@ void readTicPort()
|
||||
}
|
||||
|
||||
// Check if the system is currently receiving data
|
||||
if (isReceiving)
|
||||
{
|
||||
if (isReceiving) {
|
||||
// Check if the end of the frame is reached (ETX character)
|
||||
if (incomingByte == ETX)
|
||||
{
|
||||
if (incomingByte == ETX) {
|
||||
// Extract the useful part of the frame
|
||||
if (nActiveData == 1)
|
||||
{
|
||||
if (nActiveData == 1) {
|
||||
processTrame(data1); // Process the data in data1
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
processTrame(data2); // Process the data in data2
|
||||
}
|
||||
// Indicate that the data reception is complete
|
||||
@ -404,41 +367,29 @@ void readTicPort()
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
// Debugging information: Print the extracted data
|
||||
#ifdef DEBUG
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i)
|
||||
{
|
||||
for (int i = 0; i < NB_ETIQUETTE; ++i) {
|
||||
DebugPort.print(TicValues[i].name);
|
||||
DebugPort.print(":");
|
||||
DebugPort.println(TicValues[i].value);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Add the incoming byte to the current frame
|
||||
if (nActiveData == 1)
|
||||
{
|
||||
if (nActiveData == 1) {
|
||||
data1 += (char)incomingByte; // Append the byte to data1
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
data2 += (char)incomingByte; // Append the byte to data2
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Look for the start of the frame (STX character)
|
||||
if (incomingByte == STX)
|
||||
{
|
||||
if (incomingByte == STX) {
|
||||
isReceiving = true;
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
if (nActiveData == 1)
|
||||
{
|
||||
if (nActiveData == 1) {
|
||||
data2 = "";
|
||||
nActiveData = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
data1 = "";
|
||||
nActiveData = 1;
|
||||
}
|
||||
|
2
tic.h
2
tic.h
@ -4,6 +4,7 @@
|
||||
#define TIC_DEF
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define TIC
|
||||
|
||||
@ -159,4 +160,5 @@ const static String kPointeMobile[4] = { "no", "PM1", "PM2", "PM3" };
|
||||
void readTicPort();
|
||||
String ticValuesAsJson();
|
||||
String ticBasicValuesAsJson();
|
||||
void mqttPublish(PubSubClient *mqttclient);
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user