Hi Drmpf, Im no going to Ask anything with Xbee and Softwareserial.
In addition to the time and date, I want to send the value of a voltage that I am going to measure with the Arduino's ADC.
So I did this, I saw some ADC tutorials, I initialized the A0 port where my analog signal is going to enter and then I initialized a variable called voltPanel, this variable is going to contain the real value of the voltage that I am reading.
Then to be able to intrododuce in the data frame, use the same procedure that I saw:
//FRAME
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, String(voltPanel), sizeof(String(voltPanel)));
idx += sizeof(voltPanel); //HERE
memmove(fullPacket + idx, buffer, 20);
idx += 20;
// dejar un byte para el cheksum
if ((idx + 1) != sizeof(fullPacket)) {
// fullPacket size wrong
Serial.println(F("fullPacket size wrong"));
}
This is the actual 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)
uint8_t Rx_nextByte;
#define START_DELIMITER 0x7E
//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 == 20 chars
byte fullPacket[3 + 14 + 2 + 20 + 1];// (Delimitador + longitud )+ (tipo + direccion)+ voltage + fecha + checksum = 37
// [3 + 14 + 2 + 20 + 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);
XBeeSerial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
Serial.println();
Serial.println("Programacion de ATMega328:");
SafeString::setOutput(Serial);
}
void loop() {
Serial.println("Espera a recibir seƱal");
while (XBeeSerial.available()) {
Rx_nextByte = XBeeSerial.read();
if (Rx_nextByte == START_DELIMITER){
Serial.println("SeƱal recibida");
//RTC
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
//ADC
//Convertir voltaje a Valor REAL
voltPanel= ((float(analogRead(ADC))*5.0/1023)*6+10, 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 en el encabezado length y las tramas de arriba.
//{0x7E, 0x00, 0x27}; //0x27 19 caracteres de fecha + 4bytes del flotante + espacio
//DATALOGGER
String dataString = "";
dataString += String(voltPanel);
dataString += String(buffer);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
//FRAME
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, String(voltPanel), sizeof(String(voltPanel)));
idx += sizeof(voltPanel);
memmove(fullPacket + idx, buffer, 20);
idx += 20;
// 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);
}