如何使用 Arduino 将 7Ultrasonic HC-SR04 数据从 LoRa 节点发送到 LoRa 网关?

物联网 劳拉
2021-06-17 17:50:51

我是一名硕士生,在我的项目中,我必须从不同类型的传感器收集数据并将其发送到云端,我使用现有示例使用 DH11 传感器完成了这项工作,并且效果很好,但是当我想更改传感器时并通过LoRa网关将Ultrasonic HC-SR04数据发送到云端它不起作用,传感器收集的数据被Arduino板检测并发送到网关,但我无法使用它甚至无法读取它. 这是我使用的代码

LoRa网关:

#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板: 在此处输入图片说明

/*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("");
}

此图包含传感器+Arduino板(COM5)的串口监视器和LoRa网关(dragino板)的串口监视器截图在此处输入图片说明

0个回答
没有发现任何回复~