---
# System prepended metadata

title: ESP32 LoRa 傳輸資料測試
tags: [LoRa, ESP32, Arduino]

---

---
disqus: ahb0222
GA : G-VF9ZT413CG
---
# ESP32 LoRa 傳輸資料測試
> [color=#40f1ef][name=LHB阿好伯, 2022/11/13][:earth_africa:](https://www.facebook.com/LHB0222/)
###### tags: `Arduino` `ESP32` `LoRa`

[TOC]

今天來測試一款ESP32 內建LoRa的開發版
目前測試結果大約可以傳送1.5km
還沒找到一個夠空曠的地方不然傳輸距離應該會更好

![](https://hackmd.io/_uploads/B1ob4ATBo.png)

![](https://hackmd.io/_uploads/rkWSLA6Sj.png)

![](https://hackmd.io/_uploads/rkaBrC6Bs.png)

# 傳送
![](https://hackmd.io/_uploads/rkpsE0ari.jpg)
```cpp=
/*********
  Modified from the examples of the Arduino LoRa library
  More resources: https://randomnerdtutorials.com
  資料傳送測試每秒傳送一數字並加一
*********/

#include <SPI.h>
#include <LoRa.h>

//define the pins used by the transceiver module
//#define ss 18
//#define rst 14
//#define dio0 26

int counter = 0;
// For a connection via I2C using the Arduino Wire include:
#include <Wire.h>               
#include "HT_SSD1306Wire.h"


SSD1306Wire  display(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED); // addr , freq , i2c group , resolution , rst

#define DEMO_DURATION 3000
typedef void (*Demo)(void);

int demoMode = 0;

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Sender");

  //setup LoRa transceiver module
    LoRa.setPins(18, 14, 26);
  
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");

  display.init();
  display.setFont(ArialMT_Plain_10);
  VextON();
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;

  delay(1000);
  display.clear();
  display.drawString(0, 0, "Hello world");
  display.drawString(0, 30, String(counter));
  display.display();
}
void VextON(void)
{
  pinMode(Vext,OUTPUT);
  digitalWrite(Vext, LOW);
}
```

# 接收
![](https://hackmd.io/_uploads/S1ns40pSo.jpg)

```cpp=
/*********
  Modified from the examples of the Arduino LoRa library
  More resources: https://randomnerdtutorials.com
  接收數據並顯示在螢幕上
*********/

#include <SPI.h>
#include <LoRa.h>

//define the pins used by the transceiver module
//#define ss 18
//#define rst 14
//#define dio0 26

// For a connection via I2C using the Arduino Wire include:
#include <Wire.h>               
#include "HT_SSD1306Wire.h"


SSD1306Wire  display(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED); // addr , freq , i2c group , resolution , rst

#define DEMO_DURATION 3000
typedef void (*Demo)(void);

int demoMode = 0;
int counter = 1;


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Receiver");

  //setup LoRa transceiver module
  LoRa.setPins(18, 14, 26);
  
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");

  delay(100);
  // Initialising the UI will init the display too.
  display.init();
  display.setFont(ArialMT_Plain_10);
  VextON();
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      String LoRaData = LoRa.readString();
      Serial.print(LoRaData); 
      display.clear();
      display.drawString(0, 0, "Hello world");
      display.drawString(0, 30, LoRaData);
      display.display();
    }

    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
 
  }
}

void VextON(void)
{
  pinMode(Vext,OUTPUT);
  digitalWrite(Vext, LOW);
}
```


🌟全文可以至下方連結觀看或是補充


全文分享至

https://www.facebook.com/LHB0222/

https://www.instagram.com/ahb0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

# [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings)



![](https://i.imgur.com/nHEcVmm.jpg)


