客户端<>上的套接字错误,与Paho MQTT-SN网关和ESP8266 CLient断开连接

时间:2019-11-25 20:11:42

标签: sockets mqtt iot esp8266

我正在尝试测试MQTT-SN。

我正在为客户端使用Mosquitto Broker,Paho MQTT-SN网关和此库(https://github.com/S3ler/arduino-mqtt-sn-client)。

我正在使用esp8266作为客户端。

有了这个客户端,我可以连接,订阅,从订阅的主题中接收信息,但不能发布到主题中

memset(buffer, 0x0, buffer_length);
mqttSnClient.publish(buffer, publishTopicName , qos);

每次我尝试与此客户发布时,Mosquitto都会给我

Socket error on client <clientid>, disconnecting

我的客户与经纪人断开连接。

有任何线索吗?

EDIT1

客户代码

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "WiFiUdpSocket.h"
#include "MqttSnClient.h"
#include <NTPClient.h>

const char* ssid     = "example";
const char* password = "example1";

long utcOffsetInSeconds = -10800;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

#define buffer_length 10
char buffer[buffer_length + 1];
uint16_t buffer_pos = 0;

IPAddress gatewayIPAddress(192, 168, 0, 106);
uint16_t localUdpPort = 10000;


WiFiUDP udp;
WiFiUdpSocket wiFiUdpSocket(udp, localUdpPort);
MqttSnClient<WiFiUdpSocket> mqttSnClient(wiFiUdpSocket);

const char* clientId = "hamilton12";
char* subscribeTopicName = "ESP8266/123";
char* publishTopicName = "ESP8266/123";

int8_t qos = 1;

void mqttsn_callback(char *topic, uint8_t *payload, uint16_t length, bool retain) {
  timeClient.update();
  Serial.print("Received - Topic: ");
  Serial.print(topic);
  Serial.print(" Payload: ");
  for (uint16_t i = 0; i < length; i++) {
    char c =  (char) * (payload + i);
    Serial.print(c);
  }
  Serial.print(" Lenght: ");
  Serial.print(length);

  Serial.print(" Received Timestamp milliseconds: ");
  Serial.print(timeClient.getHours());
  Serial.print(":");
  Serial.print(timeClient.getMinutes());
  Serial.print(":");
  Serial.println(timeClient.getSeconds());
}

void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("Starting MqttSnClient - ");
  mqttSnClient.setCallback(mqttsn_callback);
  if  (!mqttSnClient.begin()) {
    Serial.print("Could not initialize MQTT-SN Client ");
    while (true) {
      Serial.println(".");
      delay(1000);
    }
  }
  Serial.println(" ready!");
}

void convertIPAddressAndPortToDeviceAddress(IPAddress& source, uint16_t port, device_address& target) {
  // IPAdress 0 - 3 bytes
  target.bytes[0] = source[0];
  target.bytes[1] = source[1];
  target.bytes[2] = source[2];
  target.bytes[3] = source[3];
  // Port 4 - 5 bytes
  target.bytes[4] = port >> 8;
  target.bytes[5] = (uint8_t) port ;
}


void loop() {
  if (!mqttSnClient.is_mqttsn_connected()) {
#if defined(gatewayHostAddress)
    IPAddress gatewayIPAddress;
    if (!WiFi.hostByName(gatewayHostAddress, gatewayIPAddress, 20000)) {
      Serial.println("Could not lookup MQTT-SN Gateway.");
      return;
    }
#endif
    device_address gateway_device_address;
    convertIPAddressAndPortToDeviceAddress(gatewayIPAddress, localUdpPort, gateway_device_address);
    Serial.print("MQTT-SN Gateway device_address: ");
    printDeviceAddress(&gateway_device_address);


    if (!mqttSnClient.connect(&gateway_device_address, clientId, 180) ) {
      Serial.println("Could not connect MQTT-SN Client.");
      delay(1000);
      return;
    }
    Serial.println("MQTT-SN Client connected.");
    //mqttSnClient.set_mqttsn_connected();
    if (!mqttSnClient.subscribe(subscribeTopicName, qos)){
      Serial.println("Cant subscribe");
    }
    Serial.println("Subscribed");
  }

  //It never enters this IF
  if (Serial.available() > 0) {
    buffer[buffer_pos++] = Serial.read();
    if (buffer[buffer_pos - 1] == '\n') {
      // only qos -1, 0, 1 are supported
      if (!mqttSnClient.publish(buffer, publishTopicName , qos)) {
        Serial.println("Could not publish");
      }
      Serial.println("Published");
      memset(buffer, 0x0, buffer_length);
      buffer_pos = 0;
    }
  }

  //Uncommenting this line will give socket error
  //mqttSnClient.publish(buffer, publishTopicName , qos);


  mqttSnClient.loop();

}

etc / mosquitto / mosquitto.conf

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

#include_dir /etc/mosquitto/conf.d

connection_messages true
log_timestamp true
log_dest stderr
log_type error
log_type warning
log_type debug

allow_anonymous true

gateway.conf

BrokerName=192.168.0.106
BrokerPortNo=1883
BrokerSecurePortNo=8883

#
# When AggregatingGateway=YES or ClientAuthentication=YES,
# All clients must be specified by the ClientList File  
#

ClientAuthentication=NO
AggregatingGateway=NO
QoS-1=NO
Forwarder=NO

#ClientsList=/path/to/your_clients.conf

PredefinedTopic=NO
#PredefinedTopicList=/path/to/your_predefinedTopic.conf

#RootCAfile=/etc/ssl/certs/ca-certificates.crt
#RootCApath=/etc/ssl/certs/
#CertsFile=/path/to/certKey.pem
#PrivateKey=/path/to/privateKey.pem

GatewayID=1
GatewayName=PahoGateway-01
KeepAlive=900
#LoginID=your_ID
#Password=your_Password


# UDP
GatewayPortNo=10000
MulticastIP=225.1.1.1
MulticastPortNo=1884

# UDP6
GatewayUDP6Bind=FFFF:FFFE::1 
GatewayUDP6Port=10000
GatewayUDP6Broadcast=FF02::1
GatewayUDP6If=wpan0

# XBee
Baudrate=38400
SerialDevice=/dev/ttyUSB0
ApiMode=2

# LOG
ShearedMemory=NO;

EDIT2

终端运行的蚊子

hamilton@hamilton-note:~$ mosquitto
1574806892: mosquitto version 1.4.15 (build date Tue, 18 Jun 2019 11:42:22 -0300) starting
1574806892: Using default config.
1574806892: Opening ipv4 listen socket on port 1883.
1574806892: Opening ipv6 listen socket on port 1883.
1574806900: New connection from 192.168.0.106 on port 1883.
1574806900: New client connected from 192.168.0.106 as hamilton123 (c1, k46080).
1574806900: Socket error on client hamilton123, disconnecting.
^C1574806911: mosquitto version 1.4.15 terminating

运行Paho Gateway的终端

hamilton@hamilton-note:~/Downloads$ ./MQTT-SNGateway 

 ***************************************************************************
 * MQTT-SN Transparent Gateway
 * Part of Project Paho in Eclipse
 * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)
 *
 * Author : Tomoaki YAMAGUCHI
 * Version: 1.3.1
 ***************************************************************************

20191126 192134.372 PahoGateway-01 has been started.

 ConfigFile: ./gateway.conf
 PreDefFile: ./predefinedTopic.conf
 SensorN/W:  UDP Multicast 225.1.1.1:1884 Gateway Port 10000
 Broker:     192.168.0.106 : 1883, 8883
 RootCApath: (null)
 RootCAfile: (null)
 CertKey:    (null)
 PrivateKey: (null)



20191126 192140.660   CONNECT           <---  hamilton123                         12 04 04 01 B4 00 68 61 6D 69 6C 74 6F 6E 31 32 33 00
20191126 192140.660   CONNECT           ===>  hamilton123                         10 17 00 04 4D 51 54 54 04 02 B4 00 00 0B 68 61 6D 69 6C 74 6F 6E 31 32 33
20191126 192140.874   CONNACK           <===  hamilton123                         20 02 00 00
20191126 192140.874   CONNACK           --->  hamilton123                         03 05 00

20191126 192140.879   SUBSCRIBE   0200  <---  hamilton123                         11 12 20 02 00 45 53 50 38 32 36 36 2F 31 32 33 00
20191126 192140.879   SUBSCRIBE   0200  ===>  hamilton123                         82 10 02 00 00 0B 45 53 50 38 32 36 36 2F 31 32 33 01
20191126 192140.879   SUBACK      0200  <===  hamilton123                         90 03 02 00 01
20191126 192140.879   SUBACK      0200  --->  hamilton123                         08 13 20 00 01 02 00 00

20191126 192140.883   PUBLISH     0300  <---  hamilton123                         08 0C 22 00 01 03 00 00
20191126 192140.884   PUBLISH     0300  ===>  hamilton123                         32 07 00 02 00 01 03 00 00
^C20191126 192149.215 BrokerSendTask   stopped.
20191126 192149.215 PacketHandleTask stopped.
20191126 192149.215 ClientSendTask   stopped.
20191126 192149.386 BrokerRecvTask   stopped.
20191126 192150.158 ClientRecvTask   stopped.

20191126 192150.215 MQTT-SN Gateway  stoped

1 个答案:

答案 0 :(得分:1)

感谢您对塞尔顿(DaltonCézane)的帮助。

但是我在客户端库的一个未解决问题中发现了问题:

  

您的示例WiFiUdpMqttSnClient程序遇到了麻烦   它不会成功发布测试消息。我在用着   paho-mqtt-sn网关。

     

我在黑暗中猛扑一下,但是我认为这是因为   发布标志TopicIdType设置为2的消息。   应该为零(正常),因为它没有预先注册,也不是   简短的话题。

     

在文件MqttSnClient.h第216行中,对send_publish的调用已   short_topic设置为true。但这还不是全部。在文件mqttsn_messages.h中   如果short_topic标志为false,则在第215行附近将标志设置为   预定义的。我删除了后面的“ else”子句,因此设置了标志   设为零,我现在可以成功发布。

     

我怀疑我的黑客攻击不是完整的解决方案,但希望它能对您有所帮助   解决此问题。

此评论由@nottledim表示,非常感谢!

现在我可以使用esp8266进行发布了。

如果有人遇到这个问题,就离开这里。

链接到问题:https://github.com/S3ler/arduino-mqtt-sn-client/issues/3