Equipment communication method based on LoRa private protocol
Technical Field
The invention relates to the technical field of wireless communication, in particular to a device communication method based on LoRa private protocol.
Background
In the rapidly developing technology age of today, the demand for remote monitoring and data transmission is growing, covering a wide range of industries and applications including but not limited to environmental protection, smart agriculture, industrial automation, smart cities, logistics tracking and emergency response systems, which generally rely on high-performance sensor networks to collect critical data (such as temperature, displacement, flow, radiation and vibration, etc.) and transmit these data in real time to a central monitoring system.
In view of the fact that these sensors are mostly located in outdoor environments where power is not available, they must be able to operate in a low power consumption mode, in addition, in view of the fact that the distance between the monitoring point and the monitoring center is often very far away, this requires that the sensor device must have long distance data transmission capabilities, existing wireless communication technologies, such as Wi-Fi, are not suitable for large scale remote monitoring networks due to the limitations of transmission distance and power consumption, and also, while cellular network technologies such as 2G/3G/4G are capable of implementing long distance data transmission, their high deployment cost and large power consumption limit their practicality in these applications, especially when the data transmission amount is too large, it is very easy to cause transmission errors or affect transmission effects.
Disclosure of Invention
(One) solving the technical problems
In order to overcome the defects of the prior art, the invention provides a device communication method based on a LoRa private protocol, which comprises the steps of firstly fragmenting a message when transmitting a large message, filling each fragment into a LoRa data packet according to the format of the LoRa data packet structure, sequentially sending each sub-packet, and recombining the sub-packets into a complete message after receiving all the sub-packets by a receiver, thereby greatly reducing the error rate during transmission, ensuring the transmission effect and solving the problems in the background art.
(II) technical scheme
In order to achieve the above purpose, the invention is realized by the following technical scheme:
a device communication method based on LoRa private protocol includes the following steps:
the communication request initiator generates a request message, and the message subpackages are carried out when the size of the request message exceeds the limit;
The communication request initiator sequentially sends all the message sub-packets from the sub-packet 1 until the last message sub-packet is sent;
The communication request responding party receives the packet header of the packet 1 in the request message, obtains the plaintext data of each packet body after verification and adjustment processing, and forms complete packet data;
after the communication request responding party analyzes according to the command field in the packet head of any packet, the communication request responding party executes corresponding operation and generates a response message by using the complete message data;
The communication request responding party carries out subpackaging processing on the response message;
the communication request responding party judges whether the request message is a multicast or broadcast message;
If so, returning a response message according to the delay set time length T, and if not, returning the response message without delay, wherein the broadcast packet unconditionally returns the response message, the multicast packet judges whether the multicast packet is in the group, the unicast packet returns the response message only if the serial number dest_SN of the whole destination device is matched, and the rest cases do not respond;
and the communication request initiator receives the message packets of all the response messages and obtains decrypted complete message data.
Further, the request message size overrun indicates that the request message size exceeds the maximum length limit of the LoRa single packet transmission;
The packet comprises a packet header, a packet body and a packet tail, wherein the data in the packet header comprises a packet start sign field, a packet length field, a packet option field, a destination equipment serial number field, a source equipment serial number field, a packet serial number field, a command field, a random key field and a packet header verification field;
the data in the package includes load data as source data;
The data in the packet tail is check data, and the check information generated by taking the data in the packet head and the packet body as input is used for judging whether the data is sent in error in the transmission process.
Further, the packet start flag field describes a data start position of a packet, and the packet length field is combined to obtain a start and end position of data belonging to the packet;
the packet length field describes the length of a packet, the total byte length from the header packet start flag field to the packet tail check data;
The packet option field is packet auxiliary description information and consists of a plurality of flag bits;
The field of the sequence number of the destination device describes the identity information of the packet responder, the condition matching is carried out on the received packet device by using the sequence number and the sequence number of the self device, the packet is received and is processed in the next step after the matching is successful, otherwise, the packet is discarded;
The source equipment serial number field describes the identity information of the packet initiator, and if a response packet needs to be returned, the equipment receiving the packet acquires the equipment serial number of the corresponding target equipment, which is returned by the response packet, from the source equipment serial number field;
The packet sequence number field shows what packet the packet is a complete message;
The command field describes the intent of the package;
the random key field is used for data encryption;
the packet header check field is used to check whether an error occurs in the packet header transmission process.
Further, the flag bits include, but are not limited to, a response flag bit and a message end flag bit, wherein the response flag bit describes that the packet is a request packet or a response packet, the response flag bit is 0 to represent the request packet, the response flag bit is 1 to represent the response packet, the response packet is a response to the request packet, and the message end flag bit describes whether the fragmented packet is the last fragmented packet of a complete message.
Further, before the packet is sent, the process of adding ciphertext data is that a random key with the length of 2 bytes is dynamically generated and filled in a random key field of a packet head, and the plaintext data of the packet body is encrypted;
Before encryption, checking whether the length of the plaintext data of the bag body to be transmitted is an integer multiple of the length of a random key;
If not, carrying out tail zero padding on the inclusion plaintext data, then taking out two bytes from the inclusion plaintext data subjected to zero padding and carrying out exclusive or processing on the two bytes and a random key each time to generate 2-byte ciphertext data, and repeating the steps until all the inclusion plaintext data subjected to zero padding are encrypted;
and discarding single ciphertext byte data generated by zero padding at the tail of the bag body, intercepting the original bag body length data, and filling the single ciphertext byte data into the bag body.
Further, after the packet is received, the decryption process is that the receiving equipment decrypts the ciphertext data of the packet body by using the random key in the random key field of the packet head;
Before decryption, checking whether the length of the ciphertext data of the bag body is an integer multiple of the length of the random key;
If not, carrying out tail zero padding on the bag body ciphertext data, then taking out two bytes from the bag body ciphertext data subjected to zero padding and carrying out exclusive or processing on the two bytes and the random key each time to generate 2-byte plaintext data, and repeating the steps until all the bag body ciphertext data subjected to zero padding are decrypted;
And discarding the plaintext byte data at the tail of the bag body, and intercepting the original bag body length data to obtain decrypted bag body data.
Further, the verification process is as follows:
The packet head is checked, the packet length is calculated through the packet length field in the packet head after the check is passed, and the packet ciphertext data and the packet tail check data are read;
If the receiving condition is met, checking the complete packet, decrypting the packet body ciphertext data by using a random key field in the packet head after the checking is passed, repeating the steps to obtain packet body plaintext data of each packet, and caching the packet body ciphertext data.
Further, the method for realizing broadcasting comprises the following steps:
splitting the equipment serial number into a group number and adding an equipment ID, after one piece of equipment receives a packet and checks the packet to be free of errors, judging the condition of the target equipment serial number of the packet header, and if the group number part is 0xFFFF, indicating that the packet is a broadcast packet, and responding;
The method for realizing multicast comprises the following steps:
if the group number part is not 0xFFFF, but the equipment ID part is 0xFFFFFFFF, indicating that the packet is a multicast packet, comparing the group number with the group number in the own equipment serial number, and if the group number is consistent with the group number in the own equipment serial number, responding;
the unicast realization method comprises the following steps:
If the group number part is not 0xFFFF, the device ID part is not 0xFFFFFFFF, which indicates that the packet is a unicast packet, and the device serial number of the packet is consistent with the device serial number of the responding party in the packet header and responds.
Further, the response message is subjected to subpackaging, the packet header ACK bit is set to 1, the packet header ACK bit indicates that the response packet is a response packet, the target equipment serial number fills the source equipment serial number in the request message, and the source equipment serial number fills the own equipment serial number.
Further, when a response message is returned according to the delay set time length T, the calculation process of the delay set time length T is as follows:
firstly, acquiring the total byte length L of a current request message and a preset initial set time length T 0;
Secondly, a mathematical function combination model is built, L and T 0 are taken as input, and the combination model is based on the following formula:
wherein C represents a constant for adjusting the influence degree of the logarithmic function on T, the value range of C is 1-10, G represents a random factor, and the value range is 0-1;
And finally, outputting the calculated delay setting time length T.
(III) beneficial effects
The invention provides a device communication method based on LoRa private protocol, which has the following beneficial effects:
The communication method adopts a request response mode, an initiator sends a request message, a responder receives the request message to execute corresponding operation and return a response message, and a sender sending the request message receives the response message and executes corresponding processing, so as to solve the problem that the length of data sent by LoRa for one time cannot exceed the limit of 256 bytes, when a large message is transmitted, firstly slicing the message, filling each slice into the LoRa data packet according to the format of the LoRa data packet structure, sequentially sending each sub-packet, and recombining the whole sub-packet into a complete message after receiving all sub-packets by a receiver;
The LoRa data packet structure can realize one-to-one and one-to-many communication between a communication request initiator and a communication request responder, wherein one-to-one communication is unicast communication, multicast and broadcast communication can be realized during one-to-many communication, a random delay response mechanism is designed for preventing data collision caused by simultaneous return of response messages by a plurality of communication request responders during one-to-many communication, dynamic delay setting is performed according to actual conditions, accuracy and effectiveness of delay setting are ensured while the probability of data collision is reduced, and the designed LoRa data packet structure supports encryption and decryption of message data by considering the requirement of data transmission safety, and uses the set of communication method and relays a plurality of devices to perform long-distance real-time transmission on the data, thereby ensuring the transmission effect.
Drawings
FIG. 1 is a diagram of a LoRa packet structure according to the present invention;
FIG. 2 is a schematic diagram of a method for encrypting and decrypting data according to the present invention;
FIG. 3 is a schematic diagram of a communication mode of the present invention;
fig. 4 is a schematic diagram of a communication timing sequence according to the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Example 1
Referring to fig. 1, a method for device communication based on the LoRa private protocol is disclosed, in which data is transmitted between devices in a packet unit, the maximum data length of a packet transmitted by LoRa in a single time is typically 256 bytes due to the technical characteristics of LoRa, in order to transmit a message with a length exceeding the length limit, a method is designed to fragment the message, the size of each packet after fragmentation is not greater than the limit length, and a receiver receives all the fragments and then assembles the fragments into a complete message.
In the embodiment, the package content comprises a package head, a package body and a package tail, wherein the package head is used for describing information such as package length, package sender identity information, package receiver identity information, package body load data meaning and the like, the package body load data is source data such as sensor state information data, equipment control information data and the like, the package tail check data is check information generated by taking the package head and the package body data as inputs, and the package receiver judges whether the data is transmitted in the transmission process or not through the package tail check information;
The packet Header can be subdivided into a packet start flag field SOP, a packet length field length, a packet option field option, a destination device serial number field dest_SN_1-2, a source device serial number field source_SN_1-2, a packet serial number field seq_number, a command field command, a random key field random_key and a packet Header check field header_CRC 16;
The packet start flag field describes the data start position of a packet, and in combination with the packet length, the start and end positions of data belonging to a packet can be obtained, and the data needs to be fixed to a certain value, such as 0xAAAA, in practical application;
The packet length section describes the length of one packet, indicating the total byte length from the header packet start flag field to the packet end check data.
The packet option field is packet auxiliary description information, and consists of a plurality of flag bits, such as a response flag bit, a message end flag bit and the like, wherein the response flag bit describes whether the packet is a request packet (the response flag bit is 0) or a response packet (the response flag bit is 1). The response packet is a response to the request packet, for example, the communication request sender sends out the request packet for acquiring the sensor data, and the communication request responder returns the response packet with the sensor data after receiving the request packet. The end-of-message flag bit is the last fragment packet (0 represents the last fragment packet) describing whether the fragment packet is a complete message;
The sequence number field of the destination device describes the identity information of the packet receiver, the device receiving the packet uses the sequence number to carry out condition matching with the own sequence number, and after the matching is successful, the next processing is carried out on the packet data, otherwise, the packet data is discarded;
The source equipment serial number field describes the identity information of the sender of the packet, and if a response packet needs to be returned, the equipment receiving the packet knows which equipment the response packet should be returned to from the source equipment serial number field;
the packet sequence number field indicates what fragment packet the packet is a complete message;
the command field describes the intention of the packet, such as the packet requesting the sensor data, or setting to open certain switches, etc., and the receiver also understands the packet body data according to the command field;
The random key field is used for data encryption to prevent data from being stolen. The random key is transmitted in the packet head of each packet, firstly, the communication equipment omits the complicated process of configuring the key, and the key required by the receiver for decrypting the packet body data can be directly found in the packet head;
the packet header checking field is used for checking whether an error occurs in the packet header transmission process;
The length of the packet header is a fixed value, after receiving LoRa data, the receiving equipment searches for a packet start flag SOP, reads the length data of the packet header after searching for the length data of the packet header, takes out the data before the packet header check field to generate a CRC16 check value, compares the CRC16 check value with the packet header check field, and if the CRC16 check value is consistent with the packet header check field, subtracts the length of the packet header from the length of the packet header to obtain the total length of the packet body length and the packet tail check data, and reads the packet body data and the packet tail check data. The header data and the body data are then used to generate a CRC32 check value and compared to the trailer check data, if consistent, indicating that the packet is error free during transmission.
Example two
Referring to fig. 2, a device communication method based on the LoRa private protocol can realize 3 communication modes of unicast, multicast and broadcast, and the realization method includes that a device serial number is split into a group number plus a device ID, when a device receives a packet and checks the packet, a condition judgment is carried out on a destination device serial number of the packet header, if the group number is 0xFFFF, the packet is indicated to be a broadcast packet and needs to be responded, if the group number is not 0xFFFF, but the device ID is 0xffffff, the packet is indicated to be a multicast packet, then the group number is compared with the group number in the self device serial number, if the group number is consistent, the response is needed, if the group number is not 0xFFFF, the device ID is not 0xffffff, the packet is indicated to be a unicast packet, and the response can be carried out only if the group number is completely consistent with the receiving device serial number in the self device serial number.
Example III
Referring to fig. 3, a device communication method based on the LoRa private protocol can encrypt data to be transmitted to prevent the data from being stolen, before packet transmission, the transmitting device dynamically generates a random key random_key with a length of 2 bytes to be filled in a packet header random key field and encrypts packet body plaintext data by using the random key random_key, before encryption, checks whether the length of the packet body plaintext data to be transmitted is integral multiple of the random key length, if not, carries out tail zero padding processing on the packet body plaintext data, namely byte 0x00 processing, then takes out two bytes from the packet body plaintext data after the zero padding processing and carries out exclusive or (XOR) processing on the random key each time to generate 2-byte ciphertext data, repeats until all the packet body plaintext data after the zero padding processing is encrypted, and finally discards the ciphertext byte data generated by adding byte 0x00 data at the end of the packet body, only intercepts the original packet body plaintext data with the length but is the encrypted data and fills the packet body;
After the packet is received, the receiving equipment decrypts the packet body ciphertext data by using a random key in a packet head random key field, and also, before decryption, the receiving equipment needs to check whether the length of the packet body ciphertext data is an integer multiple of the length of the random key or not, if not, the packet body ciphertext data is subjected to tail zero padding, then, two bytes are taken out from the packet body ciphertext data subjected to the zero padding and are subjected to exclusive OR processing with the random key each time to generate 2-byte plaintext data, and the steps are repeated until the packet body ciphertext data subjected to the zero padding are completely decrypted, and finally, the plaintext byte data generated by adding 0x00 data at the end of the packet body is abandoned, and only the original packet body length data is intercepted, so that decrypted packet body data is obtained;
The simple data encryption and decryption method is suitable for equipment and systems with weak operation capability, if the equipment has strong operation capability or is provided with a hardware AES encryption and decryption module, encryption and decryption methods such as AES128 and AES256 can be used, when the AES encryption and decryption method is used, 128-bit or 256-bit keys can be generated by combining fields such as a packet header random key field, a destination equipment serial number field, a source equipment serial number field and a packet serial number field with a method for supplementing 0x00 data, and the actual arrangement and combination method can be determined by itself, so that the risk of data decoding caused by leakage of a key generation method is reduced.
Example IV
Referring to fig. 4, an actual communication time sequence flow is described in the following steps that S1, a communication request initiator generates a request message, and if the size of the request message exceeds the maximum length limit of LoRa single packet transmission, the message is packetized. And on the setting of the packet header, the packet start mark field is fixed to be 0xAAAA, and the packet length is set according to the actual size of each sub-packet. The method comprises the steps of setting an ACK bit to 0 to indicate a request packet, setting a last subpacket MF bit to 0 to indicate the last subpacket of a complete message, setting the rest subpacket MF bit to 1, filling a source equipment serial number into an own equipment serial number, filling a destination equipment serial number according to whether the application range of the request message is unicast, multicast or broadcast, setting a packet serial number from the first subpacket to the last subpacket to n, wherein n is determined by the size of an actual message, filling a command field according to the actual request command, filling each subpacket with the same value, filling a random key field with a dynamically generated random key, filling each subpacket with a different random key, generating a CRC16 check value according to other fields of the filling, filling each subpacket with a random key in the packet header, encrypting own packet body data according to the random key in the packet header, and finally generating a CRC32 check value according to the filled packet header and the encrypted packet body data to the packet body data.
S2, the communication request initiator sequentially sends all the message sub-packets generated in the S1 from the sub-packet 1 until the last message sub-packet is sent.
S3, the communication request responding party receives the 1 packet header of the request message, checks the packet header, calculates the packet length through the packet length field in the packet header after the check is passed, reads the packet ciphertext data and the packet tail check data, combines the packet header, the packet body and the packet tail data into a complete packet, decides whether to discard the packet or receive the packet and perform the next processing according to the receiving conditions of broadcasting, multicasting and unicasting, and checks the whole packet if the receiving conditions are met. After verification, decrypting the packet body ciphertext data by using a random key field in the packet head to obtain packet body plaintext data of the packet 1, caching, repeating the steps until the packet body plaintext data of the last packet is obtained and cached, and finally combining all cached packet body plaintext data into complete packet data.
S4, the communication request responding party understands and uses the complete message data finally generated in S3 according to the meaning of the command field in any message packet header in S3, executes corresponding operation and generates a response message.
S5, the communication request responder carries out subpackaging processing on the response message of S4, a packet header ACK bit is set to 1 to indicate that the response packet is a response packet, the target equipment serial number fills the source equipment serial number in the request message, the source equipment serial number fills the own equipment serial number, and the remaining packet header field filling, packet body data encryption and packet tail check value filling methods are the same as those described in S1.
S6, the communication request responding party judges whether the request message is a multicast or broadcast message, if so, the response message is returned according to the delay set time length T, so that a plurality of responding parties can be prevented from returning the response message at the same time, and data collision is avoided. When a response message is returned, all the message sub-packets generated in the step S5 are sequentially sent out from the sub-packet 1 until the last message sub-packet is sent out;
when a response message is returned according to the delay set time length T, the calculation process of the delay set time length T is as follows:
firstly, acquiring the total byte length L of a current request message and a preset initial set time length T 0;
Secondly, a mathematical function combination model is built, L and T 0 are taken as input, and the combination model is based on the following formula:
In the formula, C represents a constant for adjusting the influence degree of a logarithmic function on T, the value range of C is 1-10, the value of the embodiment is usually 10, the influence degree of the packet length L on T can be controlled by selecting a proper value of C, log 2 (L+1) is used for introducing a nonlinear relation, the logarithmic function is helpful for preventing the rapid increase of T caused by the small change of the packet length L because of the slow increase of the logarithmic function when the input value is larger, meanwhile, L+1 ensures that the value in the logarithmic function is always larger than zero, T 0 provides a basic value of delay, even if the total byte length is zero, a non-zero delay exists, G represents a random factor, the value range of the random factor is 0-1, different equipment random factors take different values, and even if the lengths of broadcast or multicast packets received by a plurality of receivers are L, the finally calculated value of the delay setting time length T is different after the random factor is introduced, so that data collision is avoided;
finally, outputting the calculated delay setting time length T;
And S7, the communication request initiator receives all response message packets according to the method described in the S3 to obtain decrypted complete response message data, and the communication request initiator carries out the next processing on the response message data, which is not described in detail herein.
S8, ending the communication.
In the application, the several formulas are all the numerical calculation after dimensionality removal, the formula is set up by collecting a large amount of data to perform software simulation to obtain a formula of the latest real situation, and part of coefficients or weights in the formula are set by those skilled in the art according to the actual situation, so that details are not repeated here.
The above embodiments may be implemented in whole or in part by software, hardware, firmware, or any other combination. When implemented in software, the above-described embodiments may be implemented in whole or in part in the form of a computer program product. Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed over a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
The foregoing is merely illustrative of the present application, and the present application is not limited thereto, and any person skilled in the art will readily recognize that variations or substitutions are within the scope of the present application.