LoRa Duplex Communication Using Bharat Pi

by bharatpi in Circuits > Arduino

593 Views, 0 Favorites, 0 Comments

LoRa Duplex Communication Using Bharat Pi

Screenshot 2024-03-11 132547.png

The Bharat Pi Lora Duplex combines the functionalities of the ESP32 microcontroller and LoRa technology, making it suitable for various long-range, low-power communication projects.

Supplies

Bharat Pi LoRa Module

DumbDisplay

How to Connect DumbDisplay

DumbDisplay Android app is a simple Bluetooth Classic / Bluetooth LE / USB Serial Terminal (monitor / plotter), extended to act as a "dumb" display (as well as a little bit of inputting means) for Arduino / ESP8266 / ESP32 / Raspberry Pi Pico -- using the DumbDisplay Arduino library (https://github.com/trevorwslee/Arduino-DumbDisplay)

The idea of using DumbDisplay as a display is that you can defer connecting real output gadgets to your microcontroller board until later stage of your experiment.

The microcontroller board can connect to the DumbDisplay Android app via

• USB (OTG)

• Bluetooth (with HC-05 / HC-06)

• WIFI

• USB via DDWifiBridge

DumbDisplay app can be connected to Bluetooth of the LoRa board via Android device bluetooth

LoRa Duplex Communication

LoRa technology:

LoRa is an RF modulation technology for low-power, wide area networks (LPWANs). The name, LoRa, is a reference to the extremely long-range data links that this technology enables. created by Semtech to standardize LPWANs, LoRa provides for long-range communications: up to three miles (five kilometers) in urban areas, and up to 10 miles (15 kilometers) or more in rural areas (line of sight). A key characteristic of the LoRa-based solutions is ultra-low power requirements, which allows for the creation of battery-operated devices that can last for up to 10 years. Deployed in a star topology, a network based on the open LoRaWAN protocol is perfect for applications that require long-range or deep in-building communication among a large number of devices that have low power requirements and that collect small amounts of data.

Full Duplex Communication:

Full-duplex communication allows devices to transmit and receive data simultaneously. This mode enables more efficient communication between devices, reducing latency and potentially increasing throughput. However, achieving full-duplex communication in LoRa networks can be challenging due to the nature of the technology and its focus on low-power, long-range communication. While LoRa itself supports full-duplex communication, it may not be commonly implemented due to power constraints and the need for more complex hardware.


Code

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


const int ss = 4;          // LoRa radio chip select
const int rst = 14;        // LoRa radio reset
const int dio0 = 2;        // change for your board; must be a hardware interrupt pin


String outgoing;            // outgoing message


byte msgCount = 0;          // count of outgoing messages
byte localAddress = 0xEE;   // address of this device for LoRa
byte destination = 0xAA;    // destination to send to for LoRa
long lastSendTime = 0;      // last send time for LoRa
int interval = 1000;        // interval between LoRa sends


String device_name = "abcd";


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run make menuconfig and enable it
#endif


#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif


BluetoothSerial SerialBT;


void setup() {
  Serial.begin(115200);                   // initialize serial
  SerialBT.begin(device_name);             // Bluetooth device name
  Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());


  // override the default CS, reset, and IRQ pins (optional)
  LoRa.setPins(ss, rst, dio0);            // set CS, reset, IRQ pin


  if (!LoRa.begin(866E6)) {               // initialize LoRa at 867 MHz
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                         // if failed, do nothing
  }


  Serial.println("LoRa init succeeded.");
}


void loop() {
  handleBluetooth();  // Handle Bluetooth communication
  handleLoRa();       // Handle LoRa communication
}


void handleBluetooth() {
  if (Serial.available()) {
    String message = Serial.readString();
    sendMessage(message);  // Send Bluetooth message to LoRa
    Serial.println("Sending " + message);
  }


  if (SerialBT.available()) {
    char data = SerialBT.read();
    delay(10);
    LoRa.beginPacket();                   // start packet
    LoRa.write(destination);              // add destination address
    LoRa.write(localAddress);             // add sender address
    LoRa.write(msgCount);                 // add message ID
    LoRa.write(1);                        // add payload length (1 for single character)
    LoRa.print(data);                     // add payload
    LoRa.endPacket();                     // finish packet and send it
    msgCount++;                           // increment message ID
  }
}


void handleLoRa() {
  // parse for a packet, and call onReceive with the result:
  onReceive(LoRa.parsePacket());


  // Additional LoRa handling if needed
}


void sendMessage(String outgoing) {
  LoRa.beginPacket();                   // start packet
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress);             // add sender address
  LoRa.write(msgCount);                 // add message ID
  LoRa.write(outgoing.length());        // add payload length
  LoRa.print(outgoing);                 // add payload
  LoRa.endPacket();                     // finish packet and send it
  msgCount++;                           // increment message ID
}
String incomingMessage = "";


void onReceive(int packetSize) {
  if (packetSize == 0) return;


  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  byte incomingMsgId = LoRa.read();     // incoming msg ID
  byte incomingLength = LoRa.read();    // incoming msg length


  while (LoRa.available()) {
    incomingMessage += (char)LoRa.read();
  }


  if (incomingMessage.endsWith("\n")) {
    // Remove newline character
    incomingMessage.trim();
    // if the recipient isn't this device or broadcast,
    if (recipient != localAddress && recipient != 0xFF) {
      // Additional processing for LoRa messages if needed
      return;
    }


    // if message is for this device, or broadcast, print details:
    String message = "Message: " + incomingMessage + "\n";
    Serial.print(message);
    SerialBT.write((uint8_t*)message.c_str(), message.length());
    incomingMessage = ""; // Clear the buffer for the next message
  }
}

Applications

Bharat Pi Lora Module with ESP32 is a development board designed for prototyping applications based on LoRa technology. LoRa stands for Long Range and is a Low Power Wide Area Network (LPWAN) protocol ideal for Internet of Things (IoT) projects requiring long-range data communication with low power consumption.

Here are some potential applications for Bharat Pi Lora Duplex:

  • Smart metering: It can be used to build remote monitoring systems for utilities like electricity, water, and gas meters. This allows for automatic meter reading, reducing manual intervention and enabling real-time usage tracking
  • Wireless sensor networks: Bharat Pi Lora Duplex can be used to create sensor networks for various applications like environmental monitoring (temperature, humidity, air quality), agriculture (soil moisture, crop health), and asset tracking (inventory management, logistics).
  • Industrial automation: In industrial settings, the board can be used for monitoring and control of machines and processes in remote locations. This can be helpful for applications like pressure monitoring in pipelines or temperature control in warehouses.
  • Smart cities: LoRa technology is well-suited for smart city applications due to its long range and low power capabilities. Bharat Pi Lora Duplex can be used to develop solutions for smart parking, waste management, and traffic monitoring.

Video

LoRa duplex using Bharat pi.#iot #bharatpi #lora