11/*
2- Description: LoRa868 Duplex communication.Send messages regularly "HeLoRa World!"
2+ *******************************************************************************
3+ * Copyright (c) 2021 by M5Stack
4+ * Equipped with M5Core sample source code
5+ * 配套 M5Core 示例源代码
6+ * Visit the website for more information:https://docs.m5stack.com/en/module/comx_lorawan
7+ * 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/comx_lorawan
8+ *
9+ * describe: comx_lorawan.
10+ * date:2021/9/2
11+ *******************************************************************************
12+ LoRa868 Duplex communication.Send messages regularly "HeLoRa World!"
13+ LoRa868 双工通讯。定期发送消息“HeLoRa World!”
14+
315*/
416#include < M5Stack.h>
517#include < M5LoRa.h>
618
7- String outgoing; // outgoing message
19+ String outgoing; // outgoing message. 传出讯息
820
9- byte msgCount = 0 ; // count of outgoing messages
10- byte localAddress = 0xFF ; // address of this device
11- byte destination = 0xBB ; // destination to send to
21+ byte msgCount = 0 ; // count of outgoing messages. 传出消息的计数
22+ byte localAddress = 0xFF ; // address of this device. 此设备的地址
23+ byte destination = 0xBB ; // destination to send to. 发送目的地
1224
13- long lastSendTime = 0 ; // last send time
14- int interval = 1000 ; // interval between sends
25+ long lastSendTime = 0 ; // last send time. 上次发送时间
26+ int interval = 1000 ; // interval between sends. 发送间隔
1527
1628void setup () {
1729 M5.begin ();
1830 M5.Power .begin ();
31+
1932 while (!Serial);
2033
2134 Serial.println (" LoRa Duplex B" );
2235
23- // override the default CS, reset, and IRQ pins (optional)
24- LoRa.setPins ();// set CS, reset, IRQ pin
36+ // override the default CS, reset, and IRQ pins (optional). 覆盖默认的 CS、复位和 IRQ 引脚(可选)
37+ LoRa.setPins ();// set CS, reset, IRQ pin. 设置 CS、复位、IRQ 引脚
2538
26- if (!LoRa.begin (868E6 )) { // initialize ratio at 868 MHz
39+ if (!LoRa.begin (868E6 )) { // initialize ratio at 868 MHz. 868 MHz 时的初始化比率
2740 Serial.println (" LoRa init failed. Check your connections." );
28- while (true ); // if failed, do nothing
41+ while (true ); // if failed, do nothing. 如果失败,什么都不做
2942 }
3043
3144 Serial.println (" LoRa init succeeded." );
3245}
3346
3447void loop () {
3548 if (millis () - lastSendTime > interval) {
36- String message = " HeLoRa World!" ; // send a message
49+ String message = " HeLoRa World!" ; // send a message. 发送消息
3750 sendMessage (message);
3851 Serial.println (" Sending " + message);
3952 M5.Lcd .setTextColor (BLUE);
4053 M5.Lcd .println (" Sending " + message);
41- lastSendTime = millis (); // timestamp the message
54+ lastSendTime = millis (); // timestamp the message. 给消息加时间戳
4255 interval = random (1000 ) + 500 ;
4356 }
4457
45- // parse for a packet, and call onReceive with the result:
58+ // parse for a packet, and call onReceive with the result:. 解析数据包,并使用结果调用 onReceive:
4659 onReceive (LoRa.parsePacket ());
4760
4861 if (M5.BtnA .wasPressed ()){
@@ -60,58 +73,58 @@ void loop() {
6073void reinit (){
6174 Serial.println (" LoRa Duplex Reinitialization" );
6275
63- // override the default CS, reset, and IRQ pins (optional)
64- LoRa.setPins ();// set CS, reset, IRQ pin
76+ // override the default CS, reset, and IRQ pins (optional). 覆盖默认的 CS、复位和 IRQ 引脚(可选)
77+ LoRa.setPins ();// set CS, reset, IRQ pin. 设置 CS、复位、IRQ 引脚
6578
66- if (!LoRa.begin (868E6 )) { // initialize ratio at 868 MHz
79+ if (!LoRa.begin (868E6 )) { // initialize ratio at 868 MHz. 868 MHz 时的初始化比率
6780 Serial.println (" LoRa init failed. Check your connections." );
6881 M5.Lcd .setCursor (0 , 0 );
6982 M5.Lcd .setTextColor (RED);
7083 M5.Lcd .println (" Init failed!!!" );
71- while (true ); // if failed, do nothing
84+ while (true ); // if failed, do nothing. 如果失败,什么都不做
7285 }
7386
7487 Serial.println (" LoRa init succeeded." );
7588}
7689
7790void sendMessage (String outgoing) {
78- LoRa.beginPacket (); // start packet
79- LoRa.write (destination); // add destination address
80- LoRa.write (localAddress); // add sender address
81- LoRa.write (msgCount); // add message ID
82- LoRa.write (outgoing.length ()); // add payload length
83- LoRa.print (outgoing); // add payload
84- LoRa.endPacket (); // finish packet and send it
85- msgCount++; // increment message ID
91+ LoRa.beginPacket (); // start packet. 开始包
92+ LoRa.write (destination); // add destination address. 添加目标地址
93+ LoRa.write (localAddress); // add sender address. 添加发件人地址
94+ LoRa.write (msgCount); // add message ID. 添加消息标识
95+ LoRa.write (outgoing.length ()); // add payload length. 添加有效载荷长度
96+ LoRa.print (outgoing); // add payload. 添加有效载荷
97+ LoRa.endPacket (); // finish packet and send it. 完成数据包并发送
98+ msgCount++; // increment message ID. 增加消息 ID
8699}
87100
88101void onReceive (int packetSize) {
89- if (packetSize == 0 ) return ; // if there's no packet, return
102+ if (packetSize == 0 ) return ; // if there's no packet, return. 如果没有包,返回。
90103
91104 // read packet header bytes:
92- int recipient = LoRa.read (); // recipient address
93- byte sender = LoRa.read (); // sender address
94- byte incomingMsgId = LoRa.read (); // incoming msg ID
95- byte incomingLength = LoRa.read (); // incoming msg length
105+ int recipient = LoRa.read (); // recipient address. 收件人地址。
106+ byte sender = LoRa.read (); // sender address. 发件人地址。
107+ byte incomingMsgId = LoRa.read (); // incoming msg ID. 传入的消息 ID。
108+ byte incomingLength = LoRa.read (); // incoming msg length. 传入消息长度。
96109
97110 String incoming = " " ;
98111
99112 while (LoRa.available ()) {
100113 incoming += (char )LoRa.read ();
101114 }
102115
103- if (incomingLength != incoming.length ()) { // check length for error
116+ if (incomingLength != incoming.length ()) { // check length for error. 检查错误长度
104117 Serial.println (" error: message length does not match length" );
105- return ; // skip rest of function
118+ return ; // skip rest of function. 跳过其余功能
106119 }
107120
108- // if the recipient isn't this device or broadcast,
121+ // if the recipient isn't this device or broadcast,. 如果收件人不是此设备或广播,
109122 if (recipient != localAddress && recipient != 0xFF ) {
110123 Serial.println (" This message is not for me." );
111- return ; // skip rest of function
124+ return ; // skip rest of function. 跳过其余功能
112125 }
113126
114- // if message is for this device, or broadcast, print details:
127+ // if message is for this device, or broadcast, print details:. 如果消息是针对此设备或广播的,则打印详细信息:
115128 Serial.println (" Received from: 0x" + String (sender, HEX));
116129 Serial.println (" Sent to: 0x" + String (recipient, HEX));
117130 Serial.println (" Message ID: " + String (incomingMsgId));
0 commit comments