Inhaltsverzeichnis

LoRa - Long range network

Übersicht

Mitlerweile gibt es China LoRa module wie Sand am Meer. ESP32 LoRa Module Das Modul hat ein kleines Oled Display, LoRa und ein ESP32 onBoard. Hier ein kleiner netter Beispiel sketch für Senden und Empfangen.

Ich hafte nicht für den Code, den ich weiß zugegeben nicht ob der Sketch den EU Richtlinien entspricht. Nutzen auf eigene Gefahr! In der LoRa Lib muss auf alle Fälle die Leistung heruntergestellt werden.

Sender

sender.cpp
// This example just provide basic LoRa function test;
// Not the LoRa's farthest distance or strongest interference immunity.
// For more informations, please vist www.heltec.cn or mail to support@heltec.cn
 
#include <SPI.h>
#include <LoRa.h>
#include<Arduino.h>
#include "SSD1306.h "
// WIFI_LoRa_32 ports
 
// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)
 
#define SS      18
#define RST     14
#define DI0     26
#define BAND    833E6  //915E6 -- 这里的模式选择中,检查一下是否可在中国实用915这个频段
 
int counter = 0;
int counter2 = 0;
SSD1306  display(0x3c, 4, 15);
 
void setup() {
  pinMode(25,OUTPUT); //Send success, LED will bright 1 second
 
  Serial.begin(115200);
  while (!Serial); //If just the the basic function, must connect to a computer
 
  SPI.begin(5,19,27,18);
  LoRa.setPins(SS,RST,DI0);
//  Serial.println("LoRa Sender");
 
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
 
  pinMode(16,OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50);
  digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
 
  display.init();
  display.setContrast(255);
}
void fillRect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<DISPLAY_HEIGHT/2; i+=3) {
    display.setColor((color % 2 == 0) ? BLACK : WHITE); // alternate colors
    display.fillRect(i, i, DISPLAY_WIDTH - i*2, DISPLAY_HEIGHT - i*2);
    display.display();
    delay(10);
    color++;
  }
  // Reset back to WHITE
  display.setColor(WHITE);
}
 
unsigned long previousMillis = 0;
 
void loop() {
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    display.clear();
    display.setColor(WHITE); // alternate colors
    display.drawString(0, 0, String(counter,DEC));
    display.drawString(0, 20, String(++counter2,DEC));
    display.drawString(0, 40, String(LoRa.packetRssi(),DEC));
    display.display();
    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }
 
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
 
 
  unsigned long currentMillis = millis();
 
    if (currentMillis - previousMillis >= 2000) {
      previousMillis = currentMillis;
      // send packet
      Serial.print("Sending packet: ");
      Serial.println(counter);
      LoRa.beginPacket();
      LoRa.print("hello ");
      LoRa.print(counter);
      LoRa.endPacket();
      display.clear();
      display.setColor(WHITE); // alternate colors
      display.drawString(0, 0, String(counter++,DEC));
      display.drawString(0, 20, String(counter2,DEC));
      display.drawString(0, 40, String(LoRa.packetRssi(),DEC));
      display.display();
 
    }
}

Empfänger

receiver.cpp
// This example just provide basic LoRa function test;
// Not the LoRa's farthest distance or strongest interference immunity.
// For more informations, please vist www.heltec.cn or mail to support@heltec.cn
 
#include <SPI.h>
#include <LoRa.h>
#include<Arduino.h>
#include "SSD1306.h "
// WIFI_LoRa_32 ports
 
// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)
 
#define SS      18
#define RST     14
#define DI0     26
#define BAND    833E6  //915E6 -- 这里的模式选择中,检查一下是否可在中国实用915这个频段
 
int counter = 0;
 
SSD1306  display(0x3c, 4, 15);
 
 
void setup() {
  pinMode(25,OUTPUT); //Send success, LED will bright 1 second
 
  Serial.begin(115200);
  while (!Serial); //If just the the basic function, must connect to a computer
 
  SPI.begin(5,19,27,18);
  LoRa.setPins(SS,RST,DI0);
//  Serial.println("LoRa Sender");
 
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
 
  pinMode(16,OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50);
  digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
 
  display.init();
  display.setContrast(255);
 
  // register the receive callback
  //LoRa.onReceive(onReceive);
 
  // put the radio into receive mode
  //LoRa.receive();
}
void fillRect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<DISPLAY_HEIGHT/2; i+=3) {
    display.setColor((color % 2 == 0) ? BLACK : WHITE); // alternate colors
    display.fillRect(i, i, DISPLAY_WIDTH - i*2, DISPLAY_HEIGHT - i*2);
    display.display();
    delay(10);
    color++;
  }
  // Reset back to WHITE
  display.setColor(WHITE);
}
void loop() {
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    display.clear();
    display.setColor(WHITE); // alternate colors
    display.drawString(0, 0, String(counter++,DEC));
    display.drawString(0, 20, String(LoRa.packetRssi(),DEC));
    display.display();
    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }
    Serial.println("Send back");
    LoRa.beginPacket();
    LoRa.print("ACK ");
    LoRa.print(counter);
    LoRa.endPacket();
 
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}