PubSubClient.publish 尝试将 char 转换为 unsinged int 并抛出错误

发布于 2025-01-18 15:37:11 字数 1875 浏览 1 评论 0原文

首先,我距离一个有经验的程序员还很远,所以如果我在这里问初学者问题,请原谅我。

我尝试在 ESP8266 上使用 PubSubClient 发布有效负载时遇到问题。我正在使用 VS Code 和 Platformio。

在构建期间我收到以下错误。看起来 PubSubClient 正在尝试将有效负载从 char 转换为 unsinged int,即使有效负载在 PubSubClient 库的 API 中被定义为 const char。

src\main.cpp: In function 'void sendStatusViaMqtt(String)': src\main.cpp:389:39: error: invalid conversion from 'char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive] 389 |     mqttClient->publish(relay_Status, mqttMessageCharArray, msglength, false); |                                       ^~~~~~~~~~~~~~~~~~~~ |                                       | |                                       char* In file included from src\main.cpp:15: .pio\libdeps\esp12e\PubSubClient\src/PubSubClient.h:154:55: note:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)' 154 |    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);

这是我使用的代码片段。实际上,我竭尽全力将字符串转换为 PubSubClient 文档所需的字符。

void sendStatusViaMqtt()
{
  unsigned long now = millis();
  if (now - lastMqttMsg > 100000) //Making sure I only send messages after a certain time has elapsed
  {
    String mqttMessage = generateStatusString(); //Function generateStatusString()puts putting current values of variables of the running code on the ESP8266 into a String and returns it
    unsigned int msglength = mqttMessage.length();
    char mqttMessageCharArray[mqttMessage.length()]; 
    mqttMessage.toCharArray(mqttMessageCharArray, mqttMessage.length()); //Generating a CHAR Array out of the String
    lastMqttMsg = now; 
    Serial.print("Publish message: ");
    Serial.println(mqttMessage);
    mqttClient->publish(relay_Status, mqttMessageCharArray, msglength, false);
  }  
}

我很感谢任何提示!

First of all, I am a far cry from a being an experienced programmer, so please forgive me if I am asking beginners question here.

I encountered a problem trying to publish a payload using the PubSubClient on an ESP8266. I am using VS Code with Platformio.

During Build I receive the following error. It seems like the PubSubClient is trying to convert the payload from char to unsinged int, even though the payload is defined as const char in the API of the PubSubClient library.

src\main.cpp: In function 'void sendStatusViaMqtt(String)': src\main.cpp:389:39: error: invalid conversion from 'char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive] 389 |     mqttClient->publish(relay_Status, mqttMessageCharArray, msglength, false); |                                       ^~~~~~~~~~~~~~~~~~~~ |                                       | |                                       char* In file included from src\main.cpp:15: .pio\libdeps\esp12e\PubSubClient\src/PubSubClient.h:154:55: note:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)' 154 |    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);

This is the code snipped I used. Actually I went out of my way to convert a string into a char needed per PubSubClient documentation.

void sendStatusViaMqtt()
{
  unsigned long now = millis();
  if (now - lastMqttMsg > 100000) //Making sure I only send messages after a certain time has elapsed
  {
    String mqttMessage = generateStatusString(); //Function generateStatusString()puts putting current values of variables of the running code on the ESP8266 into a String and returns it
    unsigned int msglength = mqttMessage.length();
    char mqttMessageCharArray[mqttMessage.length()]; 
    mqttMessage.toCharArray(mqttMessageCharArray, mqttMessage.length()); //Generating a CHAR Array out of the String
    lastMqttMsg = now; 
    Serial.print("Publish message: ");
    Serial.println(mqttMessage);
    mqttClient->publish(relay_Status, mqttMessageCharArray, msglength, false);
  }  
}

I am thankful for any hint!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-01-25 15:37:11

您的mqttmessagechararray是类型char [](哪个衰减 char*传递到函数时),而您的mqttclient-> Publish()uint8_t* 代码>作为第二个参数。

最后一行来修复它。

mqttClient->publish(relay_Status, (uint8_t*)&mqttMessageCharArray, msglength, false);

可以通过使用mqttmessagechararray使用&mqttmessagechararray来替换 代码>。

uint8_t(又称未符号8位整数)基本上与几乎每个系统上的unsigned char。从定义上讲,它们俩都是大小的1个字节,因此在它们之间可以安全地施放而不会丢失任何数据。

Your mqttMessageCharArray is of type char[] (which decays to char* on passing to function), whereas your mqttClient->publish() takes a uint8_t* as its second argument.

It can simply be fixed by replacing the last line with :-

mqttClient->publish(relay_Status, (uint8_t*)&mqttMessageCharArray, msglength, false);

Here we take adress of mqttMessageCharArray using the & and then cast it to a uint8_t*.

uint8_t (aka. unsigned 8bit integer) is basically same as unsigned char on virtually every system. Both of them are by definition 1 byte in size so its safe to cast between them without any data loss.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文