Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino Knollerary PubSubClient将发布消息,但可以';我不接受他们_Arduino_Mqtt - Fatal编程技术网

Arduino Knollerary PubSubClient将发布消息,但可以';我不接受他们

Arduino Knollerary PubSubClient将发布消息,但可以';我不接受他们,arduino,mqtt,Arduino,Mqtt,我正在使用Knollery PubSubClient连接到我的MQTT服务器。我已经能够成功地进行身份验证和连接后,没有太多的工作。我甚至可以将消息发布到主题。但是,我遇到的问题是,我可以订阅主题,并且不会收到任何错误,但是当我发布到该主题时(从Mac上的Mosquetto),不会调用回调,并且订阅主题的消息似乎不会收到。我尝试过同时运行同一主题的mosquitto订阅,但收到了已发布的消息。不确定我的回调代码中是否有问题,或者这里发生了什么。任何帮助都将不胜感激。Arduino代码如下: /*

我正在使用Knollery PubSubClient连接到我的MQTT服务器。我已经能够成功地进行身份验证和连接后,没有太多的工作。我甚至可以将消息发布到主题。但是,我遇到的问题是,我可以订阅主题,并且不会收到任何错误,但是当我发布到该主题时(从Mac上的Mosquetto),不会调用回调,并且订阅主题的消息似乎不会收到。我尝试过同时运行同一主题的mosquitto订阅,但收到了已发布的消息。不确定我的回调代码中是否有问题,或者这里发生了什么。任何帮助都将不胜感激。Arduino代码如下:

/*
 Basic MQTT example 

  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 10, 2, 63, 123 };
byte ip[]     = { 192, 168, 1, 10 };


void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println(topic);
  //convert byte to char
  payload[length] = '\0';
  String strPayload = String((char*)payload);

  Serial.println(strPayload);
  //int valoc = strPayload.lastIndexOf(',');
  //String val = strPayload.substring(valoc+1);
  //Serial.println(val);


}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
  Serial.begin(19200);
  Serial.println("==STARTING==");

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }

  //delay(500);
  boolean con = client.connect("1", "3snzzon5dyade:abc", "OBSCURED_FOR_SEC");
  while(con != 1){
    Serial.println("no con-while");
     con = client.connect("1", "3snzzon5dyade:abc", "OBSCURED_FOR_SEC");
  }
  //Serial.println(con);
  if(con){
    Serial.println("got con");
    client.publish("testq","hello world");
    client.subscribe("testq");
  }else Serial.println("no con");

}

void loop()
{
  client.loop();
}
/*
基本MQTT示例
-连接到MQTT服务器
-将“hello world”发布到主题“outTopic”
-订阅主题“inTopic”
*/
#包括
#包括
#包括
//使用适合您的网络的值更新这些值。
字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
字节服务器[]={10,2,63,123};
字节ip[]={192,168,1,10};
无效回调(字符*主题,字节*有效负载,无符号整数长度){
序列号println(主题);
//将字节转换为字符
有效载荷[长度]='\0';
String strPayload=String((char*)有效载荷);
Serial.println(strPayload);
//int valoc=strPayload.lastIndexOf(',');
//字符串val=strPayload.substring(valoc+1);
//序列号println(val);
}
EthernetClient;
PubSubClient客户端(服务器,1883,回调,ethClient);
无效设置()
{
连载《开始》(19200);
Serial.println(“==开始==”);
//启动以太网连接:
if(以太网开始(mac)==0){
Serial.println(“未能使用DHCP配置以太网”);
//继续下去没有意义,所以永远不要做任何事:
//尝试使用IP地址而不是DHCP连接:
以太网开始(mac,ip);
}
//给以太网屏蔽一秒钟进行初始化:
延迟(1000);
Serial.println(“连接…”);
对于(字节thisByte=0;thisByte<4;thisByte++){
//打印IP地址每个字节的值:
Serial.print(Ethernet.localIP()[thisByte],DEC);
连续打印(“.”);
}
//延迟(500);
布尔值con=客户端连接(“1”、“3NZZON5Dyade:abc”、“模糊表示秒”);
while(con!=1){
序列号。打印号(“无冲突”);
con=客户端连接(“1”、“3NZZON5Dyade:abc”、“模糊表示秒”);
}
//序列号println(con);
如果(con){
Serial.println(“got con”);
client.publish(“testq”、“hello world”);
客户。认购(“testq”);
}else Serial.println(“无合同”);
}
void循环()
{
client.loop();
}
就像我说的,我可以看到莫斯奎托的一切都正常运转。我甚至尝试过匹配客户ID,但运气不佳。如果您有任何帮助或想法,我们将不胜感激。

您的订阅主题“testq”必须以如下方式排列:

char testq[] = {'t', 'e', 's', 't', 'q', '\0'};
请确保使用“\0”完成数组

然后您可以订阅:

client.subscribe(testq);

我看不出你的代码有什么明显的错误。很自然,我把我所有的arduino都放在工作上了,否则我会测试它。如果你到星期一还没有解决,我就试一试。谢谢你的回复。这可能是代理与您的库不兼容的原因吗?同样的代码也适用于其他代理。您使用的是哪种代理实现?不确定您要问什么。但它是一个类似于mosquitto服务器的定制代理实现。这就是问题所在吗?如果代码对mosquitto有效,但对您的custom broker实现无效,那么我想更仔细地了解一下您的实现在做什么。我相信你在实现你的经纪人方面做得很好,但是从我坐的位置来看,这是一个未知的问题。