Problem filtering topic with wildcard
Closed this issue · 7 comments
Hi,
I need some help on my ESP8266 regarding two questions please:
-
This is how I created a filter (IF clause) with a wildcard. I tried the example below, but no sucess. Only works if I put the complete topic name such "tele/Sensor/RESULT"
String Topic_Name = "tele/+/RESULT"; if (e->Topic() == Topic_Name.c_str())
-
I use the code above on sMQTTPublicClientEvent event and then I filter by this unique topic. Exists any way to my MQTT boker subscribe itself this topic and then show the payload? Or the option above is the unique to achive that?
Thanks again and Best Regards
Hi,
- If you set it as in the example, you will get the following result:
"tele/sensor1/RESULT";
"tele/sensor2/RESULT";
etc
Therefore, it cannot be directly compared with a filter. - All published messages will get into the function, you can decide which ones to filter.
Hi my friend,
Thanks for your support.
I tested before the umqtt library but after my implementation I realized it only supports 5 mqtt conected clients. On this library i defined a subscribe with the topic "tele/+/RESULT" and i had a "ondata" method to capture only this payload.
With your library i need to capture the payload on the PublicClientEvent and then filter my desired topic right? Or do you see anybother possible approach?
About the filter I was thinking to have some kind of substring to bypass the wildcard. Or you have any other sugestion?
Other question raised now. I need to implement any kind of connected clients to print and send via json/ajax the clients but you only have isconectedclient function right?
Many thanks again and regards
With your library i need to capture the payload on the PublicClientEvent and then filter my desired topic right?
Yes.
About the filter I was thinking to have some kind of substring to bypass the wildcard.
For such a filter "tele/+/RESULT" I checked the result up to + and if this is what we need, then I did additional checks.
Other question raised now. I need to implement any kind of connected clients to print and send via json/ajax the clients but you only have isconectedclient function right?
No, this is an internal function. If you need to send something to clients, then there is a function to publish.
Also, if you need to send to a specific client, this can be done when he subscribes to a specific topic.
For example, the client has subscribed to "topic/deviceID/get" you send something to this topic in the handler.
Hi,
Thanks again for your message.
About the filter I didn't understood if you will check something on this library or if the unique solution is to use some kind of substring parser.
About the json/ajax, my application have a different need. I expose one http html webpage and have a table where I see my mqtt clients on broker and the unique way to have it working in real time without webpage refresh is trought ajax over html.
So, I customized your useful library and added this funtion below to provide me the clients list with the clientID and with the IP Address. I leave you a print to show the final result :)
Thanks again and best Regards,
void clientsConnected()
{
sMQTTClientList::iterator clit;
MQTT_Clients = 0;
for (clit = clients.begin(); clit != clients.end(); clit++)
{
sMQTTClient *c = *clit;
Serial.print("Exists ");
Serial.print(clients.size());
Serial.println(" MQTT clients connected on ESP8266Broker");
Tabela_MQTT_Clientes[MQTT_Clients][0] = c->getClientIP().toString();//1ªColuna com o IP Address
Tabela_MQTT_Clientes[MQTT_Clients][1] = c->getClientId().c_str();//2ªColuna com o Client_ID
Serial.print("MQTT client with the clientID: ");
Serial.print(c->getClientId().c_str());
Serial.print(" and IP Address: ");
Serial.println(c->getClientIP());
Serial.println("");
MQTT_Clients++;
}
About the json/ajax, my application have a different need. I expose one http html webpage and have a table where I see my mqtt clients on broker and the unique way to have it working in real time without webpage refresh is trought ajax over html.
So, I customized your useful library and added this funtion below to provide me the clients list with the clientID and with the IP Address. I leave you a print to show the final result :)
Hello, the following solution can be used now:
the callback receives events about the connection of the client and its disconnection. At this moment, you can get the client's parameters and save it for yourself for further work or immediately send it somewhere.
About the filter I didn't understood if you will check something on this library or if the unique solution is to use some kind of substring parser.
Good question. As I understand it, you need the ability to subscribe to certain topics by a broker and, when publishing to these topics, have highlighting what exactly was published in these topics?
Example:
sMQTTSubscribeBrokerEvent
Hi,
About the first situation, your idea seems good, but in my use case I only need the number of clients on demand for now. But your idea keeps in my possible to do list :)
About the topics, yes, on my broker I only need one specific topic where I received my energy readings from my Wifi energy circuit braker. I leave the code inside the if topic as an example, but my doubt still about the possibility to use the wildcard to capture the topic and see only this payload on the sMQTTPublicClientEvent function.
DynamicJsonDocument doc(2048);
Serial.print("New Publish on topic: ");
Serial.println(e->Topic().c_str());
Serial.print("Payload: ");
Serial.println(e->Payload().c_str());
//Parse response
deserializeJson(doc, e->Payload().c_str());
//*** Quadro Measures **//
String Quadro_Measures = doc["SerialReceived"];
//*** Energy **//
String Energy = Quadro_Measures.substring(6,14);//Efectua o parse do valor pretendido
int Energy_Divisor = 100;//Define o valor do divisor
Energy_Value = Decode_Message(Energy,Energy_Divisor);//Função que ordena a String e converte para uma medida Double
Serial.print("Energy: ");
Serial.println(Energy_Value);
Thanks again and Best Regards
Hi my friend,
I solved my case with the substring parsing and is working as desired. Is an artificial way to use the wildcard "+" on a topic subscribe.
I leave the code below if someone need something similar.
I have my Broker working since yesterday and works perfectly with 7 mqtt clients connected :)
//====================================================================================
// Função client publish a message on Broker
//====================================================================================
case Public_sMQTTEventType:
{
sMQTTPublicClientEvent e=(sMQTTPublicClientEvent)event;
String Topico_Recebido = e->Topic().c_str();
String Topico_Inicio = Topico_Recebido.substring(0,5);//Efectua o parse do valor pretendido para obter o "tele/"
String Topico_Fim = Topico_Recebido.substring(Topico_Recebido.length()-7,Topico_Recebido.length());//Efectua o parse do valor pretendido para obter o "/RESULT"
if (Topico_Inicio == "tele/" && Topico_Fim == "/RESULT")//Apresenta apenas os dados do Sensor do Quadro (tele/+/RESULT ou tele/SensorQuadro/RESULT)
{ .... }
Thanks again for your time and Best Regards.