Hello friends, I am creating a code for a sensor that uses an Arduino as a microcontroller, I am going to make three sensors and I want all three to start sending me data, when I sent them the signal to do so from the computer.
For that I thought that simply putting:
void loop() {
delay (500);
if ((XBeeSerial.read()) != -1);{
Serial.println(XBeeSerial.read());
It was going to work, but no. I printed the value to see if it gave me -1 and indeed it gave me -1 but I was very surprised. Since I put an IF conditional for it to be fulfilled. But it seems not.
The most interesting thing was to see that after the code continued running like this in a loop, I began to notice that each time it printed a different value. Which I feel is possible since it may be the transmit Status from an ACK signal sent by the xbee from the computer indicating that the message was delivered.
I don't really know why it's wrong, and I don't know why it doesn't respect the conditional.
This is the full code.
//Librerias
#include <SafeString.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>
//#include <SPI.h>
//#include <SD.h>
//DataLogeer
//const int chipSelect = 4;
//Xbee
SoftwareSerial XBeeSerial(2, 3); //(Rx, TX)
//ADC
//ADC--> const int ADC = A0;
//float voltPanel;
//TRAMAS
byte packet1[] = {0x7E, 0x00, 0x21}; //Byte delimitador y Longitud de trama // variar por el voltaje
byte packet2[] = {0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xD7, 0xAE, 0xAD, 0xFF, 0xFE, 0x00, 0x00,}; //Id de trama, tipo de trama, direccion de 64bits, opciones y numero de brincos.
// 00:11:46 05/01/2021 == 19 chars
byte fullPacket[3 + 14 + 19 + 1];// (Delimitador + longitud )+ (tipo + direccion)+ voltage + fecha + checksum = 37
// [3 + 14 + 4 + 19 + 1]
char buffer [25]; // 19 la fecha +1 '\0', 25 bytes de tamaƱo // cambiar porque hay voltaje
#define DEBUG SafeString::Output
//Setup
void setup() {
Serial.begin(9600);
delay(100);
XBeeSerial.begin(9600);
delay(100);
// if (!SD.begin(4)) {
// Serial.println("initialization failed!");
//while (1);
//}
//Serial.println("initialization done.");
delay(2000);
Serial.println();
Serial.println("MACHOTE DEL CODIGO PRINCIPAL");
Serial.println("-------------------");
SafeString::setOutput(Serial);
}
void loop() {
delay (500);
if ((XBeeSerial.read()) != -1);{
Serial.println(XBeeSerial.read());
tmElements_t tm;
if (RTC.read(tm)) {
sprintf(buffer, ",%02d:%02d:%02d,%02d/%02d/%04d", tm.Hour , tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
Serial.println(buffer); // con este print compruebo que la fecha y hora
//Extraer Voltaje de ADC
//Convertir voltaje a Valor REAL
//voltPanel= ((float(analogRead(ADC))*5.0/1023)*3+25, 2); //Aqui multiplico por 5 y divido por 1023,
//para tener el voltaje que lee el ADC y multiplo por 3 y umo 25, para dar el voltaje real.
//convertir voltPanel a HEX
//Ingresar este valor a los datos, parte de la horayfecha (xx.xx,00:11:46,05/01/2021)
//Actualizar length y las tramas de arriba.
//{0x7E, 0x00, 0x27}; //0x27 19 caracteres de fecha + 4bytes del flotante + espacio
//DATALOGGER
// build fullpacket
size_t idx = 0;
memmove(fullPacket + idx, packet1, sizeof(packet1));
idx += sizeof(packet1);
memmove(fullPacket + idx, packet2, sizeof(packet2));
idx += sizeof(packet2);
if ((idx + 19 + 1) > sizeof(fullPacket)) {
// Si el tamaƱo es incorrecto
Serial.println(F("fullPacket size wrong"));
}
memmove(fullPacket + idx, buffer, 19);
idx += 19;
// dejar un byte para el cheksum
if ((idx + 1) != sizeof(fullPacket)) {
// fullPacket size wrong
Serial.println(F("fullPacket size wrong"));
}
int chksum = 0;
for (size_t i = 3; i < idx; i++) { // comienza despues del tamaƱo dela trama
chksum += fullPacket[i]; // comienza a sumar
}
chksum = (0xff & chksum);
chksum = (0xff) & (0xff - chksum);
fullPacket[idx] = chksum;
idx++; // El tamao total de la trama
if (idx != sizeof(fullPacket)) {
Serial.println(F("fullPacket size error"));
}
// Imprime el paquete
size_t printSize = idx * 3; // ..<space>
cSF(sfPacket, printSize);
for (size_t i = 0; i < idx; i++) {
//sfPacket += "0x";
if (fullPacket[i] < 16) {
sfPacket += '0';
}
sfPacket.print(fullPacket[i], HEX);
sfPacket.print(' ');
}
Serial.println(sfPacket);
XBeeSerial.write (fullPacket , idx);
delay(5000);
}
}
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}