0

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 <[email protected]>
  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

3
  • the two windows in the bottom picture show almost the same data
    – jsotola
    Feb 15, 2019 at 5:32
  • Please do not add screenshots of console data, copy it into the questions as formatted text instead, Feb 15, 2019 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. Feb 19, 2019 at 9:17

1 Answer 1

1

I know, this is an old issue, however as this is on the internet time is not key I suppose. Sent data on the Arduino is exactly the same as received on the LoRa GW.

Use any hex to ascii converter of your choice to confirm, e.g. https://www.rapidtables.com/convert/number/hex-to-ascii.html

Sent data is a float converted to char (single ASCII characters).

Sent data in ASCII: 1534.0

Sent data in hex: 31 35 33 34 2e 30 (e.g. 31 = ASCII character "1", 35 = ASCII character "5"... 2E = ASCII character ".",...)

As you're sending the length of your string the last character is missing (extend your loop by 1 to transmit also the last digit/ASCII "5").

Important: LoRa is all about keeping the payload as low as possible to reduce TX time thus network usage. With the above code you're converting a float number to single ASCII characters and thus your payload (= sent data) is 7 bytes long (for 1534.05). This is a lot and uses power, especially on battery powered devices, and more important it uses a lot of TX time on the network.

You could instead use only one single byte. This is more than enough. Convert your float number (1534.05). This is in millimeters. The ultra sonic (US) sensor has an accuracy of 1 cm. So goint down to the 1/100th of a mm is overkill. 1 cm values is enough: convert the float/10 to an integer (1534.05/10 = 153.405 -> 153 cm).

As the max range of the US sensor is 3 cm to 3 Meters (=3...300 [cm]) your almost within one byte (max value 256). If this is enough for your setup then send the int over LoRa as one byte. If this is not enough then screw the cut off and go for two bytes and Millimeters (= 3,000 mm, max value with 2 bytes 65,535).

On the GW side (more precisely on the LoRaWAN application/endnode side) set up an upload payload decoder to show the value of this byte in decimal (instead of hex). Examples for payload decoders are given within the TTN help. Or use the forum, e.g. https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.