Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 1   [ 2 posts ]
Author Message
 Post subject: Sending Float using Libelium LoRa SX1272
PostPosted: Thu Jun 08, 2017 12:13 pm 

Joined: Thu Jun 08, 2017 12:10 pm
Posts: 1
i found big problems sending floats using SX1272. I thought to convert them into char* because that's what sendPacketTimeout wants as argument, but the board crashes and resets after three sending using this code:
Code:
#include <arduinoUtils.h>
#include "arduinoLoRa.h"
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>

float InterceptDO = -0.33;
float SlopeDO = 3.27;

int e;
char message1[60];
char temp[10];
char lat1[] = "\nLAT: ";
char lon1[] = "\t LON: ";
char data[] = "Dissolved Oxygen: ";
byte precision = 2;

int count = 0;

TinyGPS gps;
SoftwareSerial ss(2, 3);
bool newData = false;
float flat, flon;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  ss.begin(9600);

  // Power ON the module
  sx1272.ON();
  // Set transmission mode and print the result
  e = sx1272.setMode(1);
  Serial.println(F("Setting Mode: state "));
  Serial.println(e, DEC);
  // Select frequency channel
  e = sx1272.setChannel(CH_13_868);
  Serial.println(F("Setting Channel: state "));
  Serial.println(e, DEC);
  // Select output power (Max, High or Low)
  e = sx1272.setPower('M');
  Serial.println(F("Setting Power: state "));
  Serial.println(e, DEC);
  // Set the node address and print the result
  e = sx1272.setNodeAddress(2);
  Serial.println(F("Setting node address: state "));
  Serial.println(e, DEC);
  // Print a success message
  Serial.println(F("SX1272 successfully configured"));
}

void loop(void)
{
  //////////////////////////
  // GPS
  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat;
    flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon;
  }

  ////////////////////////
  // DO PROBE
  float CountDO = analogRead(A0);
  float VoltageDO = CountDO / 1023 * 5.0;// convert from count to raw voltage
  float SensorReadingDO= InterceptDO + VoltageDO * SlopeDO; //converts voltage to sensor reading

////////////////////////////
  // LORA
  // dati ossigeno
  dtostrf(SensorReadingDO, precision+1, precision, temp);
  char* buffer1 = concat(data, temp);
  // latitudine
  dtostrf(flat, precision+1, precision+4, temp);
  char* buffer2 = concat(lat1, temp);
  buffer1 = concat(buffer1, buffer2);
  // longitudine
  dtostrf(flon, precision+1, precision+4, temp);
  buffer2 = concat(lon1, temp);
  buffer1 = concat(buffer1, buffer2);

  sprintf(message1, buffer1);
 
  //INVIO
  e = sx1272.sendPacketTimeout(3, message1);
  Serial.println(message1);
  Serial.print(F("Packet sent, state "));
  Serial.println(e, DEC);   

  delete buffer1;
  delete buffer2;
  count++;
 
  Serial.println(count);

  delay(5000);
}
char* concat(const char *s1, const char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    //in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}


What do you think could be the problem?


Top
 Profile  
 
 Post subject: Re: Sending Float using Libelium LoRa SX1272
PostPosted: Fri Jun 09, 2017 8:07 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hello Garu94,
This kind of errors are caused by a failure in memory heap allocation or pointers handle.
Ensure that yours pointers are correctly allocated into the heap. Try this code:
Code:
char* concat(const char *s1, const char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    if (result != NULL)
    {
      //in real code you would check for errors in malloc here
      strcpy(result, s1);
      strcat(result, s2);
    }
    return result;
}


Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 2 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.