Arduino

아두이노간 LoRa 통신 : 숫자 실수 솔신과 수신하기

작성자 임베디드코리아 작성일25-05-25 00:43 조회111회 댓글0건

첨부파일

< LoRㅁ 통신 - 실수를 송신과 수신 *>
- 정수 부분과 소수 부분을 분리하여 전송하고
- 수신하여 결합한다.

 -------- < Lora 통신  실수 송신 소스 코드 > --------------------------------
#include <SPI.h>
#include <LoRa.h>

int counter = 0;
float number = 105.65;

byte high_byte;
byte low_byte;

void setup() {
  Serial.begin(9600);
  Serial.println("LoRa Sender");

  if (!LoRa.begin(915E6)) {      // initialize ratio at 915 MHz
    Serial.println("LoRa init failed. Check your connections.");
    while(true);                //if failed, do nothing
  }

}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // 정수부와 소수부를 추출한다.
  high_byte = (int)(number*100)/256;
  low_byte = (int)(number*100)%256;

  // 정수부와 소수부를 송신한다.
  LoRa.beginPacket();
  LoRa.write(high_byte);
  LoRa.write(low_byte);
  LoRa.endPacket();
 
  delay(3000);

}

 -------- < Lora 통신  실수 수신 소스 코드 > --------------------------------
#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(9600);
  Serial.println("LoRa Receiver");

  if (!LoRa.begin(915E6)) {      // initialize ratio at 915 MHz
    Serial.println("LoRa init failed. Check your connections.");
    while(true);                //if failed, do nothing
  }

}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize > 0) {
    Serial.print("Received packet '");    // 수신된 데이터 크기를 packetSize에 저장
    Serial.println(packetSize);          //수신된 데이터가 존재 한다.

    // read packet
    byte num1 = LoRa.read();
    byte num2 = LoRa.read();
    float number = (num1*256+num2)/100.0;
    Serial.println(number);
   
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}