PubSubClient.publish 尝试将 char 转换为 unsinged int 并抛出错误
首先,我距离一个有经验的程序员还很远,所以如果我在这里问初学者问题,请原谅我。
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
mqttmessagechararray
是类型char []
(哪个衰减char*
传递到函数时),而您的mqttclient-> Publish()
请uint8_t*
代码>作为第二个参数。最后一行来修复它。
可以通过使用
mqttmessagechararray
使用&
的mqttmessagechararray
来替换 代码>。uint8_t
(又称未符号8位整数)基本上与几乎每个系统上的
unsigned char
。从定义上讲,它们俩都是大小的1个字节,因此在它们之间可以安全地施放而不会丢失任何数据。Your
mqttMessageCharArray
is of typechar[]
(which decays tochar*
on passing to function), whereas yourmqttClient->publish()
takes auint8_t*
as its second argument.It can simply be fixed by replacing the last line with :-
Here we take adress of
mqttMessageCharArray
using the&
and then cast it to auint8_t*
.uint8_t
(aka. unsigned 8bit integer) is basically same asunsigned 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.