我有一个使用MQTT-SN协议的项目,我使用了以下的方法 https:/github.comS3lerarduino-mqtt-sn-客户端。 作为我的MQTT-SN客户端(我使用nodeMCU和Arduino ESP8266一样),我的MQTT-SN网关在我的笔记本电脑上使用paho.mqtt-SN.embedded-c,对于我的经纪人,我在我的笔记本电脑上使用Mosquitto。
这是MQTT-SN客户端的代码。
#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();
}
这是我的MQTT-SN网关的样子,我只能连接和订阅,不能发布或更改我的客户端ID。
我已经尝试用 https:/github.comS3lerarduino-mqtt-sn-clientissues3。 但我还是不能发布,只能订阅,甚至我的客户ID也不会改变,就像我在代码中声明的那样。
我不知道怎么了,谁能告诉我我的代码有什么问题?
编辑,我的代码是这样的。
这是我的Arduino IDE终端的样子,有人知道这是什么错误吗? 或者是什么问题让我不能发布?
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 ->
如果有人从这个问题可以帮助我,我想知道什么是问题,我怎么能解决它?客户端Socket错误<>,与Paho MQTT-SN网关和ESP8266 CLient断开连接。
我已经解读了这个错误,它是这样说的。
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
有人知道这个错误是什么意思吗?请告诉我,谢谢
谢谢大家,但我已经解决了我的问题,似乎有一个错误的代码在库中,你必须改变它,现在我做的很好做一些发布