2016-02-28 32 views
0

我嘗試了下面的代碼,將使用Arduino的值發佈到Mosquitto代理上。該代碼似乎是正確的,但只要我編譯/驗證,會引發以下錯誤。原因是什麼?我如何克服這個問題?通過PubSubClient發佈一個值

#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

錯誤消息告訴您傳遞給構造函數的回調函數的簽名不正確。

您有:

void callback(char topic, byte payload, unsigned int length){ 

它應該是:

void callback(char* topic, byte* payload, unsigned int length) { 

注意*topic和​​參數。