-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RPC not work in esp-idf #200
Comments
hey , i tried your pull request and the same problem even the arduino rpc exemple do the same |
Do I understand you correctly the RPC is received but only at the start after you subscribe. But for all subsequent calls, the RPC method does not trigger anymore and the device does not print that it received any RPC messages? Because the message you entered seems good to me (it prints some garbage afterwards, because the payload is uint8_t and therefore has not null termination byte), but that can be ignored. |
Yes you right that's my issue |
I had a look at the code and I have one idea what the issue could be. Not 100% sure if my inkling is correct tough. Before the connection has been lost are your reconnecting to ThingsBoard? What I mean is are you specifically calling the And if you are would it be possible for you to add log messages to the Simply adjust the method like this. void Resubscribe_Topics() {
if (!m_rpc_callbacks.empty()) {
bool const result = m_client.subscribe(RPC_SUBSCRIBE_TOPIC);
Serial.printf("Result of topic resubscription: %s \n", result ? "Success" : "Failure");
}
if (!m_shared_attribute_update_callbacks.empty()) {
bool const result = mm_client.subscribe(ATTRIBUTE_TOPIC);
Serial.printf("Result of topic resubscription: %s \n", result ? "Success" : "Failure");
}
} |
Hey, this is my main code, and in it, I just call tb.connect one time.
#if ENCRYPTED
} tb.Resubscribe_Topics();
}` |
Thanks that's interesting, can you tell me what is printed on the serial, when the Additionally could you move the if (!tb.connected()) {
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
tb.Resubscribe_Topics();
} |
hey thats what i see in terminal: I (16348) Resubscribe: Result of topic resubscription: Success I (17353) Resubscribe: Result of topic resubscription: Success I (18358) Resubscribe: Result of topic resubscription: Success and no when i move tb.Resubscribe_Topics() to that block it stop working |
Interesting so it does not seem to be because we lose connection with the MQTT broker and have to reconnect again. Because that was my inital assumption. Somehow we loose the topic subscritption because of another reason, but I can't tell what could cause that to occur, because the library never implicitly unsubscribes server-side RPC or shared attribute updates. Just to be sure you are sending for example one RPC request from the server to the client, that is still received and the next request will already not work again. It only works if you repeadatly call tb.Resubscribe_Topics();?. Just to make sure can you add this log message to see if you disconnect, while you receive the last RPC message that is still handled by the client. if (!tb.connected()) {
tb.connect(THINGSBOARD_SERVER, TOKEN, THINGSBOARD_PORT);
Serial.printf("Lost connection with MQTT broker, attempting to reconnect");
} |
I always ask why happen to me and i use the same code that others use |
You are not the only one, that has this issue as far as I know. But for the othere developer it's an ESP32-S3 and they do receive RPC after the first one, but after around 5 - 6 hours they don't anymore. So I'm not 100% sure it is the same issue but it very well could be. For now it is probably a good idea to add the |
Understood, thanks for your help i had two week trying to fix the issue hhhh. |
Hello, I made a RPC project in esp-idf, but it did not work. He showed me that he was subscribed to the RPC, but I got no response from the terminal when I sent an RPC from Thingsboard. but when I restart the esp32 and send immediately an RPC from the thingsboard, the terminal shows that RPC have been received just after he subscribed to it, and he stops receiving RPCs after that. I hope I found a solution and thank you.
thingsboard version 12.2
`
#include <esp_netif.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <nvs_flash.h>
#include <string.h>
// Whether the given script is using encryption or not,
// generally recommended as it increases security (communication with the server is not in clear text anymore),
// it does come with an overhead tough as having an encrypted session requires a lot of memory,
// which might not be avaialable on lower end devices.
#define ENCRYPTED false
#include <Espressif_MQTT_Client.h>
#include <ThingsBoard.h>
// Examples using arduino used PROGMEM to save constants into flash memory,
// this is not needed when using Espressif IDF because per default
// all read only variables will be saved into DROM (flash memory).
// See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/memory-types.html#drom-data-stored-in-flash
// for more information about the aforementioned feature
constexpr char WIFI_SSID[] = "YOUR_WIFI_SSID";
constexpr char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD";
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
constexpr char TOKEN[] = "YOUR_DEVICE_ACCESS_TOKEN";
// Thingsboard we want to establish a connection to
constexpr char THINGSBOARD_SERVER[] = "demo.thingsboard.io";
// MQTT port used to communicate with the server, 1883 is the default unencrypted MQTT port,
// whereas 8883 would be the default encrypted SSL MQTT port
#if ENCRYPTED
constexpr uint16_t THINGSBOARD_PORT = 8883U;
#else
constexpr uint16_t THINGSBOARD_PORT = 1883U;
#endif
// Maximum size packets will ever be sent or received by the underlying MQTT client,
// if the size is to small messages might not be sent or received messages will be discarded
constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
#if ENCRYPTED
// See https://comodosslstore.com/resources/what-is-a-root-ca-certificate-and-how-do-i-download-it/
// on how to get the root certificate of the server we want to communicate with,
// this is needed to establish a secure connection and changes depending on the website.
constexpr char ROOT_CERT[] = R"(-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOF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Ql9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----
)";
#endif
constexpr char RPC_TEMPERATURE_METHOD[] = "example_set_temperature";
constexpr char RPC_SWITCH_METHOD[] = "example_set_switch";
constexpr char RPC_TEMPERATURE_KEY[] = "temp";
constexpr char RPC_SWITCH_KEY[] = "switch";
constexpr char RPC_RESPONSE_KEY[] = "example_response";
// Initalize the Mqtt client instance
Espressif_MQTT_Client mqttClient;
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
// Statuses for subscribing to rpc
bool subscribed = false;
static void on_got_ip(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) {
ESP_LOGI("RPC Example", "Got IP");
}
void InitWiFi() {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
}
RPC_Response processTemperatureChange(const RPC_Data &data) {
float example_temperature = data[RPC_TEMPERATURE_KEY];
ESP_LOGI("RPC Example", "Received the set temperature RPC method: %f", example_temperature);
}
RPC_Response processSwitchChange(const RPC_Data &data) {
bool switch_state = data[RPC_SWITCH_KEY];
ESP_LOGI("RPC Example", "Received the set switch method: %d", switch_state);
}
extern "C" void app_main(void) {
InitWiFi();
}
`
the code i get it from here #199
The text was updated successfully, but these errors were encountered: