1

I am a Master student and in my project I have to collect data from different types of sensors and send it to the cloud, I have done this with the DH11 sensor using existing examples and it worked well, but when I wanted to change the sensor and send the Ultrasonic HC-SR04 data to the cloud via the LoRa gateway it doesn't work , the data collected by the sensor is detected by the Arduino board and sent to the gateway, but I can't use it or even read it. Here is the code I used

LoRa Gateway :

#include <SPI.h>
#include <RH_RF95.h>
#include <Console.h>
#include <Process.h>
RH_RF95 rf95;
#define BAUDRATE 115200
float frequency = 868.0;

void setup() {
 Bridge.begin(BAUDRATE);
 Console.begin();
if (!rf95.init())
    Console.println("init failed");

// Setup ISM frequency
rf95.setFrequency(frequency);
// Setup Power,dBm
rf95.setTxPower(13);
rf95.setSyncWord(0x34);
}






void loop() {


 if (rf95.waitAvailableTimeout(2000)) {
      String K="";
        uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];//receive data buffer
        uint8_t len = sizeof(buf);//data buffer length
        if (rf95.recv(buf, &len))//Check if there is incoming data
        {
            //recdata( buf, len);
            Console.print("Get LoRa Packet: ");
            for (int i = 0; i < len; i++)
            {
                Console.print(buf[i],HEX);
                K=K+(buf[i],HEX);
                Console.print(" ");
            }
            Console.println();
               for (int i = 0; i < len; i++)
                {
                    X = X+buf[i];
                }
                Console.println(X);
            Console.println("K :");  
            Console.println(K);
            K="";

            for (int i = 0; i < len; i++)
                {
                    String((char *)buf[i]);
                }

                Console.println(K);


        }
     }
}

Arduino board : enter image description here

/*HC-SR04_TEST :
    Use SR04 ultro sonic sensor to measure distance and show it on the serial monity

  Hardware Connection:
   Trig < -- > Arduino D3
   Echo pin < -- > Arduino D4
   VCC < -- > Arduino 5v
   GND < -- > Arduino GND

  by Dragino <support@dragino.com>
  Dragino Technology Co., Limited
*/

    #include <SPI.h>
    #include <RH_RF95.h>

RH_RF95 rf95;
byte bGlobalErr;
char sr04_dat[3];  
// Set up SR04 pins to Arduino
const int TrigPin = 3; 
const int EchoPin = 4; 
float duration, distance,  frequency = 868.0; 
unsigned int count = 1;

void setup() 
{   // initial serial connection
        Serial.begin(9600); 
        pinMode(TrigPin, OUTPUT); 
    // Set up pin to input mode to detect wave width. 
        pinMode(EchoPin, INPUT); 

        if (!rf95.init())
        Serial.println("init failed");
    // Setup ISM frequency
        rf95.setFrequency(frequency);
    // Setup Power,dBm
        rf95.setTxPower(13);
        rf95.setSyncWord(0x34);

        Serial.println("Ultrasonic sensor:");
} 

void ReadSR04()
{
    bGlobalErr=0;
    byte sr04_in;
    byte i;

    //pinMode(dht_dpin,OUTPUT);
    //digitalWrite(dht_dpin,LOW);//Pull Low A0 and send signal
    //delay(30);//Delay > 18ms so DHT11 can get the start signal


    digitalWrite(TrigPin, LOW); 
    delayMicroseconds(2); 
    digitalWrite(TrigPin, HIGH); 
    delayMicroseconds(10);
    digitalWrite(TrigPin, LOW); 

    //digitalWrite(dht_dpin,HIGH);
    //delayMicroseconds(40);//Check the high level time to see if the data is 0 or 1
    //pinMode(dht_dpin,INPUT);
    // delayMicroseconds(40);
    //dht_in=digitalRead(dht_dpin);//Get A0 Status
    //   Serial.println(dht_in,DEC);
    //if(dht_in){
     //   bGlobalErr=1;
     //   return;
    //}
    //delayMicroseconds(80);//DHT11 send response, pull low A0 80us
    //dht_in=digitalRead(dht_dpin);


     duration = pulseIn(EchoPin, HIGH);
     distance = duration*0.034/2;


    //if(!dht_in){
     //   bGlobalErr=2;
      //  return;
    //}


    delayMicroseconds(80);//DHT11 send response, pull low A0 80us


};


void loop() 
{ 
        Serial.print("###########    ");
        Serial.print("COUNT=");
        Serial.print(count);
        Serial.println("    ###########");
        count++;
        ReadSR04();


        Serial.print("Distance : "); 
        Serial.print(distance); 
        Serial.print("cm"); 
        Serial.println(); 


        String mystring;
        byte leng;
        int i;
        mystring = String(distance);
        leng = sizeof(mystring);
        char data[sizeof(mystring)];
       for(i = 0;i < (sizeof(mystring)); i++)
        {
          data[i] = mystring[i];
        }

        rf95.send(data, leng);//Send LoRa Data

        uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];//Reply data array
    uint8_t len = sizeof(buf);//reply data length

    if (rf95.waitAvailableTimeout(3000))// Check If there is reply in 3 seconds.
    {
        // Should be a reply message for us now   
        if (rf95.recv(buf, &len))//check if reply message is correct
       {
               pinMode(13, OUTPUT);
               digitalWrite(13, HIGH);
               Serial.print("Got Reply from Gateway: ");//print reply
               Serial.println((char*)buf);

               delay(400);
               digitalWrite(13, LOW); 
               //Serial.print("RSSI: ");  // print RSSI
               //Serial.println(rf95.lastRssi(), DEC);        

        }
        else
        {
           Serial.println("recv failed");//
           rf95.send(data, strlen((char*)data));//resend if no reply
        }
    }
    else
    {
        Serial.println("No reply, is LoRa gateway running?");//No signal reply
        rf95.send(data, strlen((char*)data));//resend data
    }


        delay(3000); 
        Serial.println("");
}

This picture contains the screeshot of the serial monitor of the sensor + Arduino board (COM5) and the serial monitor of the LoRa gateway (dragino board)enter image description here

  • the two windows in the bottom picture show almost the same data – jsotola Feb 15 '19 at 5:32
  • Please do not add screenshots of console data, copy it into the questions as formatted text instead, – Bence Kaulics Feb 15 '19 at 20:41
  • Hello jsotola, actually one of the two windows is the sender (LoRa node : Ultra Sonic HC-SR04 +Arduino board+ LoRa shield) and the second is the receiver(LoRa Getway) and i want it to be the same data recieved but it shows me the packet in the hexa format and i am not able to manupulate the packet and show the same sent value in the reciever's console. – تقوى عمري - Takwa OMRI Feb 19 '19 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.