Disclosure of Invention
The invention provides a high-reliability gateway device based on MODBUS-Mqtt protocol, which is mainly suitable for an anti-interference and high-safety data transfer gateway of a sludge drying system and aims to solve the problems that most of the traditional industrial equipment and low-power-consumption wireless sensor equipment can only be communicated with the local and can not be accessed to the Internet, and meanwhile, the equipment is easily interfered by the outside world and is low in safety.
In order to achieve the aim, the invention discloses a high-reliability gateway device based on MODBUS-Mqtt protocol, which mainly comprises a main control single chip microcomputer for realizing ModBUS-Mqtt protocol conversion function and controlling communication transmission function, a network communication module and an RJ-45 interface for communicating with a network, a wifi module for communicating with a local area network, an RS485 interface for transmitting data butt joint with a data concentrator, an optoelectronic isolation module for inhibiting sharp pulse and various noise interferences, and a power supply module for supplying power to the main control single chip microcomputer; the photoelectric isolation module, the wifi module, the network communication module, the RS485 interface and the power supply module are all connected with and controlled by the master control single chip microcomputer in a coordinated manner; and after receiving the data butted by the RS485 interface, the master control single chip microcomputer carries out data frame processing, data encryption and MQTT/ModBus protocol conversion, and then carries out remote transmission and application processing on the data packet.
Preferably, the master control singlechip is also electrically connected with a power isolation module for voltage stabilization and noise reduction.
The invention has the following advantages and effects: 1. the system can work normally in a noisy environment for treating sludge, and has strong anti-interference capability; 2. the intelligence degree is high, and the safety is high; 3. simple operation, easy deployment and installation.
Detailed Description
The present invention will be described in further detail with reference to examples and drawings, but the present invention is not limited thereto.
Referring to fig. 1 to 5, the high-reliability gateway device based on the MODBUS-to-Mqtt protocol in the embodiment of the present invention mainly includes a main control single chip 1 for implementing the MODBUS-to-Mqtt protocol function and controlling the communication transmission function, a network communication module and an RJ-45 interface 2 for communicating with a network, a wifi module 3 for communicating with a local area network, an RS485 interface 4 for interfacing with a data concentrator for transmitting data, an optoelectronic isolation module 5 for suppressing sharp pulses and various noise interferences, a power isolation module 6 for stabilizing voltage and reducing noise, and a power module for providing power to the main control single chip; the photoelectric isolation module 5, the wifi module 3, the network communication module, the RS485 interface 4, the power supply module and the power supply isolation module 6 are all connected with and controlled by the master control single chip microcomputer in a coordinated manner; the main control single chip microcomputer 1 firstly carries out data frame processing, data encryption and MQTT/ModBus protocol conversion after receiving data butted by the RS485 interface, and then carries out remote transmission and application processing on the data packet.
Referring to fig. 1 to 5, the optoelectronic isolation module LTV-816S has a function of cutting off an interference signal from entering a subsequent circuit, and is used for effectively suppressing sharp pulses and various noise interferences; the wifi module ESP8266 is used for wireless transmission of Mqtt protocol or TCP/UDP protocol and the like, and can also be used as a local area network server to provide hot spot connection; the network communication module ENC28J60 supports RJ-45 to be inserted into a network cable to access a public network for data transmission; the 4G module EC20 supports various protocols including Mqtt protocol, supports LTE, UMTS and GSM/GPRS networks, has the maximum uplink rate of 50Mbps and the maximum downlink rate of 100Mbps, and has the function of transmitting a large amount of data; and the RS485 interface module is used for receiving data from the data concentrator. The power module is provided with a 220V mains supply input circuit which comprises a safety capacitor, an inductive filter and other devices, so that EMI is reduced, and pure electric power is supplied to the system as far as possible. The 5V conversion circuit and the 3.3V conversion circuit convert the filtered alternating current 220V into 5V and 3.3V and provide the 5V and 3.3V to each component. The master control single chip microcomputer is responsible for overall normal operation of the whole system, is used for converting the ModBus protocol into the Mqtt protocol and transmitting data to the cloud end through the communication module.
As shown in fig. 1, the power supply module and the power isolation module of the invention provide stable working power supply for the main control single chip and other modules. The RS485 interface, the photoelectric isolation module and the master control single chip microcomputer complete the collection of original data, and in addition, the master control single chip microcomputer converts data transmitted by the ModBus protocol into data of the Mqtt protocol. The wifi module or the 4G module uploads the data frame converted by the main control single chip microcomputer to the cloud server, and wireless data uploading and wireless communication are achieved. And the network communication module connected with the main control singlechip realizes the communication of a wired network through an Ethernet interface RJ-45.
As shown in fig. 2, in this embodiment, the EN28J60 provides an ethernet interface for the master single chip, and data can be transmitted through the differential signal input pins TPIN +, TPIN-and the differential signal output pins TPOUT +, TPOUT-in the chip. The ENC28J60 is an independent Ethernet controller with a standard Serial Peripheral Interface (SPI), which can be used as an Ethernet interface of any single chip microcomputer equipped with an SPI communication interface, conforms to all specifications of IEEE802.3, limits an incoming data packet by adopting a series of packet filtering mechanisms, and provides an internal DMA module to realize rapid data throughput and IP verification and calculation supported by hardware, wherein SO is a data output pin of the SPI interface, SI is a data input pin of the SPI interface, SCK is a clock input pin of the SPI interface, and CS is a chip selection input pin of the SPI interface. The communication with the host controller is realized by the interrupt pin INT and the SPI interface pins SO and SI, and the data transmission rate is up to 10 Mb/s. The LED display device comprises an LED indicator lamp, an LEDA driving pin, an LEDB driving pin and an LED DB driving pin, wherein the two special pins of the LEDA driving pin and the LEDB driving pin are used for connecting the LED indicator lamp and indicating the network activity state.
As shown in fig. 3, in the circuit connection diagram of the optoelectronic isolation module of this embodiment, data passing through the data concentrator passes through data pins 485_ B and 485_ a of the SP485S chip and is isolated by the optoelectronic isolation chip LTV-816S, interference of a rear-stage circuit is cut off, the data is transmitted to a data receiving pin MUC _ RXD _485 of the master control single chip microcomputer RS485, and meanwhile, communication can be performed through an RS485 data transmitting pin MCU _ TXD _485, where MCU _ RE _485 is an enable pin of the SP485 chip. The design of the module improves the anti-interference capability of the device on hardware.
As shown in fig. 4, the overall software design of the present invention can be divided into three layers according to functions: an access layer, a processing layer and an application layer. The access layer uploads data acquired by the sensing equipment through a ModBus serial link; the processing layer comprises data frame processing, data encryption and Mqtt/ModBus protocol conversion; the application layer includes remote transmission of data packets and application processing.
As shown in fig. 5, the main data processing algorithm flow chart of the present invention first initializes the serial port of the main control single chip, and the device performs timeout detection. And then, a set state machine is entered to call a data frame acquisition module to perform message analysis on the ModBus response frame of the lower computer, and after the data frame is determined to be correct, the data is stored and encrypted, so that the safety of the data is improved.
Wherein the format of the response frame is: [ device number ] [ function code ] [ byte number N ] [ read value x1]. [ read value xn ] [ CRC high 8 bit ] [ CRC low 8 bit ]. ] first, whether the address byte of the slave machine received first is correct or not is judged, namely, each slave machine has a serial number which is realized through a hardware circuit; secondly, judging the reading and writing modes of the function codes corresponding to the received bytes, then judging whether the received bytes are consistent with the byte numbers of the request frame, and then performing CRC calculation comparison on the data, wherein the correct frame passes through the identification function.
CRC check algorithm:
(1) loading a 16-bit CRC register with 0XFFF, i.e., all 1's;
(2) XOR the first 8-bit byte of the message with the low byte of the 16-bit CRC register, and storing the result in the CRC register;
(3) right shifting the CRC register by one bit, filling zero in the high bit, extracting and detecting the low bit number;
(4) if the low position is 0: repeating the step (3); if the low position is 1: the register is XOR'd with 0XA 0001;
(5) repeating (3) and (4) until 8 displacements are completed;
(6) repeating the step 2-5 for the next byte in the message to know that all messages are processed;
(7) the final value in the CRC register is the CRC value;
(8) the CRC high and low bytes are exchanged and attached to the end of the message.
The RSA asymmetric encryption optimization algorithm encrypts the data extracted from the message: firstly, a packaged RSA algorithm is imported, and real-time data is encrypted to obtain a public key and a private key.
a. Setting a public key storage function;
b. setting a private key storage function; (ii) a
(9) And encrypting the data to be encrypted by using a public key.
Therefore, the optimized asymmetric encryption algorithm is utilized to ensure the data security of the interaction between the terminal equipment and the server.
The data transfer gateway with high safety and strong anti-interference capability replaces the traditional data transfer gateway, solves the problem that the traditional gateway cannot adapt to severe environments with large noise, more vibration and the like, and simultaneously encrypts data to improve the safety of the system. The invention has the characteristics of strong anti-interference capability, high performance, easy operation, high safety and the like.
The above embodiments are preferred embodiments of the present invention, but the present invention is not limited to the above embodiments, and any other changes, modifications, substitutions, combinations, and simplifications which do not depart from the spirit and principle of the present invention should be construed as equivalents thereof, and all such changes, modifications, substitutions, combinations, and simplifications are intended to be included in the scope of the present invention.