Based on node JS build a simple MQTT server, and the mobile phone connects to the computer through the hotspot to realize the MQTT server test
Server construction
First, build the node JS environment. Configure the MQTT server. There are many online tutorials. You can refer to this blog for configuration Node.js configuration.
Server mqtt js:
const mosca = require("mosca"); const MqttServer = new mosca.Server({ port: 1883 }); MqttServer.on("clientConnected", function(client) { //Callback when there is a client connection console.log("client connected", client.id); }); /** * Listen for MQTT topic messages * When the client has a connection to publish a topic message **/ MqttServer.on("published", function(packet, client) { var topic = packet.topic; switch (topic) { case "test": // console.log('message-publish', packet.payload.toString()); //MQTT can forward topic messages to other topics //MqttServer.publish({ topic: 'other', payload: 'sssss' }); break; case "other": console.log("message-123", packet.payload.toString()); break; } }); MqttServer.on("ready", function() { //Callback when the service is turned on console.log("mqtt is running..."); });
Client sender publish js
const mqtt = require("mqtt"); const client = mqtt.connect("mqtt://192.168. 137.1: 1883 "); / / connect to mqtt server setInterval(function() { const value = Math.ceil(Math.random() * 40); client.publish("test", value.toString(), { qos: 0, retain: true }); }, 3000);
Client subscriber subscribe js
const mqtt = require("mqtt"); // const mqtt = require('./node_modules/mqtt/dist/mqtt.min.js') const client = mqtt.connect("mqtt://192.168. 137.1: 1883 "); / / specify the server address and port client.on("connect", function() { console.log("Server connection succeeded"); // connected = client.connected client.subscribe("test", { qos: 0}); //Subscribe to messages with the subject test }); client.on("message", function(top, message) { console.log("current topic: ", top); console.log("Message received:", message.toString()); });
test
First, enter node mqtt in the terminal to open the server
Enter node subscribe to open the receiving client
Enter node publish to open the sending client
Then you can see
The sending client sends data to the test topic, and the receiving client subscribes to the test topic and can receive data. Prove that the MQTT server is set up.
MQTT test of mobile computer
The mobile phone sends data to the client by connecting to the computer hotspot, and the computer receives the client's subscription topic to obtain data.
First, write the mobile APP so that it can send data using MQTT protocol. Here, a simple APP is developed based on APP inventor for testing
Logic design:
Then configure the computer, open the firewall settings, and open port 1883.
Then turn on the computer hotspot and connect the mobile phone to the computer hotspot
Then check the ip address of your computer
Since my IP address is the above one, this is also the IP address of the connected server when using the above client configuration. You should pay attention to it when configuring yourself
After that, connect to the server, open the receiving client on the computer side, click the send data button on the mobile phone side, and observe the changes of the receiving client on the computer
You can see that the computer side subscribes to the data sent by the mobile client to the server. The test was successful.
In the whole process, I think the most important thing is to determine my own LAN connection, because this is a server built by myself. Only when the mobile phone and the computer are in the same LAN can the mobile phone be connected to the server of the computer, otherwise it can not be connected. This is my most intuitive feeling in the process of this test.