POSTS
制霸 IoT 30Day! Day 26 LoRaWAN 開發介紹
LoRaWAN 開發介紹
前面我們介紹了 wifi 與藍芽今天來介紹使用 ESP32 開發 LoRaWAN
硬體介紹
今天介紹的是使用 Heltec WiFi LoRa 32 (V2)。
- 微處理器:ESP32 (雙核 32 位 MCU + ULP 內核),配合 LoRa 節點芯片 SX1276/SX1278。
- Micro USB 接口,具有完整的穩壓電路、ESD 保護、短路保護等安全措施。
- 預留 SH1.25-2 電池接口,集成鋰電池管理系統 (充放電管理、過充保護、電池電量檢測、USB/電池電源自動切換)。
- 提供 WiFi、LoRa、藍牙三種網絡連接方式、板載 Wi-Fi、藍牙專用 2.4GHz 金屬 3D 天線、預留 IPEX (u. FL) 接口共 LoRa 信號使用。
- 板載 0.96 吋 128 * 64 OLED 顯示屏,可用於顯示調試信息、傳感器數據、電池電量等信息。
- 通過 CP2102 USB-UART 芯片,提供串口功能,方便程序下載、調試信息打印。
- 支援 Arduino 開發環境:我們提供 ESP32 + Lorwan 協議的 Arduino® 庫,這是一個標準的 LoRaWAN 通信協議,可以與支持 LoRaWAN 協議的任何 LoRa 網關進行組網通信。
- 具有良好的射頻電路設計和阻抗匹配,以及基本的低功耗設計 (睡眠電流 ≤800ua),方便用戶快速驗證無線通信方案。
WiFi LoRa 32 (V2)產品圖。
相信大家可以看到與此模組等同於一次可以涵蓋 WIFI、藍芽、LoRa 根本就是大套餐解決方案。 該產品上還有小型 0.96 吋 OLED 很方便做開發使用! 不過實際開發到產品上市因該沒有人會做這樣複雜產品搞死自己?!
Arduino 安裝使用
依照說明先在偏好設定額外的開發板管理員網址輸入:https://docs.heltec.cn/download/package_heltec_esp32_index.json
arduino 安裝
在前往開發版管理員去安裝新的開發板! 輸入『Heltec ESP32 』做搜尋。
看到該板 點選安裝
在前往套件管理程式安裝開發板套件! 一樣輸入『Heltec ESP32 』做搜尋。
arduino 安裝套件
arduino 選擇新開發版
測試使用!
新增範例程式!
/*
* HelTec Automation(TM) WIFI_LoRa_32 factory test code, witch includ
* follow functions:
*
* - Basic OLED function test;
*
* - Basic serial port test(in baud rate 115200);
*
* - LED blink test;
*
* - WIFI join and scan test;
*
* - LoRa Ping-Pong test(DIO0 -- GPIO26 interrup check the new incoming messages;
*
* - Timer test and some other Arduino basic functions.
*
* by Aaron.Lee from HelTec AutoMation, ChengDu, China
* 成都惠利特自动化科技有限公司
* www.heltec.cn
*
* this project also realess in GitHub:
* https://github.com/HelTecAutomation/Heltec_ESP32
*/
#include "Arduino.h"
#include "heltec.h"
#include "WiFi.h"
#include "images.h"
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
String rssi = "RSSI --";
String packSize = "--";
String packet;
unsigned int counter = 0;
bool receiveflag = false; // software flag for LoRa receiver, received data makes it true.
long lastSendTime = 0; // last send time
int interval = 1000; // interval between sends
void logo(){
Heltec.display -> clear();
Heltec.display -> drawXbm(0,5,logo_width,logo_height,(const unsigned char *)logo_bits);
Heltec.display -> display();
}
void WIFISetUp(void)
{
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.disconnect(true);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.begin("Your WiFi SSID","Your Password");//fill in "Your WiFi SSID","Your Password"
delay(100);
byte count = 0;
while(WiFi.status() != WL_CONNECTED && count < 10)
{
count ++;
delay(500);
Heltec.display -> drawString(0, 0, "Connecting...");
Heltec.display -> display();
}
Heltec.display -> clear();
if(WiFi.status() == WL_CONNECTED)
{
Heltec.display -> drawString(0, 0, "Connecting...OK.");
Heltec.display -> display();
// delay(500);
}
else
{
Heltec.display -> clear();
Heltec.display -> drawString(0, 0, "Connecting...Failed");
Heltec.display -> display();
//while(1);
}
Heltec.display -> drawString(0, 10, "WIFI Setup done");
Heltec.display -> display();
delay(500);
}
void WIFIScan(unsigned int value)
{
unsigned int i;
if(WiFi.status() != WL_CONNECTED)
{
WiFi.mode(WIFI_MODE_NULL);
}
for(i=0;i<value;i++)
{
Heltec.display -> drawString(0, 20, "Scan start...");
Heltec.display -> display();
int n = WiFi.scanNetworks();
Heltec.display -> drawString(0, 30, "Scan done");
Heltec.display -> display();
delay(500);
Heltec.display -> clear();
if (n == 0)
{
Heltec.display -> clear();
Heltec.display -> drawString(0, 0, "no network found");
Heltec.display -> display();
//while(1);
}
else
{
Heltec.display -> drawString(0, 0, (String)n);
Heltec.display -> drawString(14, 0, "networks found:");
Heltec.display -> display();
delay(500);
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Heltec.display -> drawString(0, (i+1)*9,(String)(i + 1));
Heltec.display -> drawString(6, (i+1)*9, ":");
Heltec.display -> drawString(12,(i+1)*9, (String)(WiFi.SSID(i)));
Heltec.display -> drawString(90,(i+1)*9, " (");
Heltec.display -> drawString(98,(i+1)*9, (String)(WiFi.RSSI(i)));
Heltec.display -> drawString(114,(i+1)*9, ")");
// display.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Heltec.display -> display();
delay(800);
Heltec.display -> clear();
}
}
bool resendflag=false;
bool deepsleepflag=false;
void interrupt_GPIO0()
{
delay(10);
if(digitalRead(0)==0)
{
if(digitalRead(LED)==LOW)
{resendflag=true;}
else
{
deepsleepflag=true;
}
}
}
void setup()
{
Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, BAND /*LoRa RF working band*/);
logo();
delay(300);
Heltec.display -> clear();
WIFISetUp();
WIFIScan(1);
attachInterrupt(0,interrupt_GPIO0,FALLING);
LoRa.onReceive(onReceive);
send();
LoRa.receive();
displaySendReceive();
}
void loop()
{
if(deepsleepflag)
{
LoRa.end();
LoRa.sleep();
delay(100);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
pinMode(18,INPUT);
pinMode(19,INPUT);
pinMode(26,INPUT);
pinMode(27,INPUT);
digitalWrite(Vext,HIGH);
delay(2);
esp_deep_sleep_start();
}
if(resendflag)
{
resendflag=false;
send();
LoRa.receive();
displaySendReceive();
}
if(receiveflag)
{
digitalWrite(25,HIGH);
displaySendReceive();
delay(1000);
receiveflag = false;
send();
LoRa.receive();
displaySendReceive();
}
}
void send()
{
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter++);
LoRa.endPacket();
}
void displaySendReceive()
{
Heltec.display -> drawString(0, 50, "Packet " + (String)(counter-1) + " sent done");
Heltec.display -> drawString(0, 0, "Received Size" + packSize + " packages:");
Heltec.display -> drawString(0, 10, packet);
Heltec.display -> drawString(0, 20, "With " + rssi);
Heltec.display -> display();
delay(100);
Heltec.display -> clear();
}
void onReceive(int packetSize)//LoRa receiver interrupt service
{
//if (packetSize == 0) return;
packet = "";
packSize = String(packetSize,DEC);
while (LoRa.available())
{
packet += (char) LoRa.read();
}
Serial.println(packet);
rssi = "RSSI: " + String(LoRa.packetRssi(), DEC);
receiveflag = true;
}
實際運作狀況 第一組
實際運作狀況 第二組
第二組 有外殼是不是很可愛!
結語
今天介紹 LoRaWAN 開發使用方式,是不是很簡單而且效果驚人! 我家在大樓七樓且位置在大樓對內處,走去 100 公尺外隔三棟大樓還收得到!