Arduino网络编程实战-Ethernet篇-MQTT客户端

MQTT客户端

Arduino Ethernet Shield V1 允许 Arduino 板连接到互联网。 它基于 Wiznet W5100ethernet 芯片(数据表)。 Wiznet W5100 提供支持 TCP 和 UDP 的网络 (IP) 堆栈。 它最多支持四个同时套接字连接。

本次实例将演示如何实现一个简单的MQTT客户端。

在这里插入图片描述

1、硬件准备

  • Arduino Mega 2560
  • Arduino Ethernet Shield
  • 路由器(推荐可以上网、开启DHCP)
  • 网线一条
  • 电脑一台

2、软件准备

3、代码实现

1)Arduino的MQTT客户端实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

#define USE\_STATIC 0
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
#if USE\_STATIC
IPAddress ip(192, 168, 0, 177);
#endif

void callback(char\* topic, byte\* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}

EthernetClient ethClient;
PubSubClient client(ethClient);
IPAddress serverIP(192, 168, 0, 100);

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("/public/TEST/window","hello world");
// ... and resubscribe
client.subscribe("/public/TEST/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {

Serial.begin(115200);
Serial.println("Ethernet MQTT Client Example");

#if USE\_STATIC
Ethernet.begin(mac,ip);
#else
Ethernet.begin(mac);
#endif

if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
while(true){
delay(1);
}
}

Serial.print("IP:");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask:");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway:");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server:");
Serial.println(Ethernet.dnsServerIP());

client.setServer(serverIP, 1886);
client.setCallback(callback);
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}

** 2)Nodejs的MQTT服务器实现**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var mosca = require('mosca');

var MqttServer = new mosca.Server({
port: 1886
});
MqttServer.on('clientConnected', function(client) {
//监听连接
console.log('client connected', client.id);
});
/\*\*
\* 监听题消息
\*\*/
MqttServer.on('published', function(packet, client) {
// 发布主题消息
var topic = packet.topic;
console.log(packet);
switch (topic) {
case 'test':
console.log('message-publish', packet.payload.toString());
break;
case 'other':
console.log('message-123', packet.payload.toString());
break;
}
});

MqttServer.on('ready', function() {
console.log('mqtt is running...');
});

4、运行结果

1)Arduino客户端

在这里插入图片描述

2)通信猫测试客户端

在这里插入图片描述

3)Nodejs服务器端

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/122762895