Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从自定义类中使用PubSubClient_C++_Arduino_Mqtt - Fatal编程技术网

C++ 从自定义类中使用PubSubClient

C++ 从自定义类中使用PubSubClient,c++,arduino,mqtt,C++,Arduino,Mqtt,我正在构建一个库,可以在多个设备上重复使用,这样就不用在代码中反复设置相同的函数 我在尝试发布消息时遇到问题,希望您能提供帮助 代码的目标是启用BLE和WiFi的ESP32(它是的一个端口,消息负载是使用ArduinoJSON 6.x创建的(最初使用的是ArduinoJSON 5.x,但这不是问题所在,因为在尝试打印纯文本时,代码失败并出现相同的错误) 代码通过Platformio.org管理,而不是通过Arduino IDE管理,我的Platformio.ini文件如下所示: [env:esp

我正在构建一个库,可以在多个设备上重复使用,这样就不用在代码中反复设置相同的函数

我在尝试发布消息时遇到问题,希望您能提供帮助

代码的目标是启用BLE和WiFi的ESP32(它是的一个端口,消息负载是使用ArduinoJSON 6.x创建的(最初使用的是ArduinoJSON 5.x,但这不是问题所在,因为在尝试打印纯文本时,代码失败并出现相同的错误)

代码通过Platformio.org管理,而不是通过Arduino IDE管理,我的Platformio.ini文件如下所示:

[env:esp32dev]
platform = espressif32
board = ttgo-t-beam
framework = arduino
lib_extra_dirs = 
    ../common_libraries
build_flags = !../bin/build_flags.sh
board_build.partitions = min_spiffs.csv
lib_deps = 
    nkolban/ESP32 BLE Arduino@^1.0.1
    bblanchon/ArduinoJson@^6.17.2
build\u flags.sh
脚本只是为WiFi和MQTT客户端设置环境变量,稍后您就会看到

库中的代码如下所示,我看到的问题是,虽然来自类设置的“注册消息”总是被发送,但来自心率监视器的数据偶尔被发送,并且总是引起上师冥想

令人沮丧的是,这只会在来自类外部的MQTT发布上失败,无论客户机在何处初始化,订阅消息都会通过

代码 我的“图书馆” 这解码为

PC: 0x2f736563
EXCVADDR: 0x2f736560

Decoding stack results
0x4008df8d: xRingbufferCreate at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_ringbuf/ringbuf.c line 704
0x4008e97f: xQueueGenericSendFromISR at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c line 1207
0x40086f41: spi_flash_mmap_pages at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_mmap.c line 201
0x400df6ce: esp_fill_random at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/hw_random.c line 61
0x400922dd: verify_allocated_region at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c line 109
0x40090af9: vTaskPlaceOnEventListRestricted at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/include/freertos/portable.h line 208
根据文件显示,我错误地调用了publish命令:

InstrFetchProhibited

This CPU exception indicates that CPU could not load an instruction because the the address of the instruction did not belong to a valid region in instruction RAM or ROM.

Usually this means an attempt to call a function pointer, which does not point to valid code. PC (Program Counter) register can be used as an indicator: it will be zero or will contain garbage value (not 0x4xxxxxxx).
(摘自页面)

我确信这是一个明显的问题,这只是我对如何传递PubSubClient的误解,但我完全不知道如何做

#include <MYMQTT32.h>
#define ST(A) #A
#define STR(A) ST(A)

// Setup the WiFi and MQTT Client based on the build flags
MYMQTT mymqtt(STR(DEVICE_NAME), device_type, STR(WIFI_SSID), STR(WIFI_PASS), STR(MQTT_SERVER), 1883);
PubSubClient mqctrl;

...
void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    hbat.wifi_setup();
    mqctrl = hbat.mqtt_setup(device_type);
    mqctrl.setCallback(control);

...

}

void loop() {
...

  mqctrl.loop()
}
//--------------------------------------------------------------------------------------------
  // Send HRM stats to MQTT
  //--------------------------------------------------------------------------------------------
  void sendHRMData() {
    Serial.println("Message received from chest strap, sending to MQTT");
    DynamicJsonDocument  dataJson(20);
  
    dataJson[F("HRM")]             = hrm.HRM;
    char buffer[20];
    serializeJson(dataJson, buffer);
~   Serial.print("Buffer Content: ");
~   Serial.println(buffer);
     mqctrl.publish("/devices/status/heartrate", buffer); // <- JSON BUFFER CAUSES GURU MEDITIATION
~   // mqctrl.publish("/devices/status/heartrate", "test message"); <- STATIC MESSAGE CAUSES THE SAME ISSUE
  }
Guru Meditation Error: Core  0 panic'ed (InstrFetchProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x2f736563  PS      : 0x00060031  A0      : 0x8001a060  A1      : 0x3ffbe290  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000001  
A6      : 0x00000000  A7      : 0x3ffb8360  A8      : 0x8008ebac  A9      : 0x3ffbc88c  
A10     : 0x00000002  A11     : 0x3ffbc230  A12     : 0x800922e0  A13     : 0x3ffbc230  
A14     : 0x00000008  A15     : 0x00000000  SAR     : 0x0000001d  EXCCAUSE: 0x00000014  
EXCVADDR: 0x2f736560  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  
Core 0 was running in ISR context:
EPC1    : 0x2f736563  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x00000000

Backtrace: 0x2f736563:0x3ffbe290 0x4001a05d:0x3ffbe2b0 0x4008cd3e:0x3ffbe2e0 0x4008a591:0x3ffbe320 0x4008df8d:0x3ffbe340 0x4008e97f:0x3ffbe360 0x40086f41:0x3ffbe380 0x401e7d03:0x3ffbc210 0x400df6ce:0x3ffbc230 0x400922dd:0x3ffbc250 0x40090af9:0x3ffbc270
PC: 0x2f736563
EXCVADDR: 0x2f736560

Decoding stack results
0x4008df8d: xRingbufferCreate at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_ringbuf/ringbuf.c line 704
0x4008e97f: xQueueGenericSendFromISR at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c line 1207
0x40086f41: spi_flash_mmap_pages at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_mmap.c line 201
0x400df6ce: esp_fill_random at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/hw_random.c line 61
0x400922dd: verify_allocated_region at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c line 109
0x40090af9: vTaskPlaceOnEventListRestricted at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/include/freertos/portable.h line 208
InstrFetchProhibited

This CPU exception indicates that CPU could not load an instruction because the the address of the instruction did not belong to a valid region in instruction RAM or ROM.

Usually this means an attempt to call a function pointer, which does not point to valid code. PC (Program Counter) register can be used as an indicator: it will be zero or will contain garbage value (not 0x4xxxxxxx).