Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

28 linhas
1.3 KiB

  1. // If using the MQTT client PubSubClient it must be ensured that the request topic for each method is subscribed like the following.
  2. _mqttClient.subscribe("MQTTnet.RPC/+/ping");
  3. _mqttClient.subscribe("MQTTnet.RPC/+/do_something");
  4. // It is not allowed to change the structure of the topic. Otherwise RPC will not work. So method names can be separated using
  5. // an _ or . but no +, # or /.. If it is required to distinguish between devices own rules can be defined like the following.
  6. _mqttClient.subscribe("MQTTnet.RPC/+/deviceA.ping");
  7. _mqttClient.subscribe("MQTTnet.RPC/+/deviceB.ping");
  8. _mqttClient.subscribe("MQTTnet.RPC/+/deviceC.getTemperature");
  9. // Within the callback of the mqtt client the topic must be checked if it belongs to MQTTnet RPC. The following code shows one
  10. // possible way.
  11. void mqtt_Callback(char *topic, byte *payload, unsigned int payloadLength)
  12. {
  13. String topicString = String(topic);
  14. if (topicString.startsWith("MQTTnet.RPC/")) {
  15. String responseTopic = topicString + String("/response");
  16. if (topicString.endsWith("/deviceA.ping")) {
  17. mqtt_publish(responseTopic, "pong", false);
  18. return;
  19. }
  20. }
  21. }
  22. // Do not send response message with the _retain_ flag set to true.
  23. // All required data for a RPC call and the result must be placed into the payload.