MQTT-SN Gateway - Not able to publish or changed my clientID, but able to connect and subscribe?

1

I have a project using MQTT-SN protocol, I use this https://github.com/S3ler/arduino-mqtt-sn-client as my MQTT-SN Client (I'm Using nodeMCU same as Arduino ESP8266) , my MQTT-SN Gateway use paho.mqtt-sn.embedded-c on my laptop and for my broker I use Mosquitto on my laptop too

this is the code for the MQTT-SN client

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#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();

}

and this my MQTT-SN Gateway look, I can only connect and subscribe, I can't Publish or change my clientID

Image of error message

I already try to fix with https://github.com/S3ler/arduino-mqtt-sn-client/issues/3 but still I can't publish and I can only subscribe even my clientid won't change like I declared on my code

I don't know what's wrong with it,Can someone tell me what's wrong with my code?

Edit:

This is what it look like in my Arduino IDE Terminal, Someone know what is the error? or what is the problem that keep me can't publish?

00:57:06.516 -> Connecting to WiFi
00:57:07.061 -> .....
00:57:09.071 -> WiFi connected
00:57:09.071 -> IP address: 
00:57:09.071 -> 192.168.1.192
00:57:09.071 -> Starting MqttSnClient -  ready!
00:57:09.071 -> MQTT-SN Gateway device_address: 192, 168, 1, 151, 7, 92
00:57:09.173 -> MQTT-SN Client connected.
00:57:09.173 -> 
00:57:09.173 -> Exception (28):
00:57:09.173 -> epc1=0x4000bf80 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
00:57:09.173 -> 
00:57:09.173 -> >>>stack>>>
00:57:09.173 -> 
00:57:09.173 -> ctx: cont
00:57:09.173 -> sp: 3ffffc80 end: 3fffffc0 offset: 0190
00:57:09.173 -> 3ffffe10:  00000011 3ffeeb84 00000000 fffffffe  
00:57:09.206 -> 3ffffe20:  00000011 00000001 00000001 3ffef57c  
00:57:09.206 -> 3ffffe30:  3ffef500 3ffef57c 3ffef4fc 40215182  
00:57:09.206 -> 3ffffe40:  3ffeec00 00000000 00000000 40215cef  
00:57:09.206 -> 3ffffe50:  00000016 3ffee478 3ffe8750 40210538  
00:57:09.206 -> 3ffffe60:  3ffeec00 0000005a 00000020 40100990  
00:57:09.206 -> 3ffffe70:  3ffeec00 3ffeec04 0000075c 3ffef5e0  
00:57:09.206 -> 3ffffe80:  00000810 00000102 00000000 3ffef500  
00:57:09.241 -> 3ffffe90:  0000075c 3ffef57c 3ffef4fc 4021058f  
00:57:09.241 -> 3ffffea0:  007a1200 59b425ea 00000000 40210628  
00:57:09.241 -> 3ffffeb0:  00000000 00000000 00000001 00000000  
00:57:09.241 -> 3ffffec0:  00000000 3ffef57c 00000000 40202b90  
00:57:09.241 -> 3ffffed0:  3ffee478 3ffee5c8 00000000 40203cae  
00:57:09.241 -> 3ffffee0:  00000013 3ffee4ba 3ffef4ac 00000008  
00:57:09.241 -> 3ffffef0:  00000000 3ffee4c0 3ffee4a8 40205a04  
00:57:09.275 -> 3fffff00:  00000008 3ffee4c0 3ffee4a8 40201771  
00:57:09.275 -> 3fffff10:  40205d30 9701a8c0 3ffe876f 40203405  
00:57:09.275 -> 3fffff20:  00000000 00000019 3ffee630 402034cc  
00:57:09.275 -> 3fffff30:  3ffee478 00002710 3ffee630 3ffe8750  
00:57:09.275 -> 3fffff40:  3ffee478 00000016 3ffee45c 40201c94  
00:57:09.275 -> 3fffff50:  0104040c 654b00b4 006e6976 00000000  
00:57:09.275 -> 3fffff60:  00000000 00000000 00000000 a8c00000  
00:57:09.275 -> 3fffff70:  5c079701 00000000 00000000 00000000  
00:57:09.309 -> 3fffff80:  3ffe8755 00000005 0000000c 00000001  
00:57:09.309 -> 3fffff90:  40201068 c001a8c0 feefeffe 3ffee75c  
00:57:09.309 -> 3fffffa0:  3fffdad0 00000000 3ffee71c 40203d90  
00:57:09.309 -> 3fffffb0:  feefeffe feefeffe 3ffe8500 40100c1d  
00:57:09.309 -> <<<stack<<<
00:57:09.309 -> 
00:57:09.309 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
00:57:09.343 -> 
00:57:09.343 -> load 0x4010f000, len 3456, room 16 
00:57:09.343 -> tail 0
00:57:09.343 -> chksum 0x84
00:57:09.343 -> csum 0x84
00:57:09.343 -> va5432625
00:57:09.343 -> ~ld
00:57:10.395 -> 

if anyone from this issue can help me , I want to know what is the problem and how can I fix it? Socket error on client <>, disconnecting with Paho MQTT-SN Gateway and ESP8266 CLient

I already decode the error and it say like this

Exception 28: LoadProhibited: A load referenced a page mapped with an 
attribute that does not permit loads
PC: 0x4000bf80
EXCVADDR: 0x00000000

Decoding stack results
0x4021547a: ip4_output_if_src at core/ipv4/ip4.c line 1590
0x40215fe3: ip_chksum_pseudo at core/inet_chksum.c line 395
0x40210830: udp_sendto_if_src at core/udp.c line 893
0x40100990: malloc(size_t) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266\umm_malloc\umm_malloc.cpp line 511
0x40210887: udp_sendto_if at core/udp.c line 692
0x40210920: udp_sendto at core/udp.c line 599
0x40202c28: WiFiUDP::endPacket() at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\ESP8266WiFi\src\WiFiUdp.cpp line 176
0x40204df5: uart_write(uart_t*, char const*, size_t) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266\uart.cpp line 509
0x40204df5: uart_write(uart_t*, char const*, size_t) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266\uart.cpp line 509
0x402033e4: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266/HardwareSerial.h line 164
0x402033e4: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266/HardwareSerial.h line 164
0x402033f0: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266/HardwareSerial.h line 165
0x402036c1: Print::write(char const*) at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266/Print.h line 62
0x40201d34: loop() at C:\Users\ASUS\OneDrive\Documents\Arduino\Test/Test.ino line 133
0x40201074: mqttsn_callback(char*, unsigned char*, unsigned short, bool) at C:\Users\ASUS\OneDrive\Documents\Arduino\Test/Test.ino line 35
0x40204058: loop_wrapper() at C:\Users\ASUS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\cores\esp8266\core_esp8266_main.cpp line 197

Is anyone know what the error mean? Please tell me and Thank You

mqtt
arduino-ide
arduino-esp8266
asked on Stack Overflow May 10, 2020 by Kevin Suharto • edited May 11, 2020 by Kevin Suharto

1 Answer

0

Thank you for all, but i already fix my problem it seem there is a wrong code in the library and you must change it, right now i'm doing fine doing some publish

answered on Stack Overflow May 12, 2020 by Kevin Suharto

User contributions licensed under CC BY-SA 3.0