eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
314 stars 179 forks source link

Is there something wrong with the gateway? #203

Closed kevin0598 closed 4 years ago

kevin0598 commented 4 years ago

Hello Sir, I want to ask something

I use this as my MQTT-SN Client https://github.com/S3ler/arduino-mqtt-sn-client and it can connect to the Gateway and even can subscribe too but the problem is I can publish a message or even change my Client ID and in my gateway show up this error

this is my code for MQTT-SN Client

[ #include

include

    #include <WiFiUdpSocket.h>
    #include <MqttSnClient.h>

const char* ssid     = "xxx";
const char* password = "xxx";

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

IPAddress gatewayIPAddress(192, 168, 1, 151);
uint16_t localUdpPort = 1884;

// #define gatewayHostAddress "arsmb.de"

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

const char* clientId = "Kevin";
char* subscribeTopicName = "test";
char* publishTopicName = "test";

int8_t qos = 0;

void mqttsn_callback(char *topic, uint8_t *payload, uint16_t length, bool retain) {
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.println(length);
}

void setup() {
Serial.begin(115200);
delay(1000);
pinMode (sensor,INPUT);
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.subscribe(subscribeTopicName , qos);
  }

    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;
}
 }

 mqttSnClient.loop();

}]

what is the problem? i don't know what's wrong image

ty4tw commented 4 years ago

show me your gateway.conf. Which protocol are you using udp or udp6?

kevin0598 commented 4 years ago

This is what my gateway.conf look like

[ #**

Copyright (c) 2016-2019, Tomoaki Yamaguchi

  #
  # All rights reserved. This program and the accompanying materials
  # are made available under the terms of the Eclipse Public License v1.0
  # and Eclipse Distribution License v1.0 which accompany this distribution.
  #
  # The Eclipse Public License is available at
  #    http://www.eclipse.org/legal/epl-v10.html
 # and the Eclipse Distribution License is available at
 #   http://www.eclipse.org/org/documents/edl-v10.php.
 #***************************************************************************
 #
 # config file of MQTT-SN Gateway
#

BrokerName=192.168.1.151
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=1884
 MulticastIP=225.1.1.1
 MulticastPortNo=1883
 MulticastTTL=1

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

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

 # LOG
 ShearedMemory=YES;](url)

I'm using udp6

ty4tw commented 4 years ago

Can I see a while log of the gateway?

ty4tw commented 4 years ago

In your case the gayeway sholud use udp. Because your client using udp I guess.