ESP32 MQTT API issue

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

ESP32 MQTT API issue

Postby idahowalker » Wed Sep 22, 2021 9:10 pm

Well the recent upgrades to the core have allowed me to get further along with using the MQTT API in the Arduino IDE but...

Here is the code

```
//#include <string.h>
#include <WiFi.h>
#include "certs.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
//#include "esp_netif.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.h"



#include "mqtt_client.h"
//////
WiFiClient wifiClient;
//esp_mqtt_event_t evtArgument ={};
esp_mqtt_client_handle_t hMQTT;
static EventGroupHandle_t wifi_event_group;
////
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
log_i( "Event dispatched from event loop base=%s, event_id=%d", base, event_id );
//esp_mqtt_event_handle_t event = event_data; //<<<gives the same error
esp_mqtt_client_handle_t client = event_data->client;
int msg_id;

} //static esp_err_t mqtt_event_handler_cb( esp_mqtt_event_handle_t event )
////
void setup()
{
wifi_event_group = xEventGroupCreate();
connectToWiFi();
esp_err_t err = 0;
// create client ID from mac address
byte mac[5];
WiFi.macAddress(mac); // get mac address
//log_i( "mac address % d. % d. % d. % d. % d", mac[0], mac[1], mac[2], mac[3], mac[4] );
String clientID = String(mac[0]) + String(mac[4]) ; // use mac address to create clientID
esp_mqtt_client_config_t mqtt_cfg = {};
mqtt_cfg.host = mqtt_server;
mqtt_cfg.port = mqtt_port;
mqtt_cfg.client_id = clientID.c_str();
mqtt_cfg.username = mqtt_username;
mqtt_cfg.password = mqtt_password;
esp_mqtt_client_handle_t hMQTT = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event( hMQTT, MQTT_EVENT_ANY, mqtt_event_handler, NULL );
err = esp_mqtt_client_start( hMQTT );
// //esp_mqtt_client_subscribe( hMQTT, topicOK, 0);
//
log_i( "%d", err );
WiFi.disconnect();
}

////
void connectToWiFi()
{

wifi_init();

}
////
void wifi_init()
{
//esp_netif_init();
}

////
static void log_error_if_nonzero(const char * message, int error_code)
{
if (error_code != 0)
{
log_i( "Last error %s: 0x%x", message, error_code);
}
}
////
void loop() {
delay(1);
// put your main code here, to run repeatedly:

}
```

Here is the error message:
```
C:\Users\KESOURLA\Documents\Arduino\ESP32_MQTT_API\ESP32_MQTT_API.ino: In function 'void mqtt_event_handler(void*, esp_event_base_t, int32_t, void*)':
ESP32_MQTT_API:30:47: error: 'void*' is not a pointer-to-object type
esp_mqtt_client_handle_t client = event_data->client;
^
Multiple libraries were found for "WiFi.h"
Used: C:\Users\KESOURLA\Documents\ArduinoData\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi
Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.51.0_x86__mdqgnx93n4wtt\libraries\WiFi
exit status 1
'void*' is not a pointer-to-object type

```

Any clue on how to get around the : 'void*' is not a pointer-to-object type would be grand!

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 MQTT API issue

Postby chegewara » Wed Sep 22, 2021 10:44 pm

idahowalker wrote:

Code: Select all

static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
	...
	esp_mqtt_client_handle_t client = event_data->client;  // event_data is type of (void* event_data), so you have to explicit cast it to some other type; but what type? you have to know API to figure it out
	...
}
https://developerinsider.co/type-casting-c-programming/

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: ESP32 MQTT API issue

Postby idahowalker » Thu Sep 23, 2021 2:55 pm

The ESPRESSIF example code
```
https://github.com/espressif/esp-idf/bl ... app_main.c
```

Does the thing

```
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
esp_mqtt_event_handle_t event = event_data;
```

The same way I am doing the thing.


But I keep getting this thing
```
C:\Users\KESOURLA\Documents\Arduino\ESP32_MQTT_API\ESP32_MQTT_API.ino: In function 'void mqtt_event_handler(void*, esp_event_base_t, int32_t, void*)':
ESP32_MQTT_API:28:35: error: invalid conversion from 'void*' to 'esp_mqtt_event_handle_t {aka esp_mqtt_event_t*}' [-fpermissive]
esp_mqtt_event_handle_t event = event_data;

```

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 MQTT API issue

Postby chegewara » Fri Sep 24, 2021 9:31 pm

Thats the difference between C and C++.

All you have to do is something like this:
https://github.com/espressif/esp-idf/bl ... ain.c#L176
https://github.com/espressif/esp-idf/bl ... ain.c#L189

Who is online

Users browsing this forum: No registered users and 34 guests