Post a new topicPost a reply Page 1 of 1   [ 9 posts ]
Author Message
 Post subject: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Mon Jun 04, 2018 9:43 am 

Joined: Mon Jun 04, 2018 9:34 am
Posts: 15
Hello,Im absolute begginer and i want some help.Well, i have 2 arduino Uno with 2 lorawan RN2483A module to each.I succesfully start a communication p2p to each other from examples library that I download from you site about LoRaWAN_P2P_01_configure_module,LoRaWAN_P2P_02_send_packets and LoRaWAN_P2P_03_receive_packets but now thats I want is to put a temperature sensor and from one arduino take the value of the temperature sensor and sending to other arduino to prin it on serial monitor.From that i read My sensor data type is float but if I want to send the sensor data, I need to convert data into ASCII text, right? how to convert float to ASCII text? And I also want to know that how to covert the ASCII text to its original sensor data?

Thank you


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Tue Jun 05, 2018 10:11 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11311
Hi,

You must convert from float or decimal to string and then from string to hexadecimal string. Here you can see and example from waspmote.

12.5894 -> "12.5894" -> "31322e35383934"

Code:
float temp;
char float_str[10];
char message[50];
char payload_hex[100];

  dtostrf( temp, 1, 2, float_str);
 
  // use snprintf() to include the float representation
  snprintf( message, sizeof(message), "%s", float_str);
  USB.println( message );

  Utils.hex2str((uint8_t*)message, payload_hex, strlen(message));


Code:
void WaspUtils::hex2str(uint8_t* number, char* macDest, uint8_t length)
{
   uint8_t aux_1=0;
   uint8_t aux_2=0;

   for(int i=0;i<length;i++)
   {
      aux_1=number[i]/16;
      aux_2=number[i]%16;
      if (aux_1<10)
      {
         macDest[2*i]=aux_1+'0';
      }
      else{
         macDest[2*i]=aux_1+('A'-10);
      }
      if (aux_2<10){
         macDest[2*i+1]=aux_2+'0';
      }
      else{
         macDest[2*i+1]=aux_2+('A'-10);
      }
   }
   macDest[length*2]='\0';
}


Regards


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Tue Jun 05, 2018 10:32 am 

Joined: Mon Jun 04, 2018 9:34 am
Posts: 15
But Im using arduino uno with libraries for arduino not waspmote it have difference in code?For example i dont use USB. how can i write this to arduino?Im asking because im new and dont know so much.And the this code you send me i will copy paste it to example of lorawan send packets right?its only to convert float to hexa not the opposite?


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Tue Jun 05, 2018 10:40 am 

Joined: Mon Jun 04, 2018 9:34 am
Posts: 15
And what is the final value which is hexa that I will be send with the command LoRaWAN.sendRadio() to the arduino receiver? my code for sending packet without function radioModuleSetup() just to see it.Also what changes I must to do to receive packet code to take the value of temp and print it to Serial monitor?
Code:
#include <Wire.h>

// Cooking API libraries
#include <arduinoUART.h>
#include <arduinoUtils.h>

// LoRaWAN library
#include <arduinoLoRaWAN.h>

// Pin definition for LoRaWAN module error LED:
const int error_led =  13;
float temp;

//////////////////////////////////////////////
uint8_t socket = SOCKET1;
//////////////////////////////////////////////

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


// define data to sen
char data[]="";

// variable
uint8_t error;


void setup()
{
  Serial.begin(57600);
  //Radio P2P example - Sending packets
  pinMode(error_led, OUTPUT);
  digitalWrite(error_led, LOW);
  pinMode(A0, INPUT);

  // module setup
  error = radioModuleSetup();
 
  // Check status
  if (error == 0)
  {
    //Module configured OK
    digitalWrite(error_led, LOW);
  }
  else
  {
    //Module configured ERROR"
    digitalWrite(error_led, HIGH);
  } 
}


void loop()
{


  temp = analogRead(A0) * 5 / 1024.0;
  temp = temp - 0.5;
  temp = temp / 0.01;
 



  error = LoRaWAN.sendRadio(data);
 
  // Check status
  if (error == 0)
  {
    //--> Packet sent OK
    digitalWrite(error_led, LOW);
    Serial.println("packet go");
  }
  else
  {
    //Error waiting for packets. error
    digitalWrite(error_led, HIGH);
    Serial.println("packet didnt go");
   
   
  }
 
  delay(5000);
}


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Wed Jun 06, 2018 8:19 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11311
Hi,

We are sorry but we can't develop a specific code, we just can send some example in order to help to develop your code.

To show data received you have to develop the inverse way, convert from hex string to ascii string. These are the functions for Waspmote, surely you can find another functions for Arduino or implment that with Arduino
Code:
/*
 * Function: Converts a string to an hex number
 *
 */
uint8_t WaspUtils::str2hex(char* str)
{
    int aux=0, aux2=0;

    if( (*str>='0') && (*str<='9') )
    {
        aux=*str++-'0';
    }
    else if( (*str>='A') && (*str<='F') )
    {
        aux=*str++-'A'+10;
    }
    else if( (*str>='a') && (*str<='f') )
    {
        aux=*str++-'a'+10;
    }
    if( (*str>='0') && (*str<='9') )
    {
        aux2=*str-'0';
    }
    else if( (*str>='A') && (*str<='F') )
    {
        aux2=*str-'A'+10;
    }
    else if( (*str>='a') && (*str<='f') )
    {
        aux2=*str-'a'+10;
    }
    return aux*16+aux2;
}



/*
 * Function: Converts a string to an array of bytes
 * For example: If the input string is defined as:
 *       char input[] = "";
 * The output string is array -> 23576173706D6F74655F50726F23
 */
uint16_t WaspUtils::str2hex(char* str, uint8_t* array)
{      
    // get length in bytes (half of ASCII characters)
   uint16_t length=strlen(str)/2;
   
    // Conversion from ASCII to HEX   
    for(uint16_t j=0; j<length; j++)
    {   
      array[j] = Utils.str2hex(&str[j*2]);     
    }
   
   return length;
}





/*
 * Function: Converts a string to an array of bytes
 */
uint16_t WaspUtils::str2hex(char* str, uint8_t* array, uint16_t size)
{      
    // get length in bytes (half of ASCII characters)
   uint16_t length = strlen(str)/2;   
   
    // Conversion from ASCII to HEX   
    for(uint16_t j=0; j<length; j++)
    {   
      // check size of array
      if (j >= size)
      {
         length = j;
         break;
      }
      
      // store conversion in array
      array[j] = Utils.str2hex(&str[j*2]);           
    }
   
   return length;
}


/*
 * Function: Converts a string to an hex number
 *
 */
uint8_t WaspUtils::str2hex(uint8_t* str)
{
   int aux=0, aux2=0;
   
   
   if( (*str>='0') && (*str<='9') )
   {
      aux=*str++-'0';
   }
   else if( (*str>='A') && (*str<='F') )
   {
      aux=*str++-'A'+10;
   }
   if( (*str>='0') && (*str<='9') )
   {
      aux2=*str-'0';
   }
   else if( (*str>='A') && (*str<='F') )
   {
      aux2=*str-'A'+10;
   }
   return aux*16+aux2;
}


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Mon Jun 11, 2018 3:35 pm 

Joined: Mon Jun 04, 2018 9:34 am
Posts: 15
Code:
float temp;
char float_str[10];
char message[50];
char payload_hex[100];

  dtostrf( temp, 1, 2, float_str);
 
  // use snprintf() to include the float representation
  snprintf( message, sizeof(message), "%s", float_str);
  USB.println( message );

  Utils.hex2str((uint8_t*)message, payload_hex, strlen(message));

You send me that about to convert float value to string first and then to hexa and its worked correclty.So what it is the opposite of that?if i want to convert hexa to string and then to float?Sorry for asking again but im confused and i dont understand that you send me with waspmote

Thank you for your help


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Tue Jun 12, 2018 7:59 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11311
Hi,

In the receiver side, you should convert from hex string to ascii string to know the content of the message. You can use str2hex() function to do that.

Regards


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Wed Jun 13, 2018 11:38 am 

Joined: Mon Jun 04, 2018 9:34 am
Posts: 15
Code:

 *  ------ P2P Code Example --------
 * 
 *  Explanation: This example shows how to configure the module
 *  for P2P mode and the corresponding parameters. After this,
 *  the example shows how to receive packets from other radio modules
 *  which must be set with the same radio settings
 * 
 *  Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 * 
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *   
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *   
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Version:           0.3
 *  Design:            David Gascon
 *  Implementation:    Luismi Marti, Ruben Martin
 */

#include "arduPiLoRaWAN.h"

// socket to use
//////////////////////////////////////////////
uint8_t sock = SOCKET0;
//////////////////////////////////////////////

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

uint8_t*buffer1[30];
uint16_t*size;


//////////////////////////////////////////////

// define functions
uint8_t radioModuleSetup(void);

// variable
uint8_t error;


void setup()
{
  printf("Radio P2P example - Receiving packets\n\n");

  // module setup
  error = radioModuleSetup();
 
  // Check status
  if (error == 0)
  {
    printf("Module configured OK\n");     
  }
  else
  {
    printf("Module configured ERROR\n");     
  } 

}


void loop()
{
 
  printf("\nListening to packets...\n");
 
   // rx
  error = LoRaWAN.receiveRadio(10000);
 
 
 
  // Check status
  if (error == 0)
   
  {
   
   
    printf("--> Packet received ");
   
    printf("packet: %s\n", (char*) LoRaWAN._buffer);
    printf("length: %u\n", LoRaWAN._length);
   
    Utils.str2hex((char*)LoRaWAN._buffer, buffer1, sizeof(LoRaWAN._buffer));
    printf("output ascii");
    printf((char*)buffer1);
  }
  else
  {
    // error code
    //  1: error
    //  2: no incoming packet
    printf("Error waiting for packets. error = %d\n", error); 
  } 
}

can you tell me where is the error here and the compile tell me LoRaWAN_P2P_03_receive_packets2.cpp: In function ‘void loop()’:
LoRaWAN_P2P_03_receive_packets2.cpp:100:5: error: ‘Utils’ was not declared in this scope
Utils.str2hex((char*)LoRaWAN._buffer, buffer1, sizeof(LoRaWAN._buffer));


Top
 Profile  
 
 Post subject: Re: 2 Arduino uno communication with LoraWAN RN2483A
PostPosted: Thu Jun 14, 2018 8:03 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11311
Hi,

Utils is a library for Wapmote that we shared with us as an example. You have to develop that for Arduino or try to copy the content of the function in you code to perform the conversion.

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 9 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.