![](/img/trans.png)
[英]invalid conversion from 'int' to 'const char*' [-fpermissive]
[英]Esp-32 invalid conversion from 'const char*' to 'int' [-fpermissive]
您好,我对一些变量转换有疑问,问题是当尝试编译代码时会收到此错误消息。 为什么不能转换?
我把wtrtemp
作为 String 尝试将其更改为 int 和 float 和 const char 相同的问题。 Mqtt 只是从 slider 中打印出一个数字。 这是从节点红色发送的
从 'const char*' 到 'int' 的无效转换 [-fpermissive]
//MQTT incoming
#define mqttFloodInterval "greenHouse/floodInt"
#define mqttFloodDuration "greenHouse/floodDur"
#define mqttLightsOnDuration "greenHouse/lightsOnDur"
#define mqttLightsOffDuration "greenHouse/lightsOffDur"
//MQTT Setup End
int wtrtemp;
void topicsSubscribe(){
client.subscribe(mqttFloodInterval);
client.subscribe(mqttFloodDuration);
client.subscribe(mqttLightsOnDuration);
client.subscribe(mqttLightsOffDuration);
}
Serial.print("MQTT message received on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
messageTemp.remove(0);
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
Serial.println(messageTemp);
if (String(topic) == mqttFloodDuration) {
wtrtemp = mqttFloodDuration; // The problem is here
Serial.print("*** (Flood Interval Received)");
}
if(wtrtemp == 23){
digitalWrite(15, HIGH); // turn on pump 5 seconds
delay(5000);
}
else {
digitalWrite(15, LOW); // turn off pump 5 seconds
delay(5000);
}
}
我没有测试您的案例的环境,但我强烈建议您是否尝试将字符串转换为使用 atol() 的 int,因为 esp32 框架支持它。
int x = (int)atol("550");
所以在你的情况下: wtrtemp = (int)atol(mqttFloodDuration); // The problem is here
wtrtemp = (int)atol(mqttFloodDuration); // The problem is here
如果这不能解决您的情况(不能 100% 记住 atol 使用的参数是否采用 const char* 或 char*),因此如果它坚持使用 char*,请尝试改用: wtrtemp = (int)atol((char*)mqttFloodDuration); // The problem is here
wtrtemp = (int)atol((char*)mqttFloodDuration); // The problem is here
如果您希望 go 在使用字符串 class 的危险但容易的道路上,那么您可以通过以下方式轻松设置字符串
String xx = "hello world";
int xx_int = xx.toInt();
但是在底层,function 也执行 atol() function 上面提到的所以请记住这一点,如果您想提高 memory 板载 esp 分配和使用的效率。
您的mqttFloodDuration
是一个扩展为字符串文字"greenHouse/floodDur"
的宏。 错误消息正确地告诉您,这不是与int
类型的变量正确匹配的类型,例如您的wtrtemp
。
Moreover, you do seem to expect wrtemp
to take a genuine integer value because you later compare it to the integer constant 23
, but it is unclear from the code presented how the string "greenHouse/floodDur"
corresponds to an integer. 可能有某种查找 function 可以用来获取相应的值,但据我所知,这将特定于您的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.