当前位置:网站首页>Fast implementation of thread mesh networking

Fast implementation of thread mesh networking

2022-06-27 06:24:00 shine_ blink

One 、 Realization function

This chapter implements two Core Equipment communication , One of the devices is server、 Another device is client. whenever client The device presses... On the circuit board BTN1 Keystrokes , Will send to server Upload data . When server received client The data on the circuit board will also be switched after passing the verification LED1 On and off state of , meanwhile client I will also receive server Orders issued ,client After the verification command passes, the... On the circuit board will also be switched LED1 On and off state of .

in addition server and client On the circuit board LED2 Are used to indicate the connection status , Only when both sides LED2 When both lights are on at the same time , It means that the two can start normal communication .

Be careful : because Core Inside comes 2.4G Wireless capabilities , Therefore, the wireless communication function of this chapter can be realized without external devices or modules . But if it's used 2.4G function ,Core Of Ble Bluetooth features and USB The function cannot be used , Developers need to pay attention to this .

Two 、 An overview of communication principles

Core In the bottom layer, based on Thread The development of wireless network COAP agreement , In fact, it is a multipoint mesh Network communication protocol , But the point-to-point communication in this example only realizes two nodes .

By understanding Thread The mechanism of wireless network should know , stay Thread There are three types of nodes in :Leader,Router,EndDevice. But please don't compare them with COAP In the agreement server,client Conceptual confusion . Because in Thread Network server and client The node may be Leader,Router,EndDevice Any of the three , And it's not fixed , Will change with the dynamic changes of the network , And this also reflects Thread The power of the network , That is, if a routing node in the network has a problem , Other nodes in the network will dynamically adjust their roles from the network .

Server and Client Schematic diagram of communication between
 Insert picture description here
Communication restrictions
There can only be one in a network server, But there can be hundreds client.

therefore server It can be used as the gateway of the whole network , To connect to other external networks such as wifi,NBIOT,LORA etc. .
PanID And frequency Channel To determine the mesh The uniqueness of the network .
Based on this , We can design PanID And frequency Channel Different Mesh The same physical space exists in the network .

client The name of must be 8 Characters ,client to server The data length of must be 8 Bytes ,server Issue to client The command length of must also be 8 Bytes .( commonly 8 The space of bytes is enough for uploading sensor data or issuing control commands .)
server Issue to client Your orders will not be immediately client received , But wait until client Upload data to next time server when ,server The command data will be attached and sent to during the response client. So if you want to make client Receive orders as soon as possible ,client Can add to server Frequency of uploading data .

There are three reasons for this :(1) If client Nodes are low-power dormant sensor devices , Probably won't be online most of the time , therefore server It is impossible to send data in real time
(2) If client Nodes are dynamically connected to and disconnected from the network ,server It is also impossible to establish a stable connection with such nodes (3)
Core Our internal resources are limited , If for each client It takes a lot of time for each node to maintain a connection Ram.

Client In the client code ClientName The name variable can be arbitrary 8 Characters ( Must be 8 individual ), For example, in this case ClientName The name is "Client01".

Be careful :ClientName yes Client In the network Server The only identity recognized ID.

3、 ... and 、Client The complete code of the terminal

--PanID and Channel To determine the mesh The uniqueness of the network 
PanID = 0x1234 --16 An integer 
Channel = 11 -- Choice range (11~26)
MyRole = "Client" -- Defined as client role 
LIB_MeshConfig(MyRole,PanID,Channel) -- Start and join mesh The Internet 
--client Own name "Client01" It has to be for 8 Characters , And and server The end mentioned client The names should be the same 
ClientName = "Client01" --ClientName yes Client In the network Server The only identity recognized ID
LIB_GpioOutputConfig("D8","STANDARD") --LED1
LIB_GpioOutputConfig("D9","STANDARD") --LED2
LIB_GpioWrite("D8",1) -- destroy 
LIB_GpioWrite("D9",1) -- destroy 
-- Set button 1( Occupy D0 mouth , Low level active )
LIB_ButtonConfig("BTN1","D10","L")
net_state = 0
-- Start the big cycle 
while(GC(1) == true)
do
    -- Network state led instructions 
    net_state = LIB_MeshClientNetStateQuery()
    if net_state == 2 then --client Joined mesh Net and found server
        LIB_GpioWrite("D9",0) --LED2 bright 
    else
        LIB_GpioWrite("D9",1) --LED2 destroy 
    end
    -- If BTN1 Press the key briefly and client Has joined mesh And found server, As to the Server Upload data 
    key = LIB_ButtonQuery("BTN1")
    if key == 1 and net_state == 2 then
        data = {
    0xA1,0x00,0x00,0x00,0x00,0x00,0x00,0x00} 
        LIB_MeshClientSendData(ClientName,data)
    end
    -- Query whether received from server Issued 8 Byte command , And analyze 
    recv_flag, cmd = LIB_MeshClientRecvCommand()
    if recv_flag == 1 and #cmd == 8 then
        if cmd[1] == 0x01 then -- Here we only analyze server The first byte of the command issued 
            LIB_GpioToggle("D8") --LED1 On or off switch 
        end
    end
end
原网站

版权声明
本文为[shine_ blink]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270617053370.html