LoRa RFM shield + Ultrasonic sensor

Bluetooth, XBee, RF......

LoRa RFM shield + Ultrasonic sensor

Postby kelvinswee94 » Fri Dec 15, 2017 1:19 am

hi, i was trying to connect my LoRa RFM shield with an ultrasonic sensor(act as client) and i want to send notification from client to the server when there is an object come close to the ultrasonic sensor. the object is detected but i was unable to send notification to the server
here is my coding for my client :
// LoRa 9x_TX
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messaging client (transmitter)
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example LoRa9x_RX
Code: Select all
#include <SPI.h>
#include <RH_RF95.h>

#define RFM95_CS 10
#define RFM95_RST 7
#define RFM95_INT 2
#define node_id "B"

// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
const int trigPin = 9;
const int echoPin = 11;

const int ledPin = 13;

long duration;
int distance;
int safetyDistance;
void setup()
{
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);


  while (!Serial);
  Serial.begin(9600);

  delay(100);

  Serial.println("Arduino LoRa TX Test!");

  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);


  while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  }
  Serial.println("LoRa radio init OK!");

  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  }
  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);

  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(ledPin, OUTPUT);

}

int16_t packetnum = 0;  // packet counter, we increment per xmission

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2;

  safetyDistance = distance;
  if (safetyDistance <= 10) {

    digitalWrite(ledPin, HIGH);
    Serial.print("Distance: ");
    Serial.println(distance);

   

    {

      Serial.println("Sending to rf95_server");
      // Send a message to rf95_server

      String radiopacket = "data to you #";
      radiopacket += String(packetnum++);
      radiopacket += " from ";
      radiopacket += node_id;

      Serial.print("Sending ");
      Serial.println(radiopacket); delay(10);
      rf95.send((uint8_t*)(radiopacket.c_str()), radiopacket.length() + 1);

      Serial.println("Waiting for packet to complete..."); delay(10);
      rf95.waitPacketSent();
      // Now wait for a reply
      uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
      uint8_t len = sizeof(buf);

      Serial.println("Waiting for reply...");
      delay(10);
      if (rf95.waitAvailableTimeout(1000))
      {
        // Should be a reply message for us now
        if (rf95.recv(buf, &len))
        {
          Serial.print("Got reply: ");
          Serial.println((char*)buf);
          Serial.print("RSSI: ");
          Serial.println(rf95.lastRssi(), DEC);
        }
        else
        {
          Serial.println("Receive failed");
        }
      }
      else
      {
        Serial.println("No reply, is there a listener around?");
      }
      delay(10000);
    }


  }
  else {

    digitalWrite(ledPin, LOW);

  }

  // Prints the distance on the Serial Monitor

}



but i got the output (serial monitor)like this :

Arduino LoRa TX Test!
LoRa radio init OK!
Set Freq to: 915.00
Distance: 6
Sending to rf95_server
Sending data to you #0 from B
Waiting for packet to complete...

the serial monitor stuck at the waiting there
any idea what is the problem?
thanks
kelvinswee94
Novice
 
Posts: 16
Joined: Wed Nov 15, 2017 4:19 pm

Re: LoRa RFM shield + Ultrasonic sensor

Postby bengchet » Fri Dec 15, 2017 4:06 pm

Hi,

LoRa shield uses SPI to operate, so please make sure reserve pin 11 - 13 as well for the purpose. So don't initialise pin 11 for ultrasonic sensor echo pin and pin 13 for LED. Find other pins instead for ultrasonic sensor echo pin and LED.

Hope it helps thanks.
bengchet
Moderator
 
Posts: 235
Joined: Tue Aug 25, 2015 8:29 am

Re: LoRa RFM shield + Ultrasonic sensor

Postby kelvinswee94 » Fri Dec 15, 2017 5:13 pm

bengchet wrote:Hi,

LoRa shield uses SPI to operate, so please make sure reserve pin 11 - 13 as well for the purpose. So don't initialise pin 11 for ultrasonic sensor echo pin and pin 13 for LED. Find other pins instead for ultrasonic sensor echo pin and LED.

Hope it helps thanks.


hi, thanks for the solution, it works, but another problem comes next. i was unable to notify the server. do i need to change something in my server code?

here is the server code:
Code: Select all
#include <SPI.h>
#include <RH_RF95.h>

#define RFM95_CS 10
#define RFM95_RST 7
#define RFM95_INT 2

// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

void setup()
{   
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  while (!Serial);
  Serial.begin(9600);
  delay(100);

  Serial.println("Arduino LoRa RX Test!");
 
  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);

  while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  }
  Serial.println("LoRa radio init OK!");

  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  }
  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);

  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);
}

void loop()
{
  if (rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
   
    if (rf95.recv(buf, &len))
    {
      RH_RF95::printBuffer("Received: ", buf, len);
      Serial.print("Got: ");
      Serial.println((char*)buf);
      Serial.print("RSSI: ");
      Serial.println(rf95.lastRssi(), DEC);
     
      // Send a reply
      uint8_t data[] = "received with thanks";
      rf95.send(data, sizeof(data));
      rf95.waitPacketSent();
      Serial.println("data received");
    }
    else
    {
      Serial.println("Receive failed");
    }
  }
}

thanks
kelvinswee94
Novice
 
Posts: 16
Joined: Wed Nov 15, 2017 4:19 pm

Re: LoRa RFM shield + Ultrasonic sensor

Postby bengchet » Fri Dec 15, 2017 9:34 pm

Hi,

Please continue this topic as a new issue/topic because previous problem was solved. At the same time, also attach some photo setup (server & client), serial monitor output for easier troubleshooting. Thanks for your cooperation.
bengchet
Moderator
 
Posts: 235
Joined: Tue Aug 25, 2015 8:29 am


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 11 guests