A specific implementation of the applet Iot (MQTT version)

Foreword

Iot, the term should have been heard by many people, that is, after the rise of big data, Ai, etc., it also followed the arrival of the research and development trend, namely the Internet of Things. Traditionally speaking, it is intelligent and programmatic control of hardware facilities. The easiest thing for everyone to think of is the smart home, which can be used to remotely control the furniture settings through an App or remote control. What we are talking about today is the communication control between people and things, and the execution of human commands through Internet products, and the machine responds.

Competing products

Speaking of small program control hardware, what product should you think of immediately? A certain worship, a certain FO, vending machine, etc., their architecture will generally have a common point is the existence or similar cloud Iot function. For example, a cloud architecture like Azure uses IoT center, storage, virtual machine and other services.
image description

Think big

Then we can look forward to a larger architecture and implementation, or hope to be able to allocate and implement similar functions, but we still have to realize this function or the efficacy of small products step by step.

Target formulation: The user control end is a small program, and we use a simple vending machine (or express cabinet) on the hardware side. What we want to achieve is to specifically notify the vending machine to open and lock the corresponding cabinet through the small program command.

Then we need to conceive the corresponding simplified version of the architecture and the implementation of docking!

At present, most of the traditional and familiar front-end and back-end interaction methods should be mostly REST APIs. Then we are in the small program (think of it as a pure front-end display) and the backend (here is Iot in the following figure, we can use SpringBoot + Netty builds an Iot center) The communication between them uses the form of API, and the corresponding command interface is opened in the background, which is called by the applet. There may be another business here: then the server has notifications for the active communication of the applet? I won't go into details here. This can be regarded as a late consideration of product iteration. You can also consider the implementation method.

The applet in this article is a demo for MQTT debugging and testing, but there is no REST API connection. The next article will focus on the introduction.

What about the communication between the Iot center and the vending machine (express cabinet)? Students who may only work in software are generally less exposed. When I first started it, I was very ignorant, but roughly speaking, it was different communication protocols to achieve communication. Let ’s take the microcontroller as an example, we can know how many types it supports. Communication protocols, such as MQTT or TCP / IP, etc.

In this version, we will make the communication version of MQTT, then the flow chart is as follows.
image description

【image】

what to prepare?

Select the existing ready-made Github resources, this time choose paho.js to facilitate MQTT communication

This article will focus on the docking communication between the applet and MQTT, no API call

Applet: You need a certain basis for applet development, this time I chose a Github project from a friend as the demo of my applet

Backend: Use SpringBoot + Netty to make a small Iot center that connects MQTT and applets

MQTT hardware simulation end: I chose Paho, mqtt.java of Github authoritative Eclipse as the simulation

Quite simply, what you need to implement is the core backend side, which is also the Java side you are best at.

Simplify first

When your position has reached a certain level, what you may need to do is not to write the underlying detailed implementation code, but a template or idea and process for such implementation, so we will temporarily make a simple version of the small program to the hardware The communication process between.

Netty is very good, I won't say much here, because the definition of advanced by BAT manufacturers generally needs to understand the level of Netty's source code. Then you need to add Netty based on SpringBoot and customize the corresponding codec content. Because we use paho, there are some formats of its own. The general idea is to define the protocol specification. The communication part is actually relatively simple because a large part Netty has actually done it for you.

The socket communication of the applet is the beginning of ws, so our Iot center also needs a ws communication channel.

Here our communication address is: ws: // localhost: 8094 / mqtt

I didn't use wss. Let's say the same. Let's realize the simple one first and then think about the complicated one.

Then the applet correspondence is also connected to this address, the applet can start the connection under the mode, and the Java hardware emulation end of Mqtt is also the connection of the same address.

For MQTT, the applet needs to subscribe to a topic, and the Java simulation terminal is also sending information to this topic. If both ends send and receive normally and both pass our Iot center, then our small project is already successful La!

effect

image description
image descriptionimage descriptionimage description

GitHub

All the relevant source code of this article has been placed on the paho-mqtt project on the GitHub project

Address: paho-mqtt
project introduction: Based on Netty4 and SpringBoot, chat room WebSocket (text picture) plus API call Netty long link to execute sending messages (online number, user list), Iot Internet of Things-MQTT protocol, TCP / IP protocol microcontroller communication , Store chat data asynchronously


image description

Guess you like

Origin www.cnblogs.com/homehtml/p/12690977.html
Recommended