我已按照以下链接中列出的步骤在我的笔记本电脑上安装并运行Mosquitto代理。
https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/
我使用命令行测试了发布和订阅事件,即mosquitto_pub和mosquitto_sub命令,它们工作正常!
我想发布到我的笔记本电脑上运行的这个代理(使用netstat-an命令监听端口1883),使用Arduino,它正在使用" PubSubClient"库。
对于Arduino Publish草图中服务器的IP地址,我已经给出了笔记本电脑本身的IP地址,它在连接到我的家庭网络时得到了。以下是发布商代码:
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define CLIENTID "Arsen"
const char topic[] = "tem";
#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} ;
IPAddress server(192, 168, 0, 10);
EthernetClient ethClient;
PubSubClient arduinoClient(server,1883, callback, ethClient) ;
unsigned long boardTime = 0 ;
float sensedTemperature = 0;
char charTemp [6];
int connRC = 0;
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
do{
int connRC = arduinoClient.connect(CLIENTID);
}while(connRC==1);
Serial.println("We are connected finally");
delay(5000);
Serial.println(arduinoClient.state());
}
void loop(void) {
boardTime = millis();
if ((boardTime % POLLINTERVAL) == 0) {
getTemp();
dtostrf(sensedTemperature,5,2,charTemp);
boolean rec = arduinoClient.publish(topic, charTemp);
Serial.println(rec);
Serial.println("Successfully published");
}
arduinoClient.loop();
}
void getTemp() {
// Send the command to get temperatures
delay(100);
sensedTemperature = analogRead(2); //temperature sensor at analog pin 2 on Arduino.
Serial.println(sensedTemperature);
delay(150);
}
订户代码为:
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <Dhcp.h>
#include <SPI.h>
#include <PubSubClient.h>
#define PIN 13
//MQTT Definition
#define CLIENTID "Aract"
#define TOPICNAME "tem"
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress server(192,168,0,5); // Mosquitto Server
int connRC = 0;
//Handle message from mosquitto and set LEDs
void callback(char* topic, byte* payload, unsigned int length) {
int i=0; char buffer [length];
for(i=0;i<length;i++) {
buffer [i] = char((payload[i]));
}
String payLoadData = buffer;
Serial.println(payLoadData);
setled(payLoadData);
}
//Our MQTT client
EthernetClient ethclient;
PubSubClient arduinoClient(server, 1883, callback, ethclient) ;
void setup() {
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}
//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ;
do{
connRC = arduinoClient.connect(CLIENTID) ;
delay(2000);
}while(connRC==1);
delay(3000);
Serial.println(arduinoClient.state());
if (connRC==1) {
boolean rec = arduinoClient.subscribe(TOPICNAME) ;
Serial.println(rec);
Serial.println("Successfully Subscribed");
} else {
Serial.println("connRC") ;
}
}
void loop()
{
arduinoClient.loop() ;
}
void setled(String s)
{
int temperature;
temperature = s.toInt();
Serial.println(temperature);
if (temperature >= 200){
digitalWrite(PIN, HIGH);
}
else{
digitalWrite(PIN, LOW);
}
}
以太网屏蔽在成功执行示例程序时运行良好。状态函数返回&#34;零&#34;这意味着电路板正在连接到网络。但是,publish命令返回一个布尔值&#34; rec&#34;这是假的。我想知道为什么这个过程一直都不成功?
verbose选项(-v)以及命令行上的Mosquitto会抛出未知错误,而不是显示已记录的详细信息。如何克服这个问题或我的技术究竟出了什么问题?
答案 0 :(得分:0)
我遇到了同样的问题,我得到了解决方案...... 只需转到PC中运行的服务并停止 mosquitto-broker ,现在打开cmd并将目录更改为mosuitto所在的位置文件被定位(在我的情况下是&#34; cd C:\ Program Files(x86)\ mosquitto &#34;)然后输入此命令 mosquitto -v ,现在刚刚离开那个窗口(如果你关闭那么它就不会工作)...... 它确实起作用,之所以它之前没有工作的原因是,当我们启动moquitto-broker服务然后它不是IPv4的开放端口时,这里是参考&#34; Mosquitto Error&#34;