Buongiorno, ho un problema con questo codice:
#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: ";
char* buffer1;
char* buffer2;
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);
buffer1 = concat(data, temp);
// latitudine
dtostrf(flat, precision+1, precision+4, temp);
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;
}
L'arduino esegue tre loop (tre invii di pacchetti), poi si resetta, mentre a volte ne esegue quattro e crasha o inizia a dare problemi con i messaggi inviati (che vengono stampati anche su seriale per testarlo).
Cosa potrebbe essere il problema? Sto usando un Waspmote SX1272 per inviare i dati, quindi sono obbligato a inviare char* e a convertire i float in char*....
Grazie in anticipo,
Simone Garuglieri