Ada Bae
Published

Simple Ultrasonic and Water Level Sensor With LoRa RFM95

DIY LoRa rfm95 simple Gateway to send and receive messages on the LoRa network!

BeginnerWork in progress339
Simple Ultrasonic and Water Level Sensor With LoRa RFM95

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LoRa Rfm95w
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Water Level Sensor
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering
Wire Cutter / Stripper, 5.25 " Overall Length
Wire Cutter / Stripper, 5.25 " Overall Length

Story

Read more

Schematics

Tx_LoRa

Rx_LoRa

Code

Tx_LoRa

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

//Ultrasonic sensor
#define TRIGGER_PIN  3  
#define ECHO_PIN     4 
#define MAX_DISTANCE 200

//Water level sensor
#define sensor A0

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int distance;

int dummyValue;
long randNumber; //Create Random Number To Avoid Transmission Loss For First Digit

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial);

  if (!LoRa.begin(915E6)){
    Serial.println("Starting LoRa Failed!");
    while(1);
    }else{
      Serial.println("LoRa Sender!");
      }
}

void loop() {
  // put your main code here, to run repeatedly:
  randNumber = random(1000);

  int randNumber = random(100); dummyValue = randNumber;
  int wlvl = analogRead(sensor);
  int distance = sonar.ping_cm();

  Serial.println(wlvl);
  Serial.println(distance);

  if (isnan(wlvl)){
    Serial.println(F("Failed to read from Water Level sensor!"));
    return;
    }
  if (isnan(distance)){
    Serial.println(F("Failed to read from Ultrasonic sensor!"));
    }

  String dataString = String(dummyValue) + (";") + String(wlvl) + (";") + String(distance);

  Serial.println(dataString);
  LoRa.beginPacket();
  LoRa.print(dataString);
  LoRa.endPacket();

  delay(100);
}

Rx_LoRa

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

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  if (!LoRa.begin(915E6)){
    Serial.println("Starting LoRa failed!");
    while(1);
    }else{
      Serial.println("LoRa Receiver!");
      }
}

void loop() {
  // put your main code here, to run repeatedly:
  String packet = "";
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    Serial.print("Received packet :-- ");
      while (LoRa.available()){
      packet = LoRa.readString();
      }
    
    Serial.println(packet);
    Serial.println();

    int firstcommaIndex   = packet.indexOf(';');
    int secondCommaIndex  = packet.indexOf(';', firstcommaIndex+1);
    int thirdCommaIndex   = packet.indexOf(';', secondCommaIndex+1);
    int fourthCommaIndex  = packet.indexOf(';', thirdCommaIndex+1);

    String firstValue  =  packet.substring( 0, firstcommaIndex);
    String secondValue =  packet.substring(firstcommaIndex+1, secondCommaIndex);
    String thirdValue  =  packet.substring(secondCommaIndex+1, thirdCommaIndex);

    Serial.print("Wlvl:-"); Serial.println(secondValue);
    Serial.print("Sonar:-"); Serial.println(thirdValue);
    Serial.println();
  }
}

Credits

Ada Bae

Ada Bae

1 project • 0 followers

Comments