Waspmote发送/接收

时间:2017-12-15 14:50:30

标签: c++ arduino lorawan

我是Waspmote的新手,为一堂课做了一些项目。任务是通过LoRaWAN发送/接收数据。我尝试了默认示例,并注意到我只能通过LoRaWAN发送十六进制。我无法发送“你好世界”#39;例如。有谁知道如何配置从ASCII发送所有字符?

#include <WaspLoRaWAN.h>

//////////////////////////////////////////////
uint8_t socket = SOCKET0;
//////////////////////////////////////////////

// define radio settings
//////////////////////////////////////////////
uint8_t power = 15;
uint32_t frequency;
char spreading_factor[] = "sf12";
char coding_rate[] = "4/5";
uint16_t bandwidth = 125;
char crc_mode[] = "on";
//////////////////////////////////////////////
// define data to send
char data[] = "0102030405060708090A0B0C0D0E0F";

// variable
uint8_t error;

void setup() {
  USB.ON();
  USB.println(F("Radio P2P example - Sending packets\n"));

  // module setup
  error = radioModuleSetup();

  // Check status
  if (error == 0) {
    USB.println(F("Module configured OK"));     
  } else {
    USB.println(F("Module configured ERROR"));     
  }  
}

void loop() {
  // Send packet
  error = LoRaWAN.sendRadio(data);

  // Check status
  if (error == 0) {
    USB.println(F("--> Packet sent OK"));
  } else {
    USB.print(F("Error waiting for packets. error = "));  
    USB.println(error, DEC);   
  }
  delay(5000);
}

当我更改data[]时,它可以正常工作,但只能使用十六进制字符。

接下来是接收代码:

#include <WaspLoRaWAN.h>

//////////////////////////////////////////////
uint8_t socket = SOCKET0;
//////////////////////////////////////////////

// define radio settings
//////////////////////////////////////////////
uint8_t power = 15;
uint32_t frequency;
char spreading_factor[] = "sf12";
char coding_rate[] = "4/5";
uint16_t bandwidth = 125;
char crc_mode[] = "on";
//////////////////////////////////////////////

// variable
uint8_t error;

void setup() {
  USB.ON();
  USB.println(F("Radio P2P example - Receiving packets\n"));

  // module setup
  error = radioModuleSetup();

  // Check status
  if (error == 0) {
    USB.println(F("Module configured OK"));     
  } else {
    USB.println(F("Module configured ERROR"));     
  }  
}

void loop() {
  USB.println(F("\nListening to packets..."));

   // rx
  error = LoRaWAN.receiveRadio(10000);

  // Check status
  if (error == 0) {
    USB.println(F("--> Packet received"));
    USB.print(F("packet: "));
    USB.println((char*) LoRaWAN._buffer);
    USB.print(F("length: "));
    USB.println(LoRaWAN._length);

    // get SNR 
    LoRaWAN.getRadioSNR();
    USB.print(F("SNR: "));
    USB.println(LoRaWAN._radioSNR);
  } else {
    // error code
    //  1: error
    //  2: no incoming packet
    USB.print(F("Error waiting for packets. error = "));  
    USB.println(error, DEC);   
  }  
}

1 个答案:

答案 0 :(得分:2)

你需要转换你想要的字符串&#34; hello world&#34;到十六进制字符串。

转换功能:

bool string_to_hexstring(char *string, char *hexstring, uint16_t hexstring_length)
{
    uint16_t hexstring_index = 0;
    while(string[hexstring_index/2] != 0 && hexstring_index < hexstring_length-2)
    {
        char c = string[hexstring_index/2];
        byte nib1 = (c >> 4) & 0x0F;
        byte nib2 = (c >> 0) & 0x0F;
        hexstring[hexstring_index++] = nib1  < 0xA ? '0' + nib1  : 'A' + nib1  - 0xA;
        hexstring[hexstring_index++] = nib2  < 0xA ? '0' + nib2  : 'A' + nib2  - 0xA;
    }
    hexstring[hexstring_index++] = 0;
    return string[hexstring_index/2] == 0;
}

稍后在您的代码中:

char buffer[32]; // needs to be at least two times the string size + 1
if (!string_to_hexstring("hello world", buffer, sizeof(buffer))) {
    // buffer size too small
}
error = LoRaWAN.sendRadio(buffer);