我尝试使用以下代码将Arduino的值发布到Mosquitto代理上。代码似乎是正确的,但是一旦我编译/验证,就会抛出以下错误。原因是什么?如何克服这个问题?
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define CLIENTID "ArduinoSensor"
#define TOPICNAME "sensor/temperature"
#define POLLINTERVAL 120000
void callback(char topic, byte payload, unsigned int length){
//Do nothing as we are publishing ONLY.
}
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
byte server [] = { 85, 119, 83, 194 };
EthernetClient ethClient;
PubSubClient arduinoClient(server, 1883, callback, ethClient) ;
unsigned long boardTime = 0 ;
float sensedTemperature = 0;
char charTemperature [20];
void setup(void) {
Serial.begin(9600);
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}
//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ; //using the address assigned through DHCP
int connRC = arduinoClient.connect(CLIENTID) ;
if (!connRC) {
Serial.println(connRC) ;
Serial.println("Could not connect to MQTT Server");
Serial.println("Please reset the arduino to try again");
delay(100);
exit(-1);
}
else {
Serial.println("Connected to MQTT Server...");
}
}
void loop(void) {
boardTime = millis();
if ((boardTime % POLLINTERVAL) == 0) {
getTemp();
dtostrf(sensedTemperature,5,2,charTemperature) ;
arduinoClient.publish(TOPICNAME, charTemperature) ;
}
}
void getTemp() {
// Send the command to get temperatures
delay(100);
sensedTemperature = analogRead(2); //temperature sensor at analog pin 2 on Arduino.
delay(150);
}
错误打印:
publisher:18: error: invalid conversion from 'void (*)(char, byte, unsigned int) {aka void (*)(char, unsigned char, unsigned int)}' to 'void (*)(char*, uint8_t*, unsigned int) {aka void (*)(char*, unsigned char*, unsigned int)}' [-fpermissive]
PubSubClient arduinoClient(server, 1883, callback, ethClient) ;
C:\Users\Chetan\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:98:4: error: initializing argument 3 of 'PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)' [-fpermissive]
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
exit status 1
invalid conversion from 'void (*)(char, byte, unsigned int) {aka void (*)(char, unsigned char, unsigned int)}' to 'void (*)(char*, uint8_t*, unsigned int) {aka void (*)(char*, unsigned char*, unsigned int)}' [-fpermissive]
In file included from C:\Users\Chetan\Desktop\publisher\publisher.ino:4:0:
有什么建议吗?
答案 0 :(得分:0)
错误消息告诉您传递给构造函数的回调函数的签名不正确。
你有:
void callback(char topic, byte payload, unsigned int length){
应该是:
void callback(char* topic, byte* payload, unsigned int length) {
请注意*
和topic
参数的payload
。