Store string of data in SD card received over serial.

Hi Guys,

I need to store bit of data received over serial cable ...which needs to be stored on SD card. Size of data is 1kb.

Similarly, the other way round, I need to read data from SD card and then transmit the same over serial.

I am able to create a .txt file...however, the file is empty, maybe the controller is not able to simultaneously read/write data over serial and also write/read data on SD card.

Can anyone direct me how this can be done?

Post your code that attempts to do what you want and fails

One part tries to capture data from serial and try to write it on SD card. Other part simply transmits data over serial.

nimit393:

/*

*/

#include <SD.h>
#include <SPI.h>

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

File myFile;

int pinCS = 4; // Pin 10 on Arduino Uno
uint16_t i = 0;
char a[512];

void setup() {
 mySerial.begin(57600);
 Serial.begin(57600);
 pinMode(pinCS, OUTPUT);

// SD Card Initialization
 if (SD.begin())
 {
   Serial.println("SD card is ready to use.");
 } else
 {
   Serial.println("SD card initialization failed");
   return;
 }

// Reading the file
 myFile = SD.open("123.txt");
 if (myFile) {
   Serial.println("Read:");
   // Reading the whole file
   while (myFile.available()) {
     Serial.write(myFile.read());
     //Serial.write(mySerial.read());
     //myFile.write(mySerial.read());
   }
   myFile.close();
 }

myFile = SD.open("456.txt", FILE_WRITE);
 if (myFile) {
   Serial.println("Read:");
   
   // Reading the whole file
   while (myFile.available()) {
     //    myFile.print(mySerial.read());
//if (mySerial.available() > 0)
 {
   //myFile.print(mySerial.read());
   myFile.print("1234567");
   Serial.write(myFile.read());
   myFile.close(); // close the file
   }
   }}
 else {
   Serial.println("error opening 123.txt");
 }

}
void loop() {
 // empty
/* if (mySerial.available() > 0)
 {
   //Serial.println(mySerial.read(),HEX);
   a[i] = mySerial.read();
   //Serial.println(a,HEX);
   i++;

}*/
  //Serial.println(a[100],HEX);
 //Serial.println(a);
 delay(3000);

}






#include <Adafruit_Fingerprint.h>

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 12);

uint8_t P[] = {
0xEF,
0x01,
0xFF,
0xFF,
0xFF,
0xFF,
0x02,
0x00,
0x82,
0x03,
0x01,
0x52,
0x30,
0x6C,
0x00,
0xFF,
0xFE,
0xFF,
0xFE,
0x81,
0xFE,
0x00,
0x3E,
0x00,
0x1E,
0x00,
0x1E,
0x00,
0x0E,
0x00,
0x0E,
0x00,
0x0E,
0x00,
0x0E,
0x00,
0x06,
0x00,
0x06,
0x00,
0x06,
0x00,
0x06,
0x80,
0x06,
0x80,
0x06,
0x80,
0x0E,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x36,
0x0E,
0xA2,
0x96,
0x1E,
0x26,
0x8B,
0x3E,
0x08,
0xAF,
0x22,
0x7E,
0x30,
0x40,
0xC8,
0x7E,
0x22,
0x43,
0x87,
0xB6,
0x55,
0x18,
0x8A,
0xBF,
0x11,
0x19,
0x0C,
0x7F,
0x27,
0x1A,
0x0B,
0x9F,
0x09,
0x27,
0x4C,
0x5F,
0x65,
0x3C,
0x61,
0xDF,
0x14,
0x41,
0xA0,
0x7F,
0x5D,
0x1F,
0x8B,
0x3D,
0x2A,
0x9E,
0x0A,
0xBA,
0x66,
0xB4,
0x8B,
0xBA,
0x35,
0x37,
0x49,
0xFA,
0x25,
0x38,
0x48,
0xDA,
0x34,
0xA7,
0x49,
0x1B,
0x30,
0x27,
0xCA,
0x1B,
0x22,
0xE9};

uint8_t Q[]={
0xEF,
0x01,
0xFF,
0xFF,
0xFF,
0xFF,
0x02,
0x00,
0x82,
0x3C,
0xB7,
0x89,
0x5B,
0x60,
0x30,
0xA1,
0x18,
0x2E,
0xB7,
0x60,
0x18,
0x53,
0x38,
0xA1,
0xD8,
0x5A,
0xBC,
0x4B,
0xF2,
0x5A,
0x3F,
0x22,
0x72,
0x34,
0x16,
0x49,
0xD9,
0x27,
0xBB,
0xC9,
0x73,
0x28,
0x3F,
0x87,
0xF9,
0x64,
0xB0,
0x8B,
0x36,
0x2C,
0x3B,
0x9E,
0x36,
0x46,
0x14,
0x62,
0x37,
0x31,
0x1C,
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0xFF,
0x01,
0x00,
0x04,
0x09,
0x01,
0x00,
0x0F};

uint8_t U[]={
0xEF,
0x01,
0xFF,
0xFF,
0xFF,
0xFF,
0x01,
0x00,
0x06,
0x06,
0x01,
0x00,
0x10,
0x00,
0x1E};

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
 
 Serial.begin(57600);
 while (!Serial);  // For Yun/Leo/Micro/Zero/...
 finger.begin(57600);

for (uint8_t i = 0; i < 13; i++)
 {
   mySerial.write(T[i]);
 }

delay(1000);

for (uint8_t i = 0; i < 139; i++)
 {
   mySerial.write(P[i]);
 }

delay(1000);

for (uint8_t i = 0; i < 139; i++)
 {
   mySerial.write(Q[i]);
 }

delay(1000);

for (uint8_t i = 0; i < 139; i++)
 {
   mySerial.write(R[i]);
 }

delay(1000);

for (uint8_t i = 0; i < 139; i++)
 {
   mySerial.write(S[i]);
 }
delay(1000);

for (uint8_t i = 0; i < 15; i++)
 {
   mySerial.write(U[i]);
 }

delay(1000);
mySerial.end();
}

void loop()                     // run over and over again
{

//mySerial.write(B);
 //delay(1000);
}

@OP

From you sketches, it appears that you are learning how to receive data from UNO-2 over the software UART Port and then write the data into a SD Memory Card attached with UNO-1. If so, look for tutorials on SD Memory Card read/write and serial communication between two UNOs.

A: Tutorial-1: Simple read/write with SD Memory Card using UNO
unoSD-1.png

#include<SPI.h>   //SDC Card uses SPI Bus
#include<SD.h>    //contains library functions

#define CSPIN 10   //
File myFile;      //file pointer variable declaration

void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  SD.begin(CSPIN);        //SD Card is initialized
  SD.remove("Test1.txt"); //remove any existing file with this name
  myFile = SD.open("Test1.txt", FILE_WRITE);  //file created and opened for writing

  if(myFile)        //file has really been opened; myFile should be non=-zero
  {
    myFile.println("Ahsanullah University"); //the nul-byte is auo saved at the end
    myFile.close();

  }
   myFile = SD.open("Test1.txt", FILE_READ);  //file created and opened for writing

  if(myFile)        //file has really been opened
  {
    while(myFile.available())   //read charcater from SD card until null-byte is found
    {
      Serial.print((char)myFile.read()); //reads character by charcater upto null-byte
    }
    myFile.close();
  }
}

void loop()
{
  
}

B: Tutorial-2: Simple data read/write between two UNOs using software serial port (SUART)
under development

unoSD-1.png

What I want is the exact complement of the code posted by you. Read something which has been received over serial and save the same on SD card.
Reading SD card and transmitting the same over serial has been achieved.

I am posting two sketches below by virtue of which two UNOs exchange data over software serial port (SUART Port). The UNO-1 is sending the float type temperature data with 0x05 as a terminating byte. The UNO-2 receives the data and saves in an array. You may practice these setup as a tutorial and then apply the methodology to achieve your objectives.

Transmission Codes

//sender_ code
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //SRX, STX

int pin = 0;
int data;
char dataBuffer[20] = "";

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
  analogReference(INTERNAL);
}

void loop()
{
  float tempC = (float)100 * (1.1 / 1024.0) * analogRead(A0); //
  Serial.print("Pressure data = ");//
  Serial.println(tempC, 2);
 
  mySUART.print(tempC); //sends 8-bit ASCII code for each digit/symbol of tempC
 
  mySUART.write(0x05);
  delay(1000);
}

Reception Codes

//sender_ code
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //SRX, STX

int data;
char dataBuffer[20] = "";
int i = 0;


void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  if (mySUART.available() > 0)
  {
    byte x = mySUART.read();
    if (x == 0x05)
    {
      Serial.println(": This data is from SUART Port");
      Serial.println();
      Serial.print("Pressure data from Buffer = ");
      Serial.println(dataBuffer);
      Serial.println("====================");
      i = 0;
    }
    else
    {
      dataBuffer[i] = x;
      Serial.write((char)dataBuffer[i]);
      i++;
    }
  }
}
#include <SPI.h>
#include <SD.h>
int r;
byte b;

const int chipSelect = 4;
void setup() {
  // put your setup code here, to run once:
  //int r;
  Serial.begin(57600);
  Serial3.begin(57600);
  Serial.print("This is a test and should be ignored");
   if (!SD.begin(chipSelect)) {
    Serial.println("\nCard failed, or not present");
    // don't do anything more:
    return;
  }
  else{
  Serial.println("\ncard initialized.");

  File OpenFile = SD.open("abc.txt", FILE_WRITE);
         OpenFile.println(1);
       OpenFile.close();
}
}

void printHex2Digit(byte b)
{
  if (b <= 0xF) Serial.print("0");
  Serial.print(b, HEX);
//  OpenFile.println(b);

}

void loop() 
{
  // put your main code here, to run repeatedly
File OpenFile = SD.open("abc.txt", FILE_WRITE);
  if(OpenFile and Serial3.available());
  { 
    int r=Serial3.read();
    if (r!= -1)
       {
       //Serial.write(r);
       printHex2Digit(r);
       OpenFile.println(r);
       OpenFile.close();
       }
    }
}

Trying to read data over serial and write the same in SD card...serial data is visible on serial monitor, but nothing gets stores in SD card.
Also is am receiving data in HEX format (commands for another device). I need to again send out the same values I store on SD card in HEX format.
EF01FFFFFFFF01000606010010001E is example of one such command.

If you already another device sending the serial data it may be worth having a look at this,

All the information is there to build one along with the code, I use it all the time takes the headache out of doing it and supports upto 32Gb card.

There is an error in your program with the semi colon after the conditional statement

//if(OpenFile and Serial3.available());
if(OpenFile and Serial3.available())

The multiple declarations of the file object are not required and may be creating problems.

The open and close of the file with each read/write in loop is slow, and you are better of leaving the file open until all the data is written, but its probably not the cause of your problem.

#include <SPI.h>
#include <SD.h>
int r;
byte b;

File OpenFile;

const int chipSelect = 4;
void setup() {
  // put your setup code here, to run once:
  //int r;
  Serial.begin(57600);
  Serial3.begin(57600);
  Serial.print("This is a test and should be ignored");
  if (!SD.begin(chipSelect)) {
    Serial.println("\nCard failed, or not present");
    // don't do anything more:
    return;
  }
  else {
    Serial.println("\ncard initialized.");

    //File OpenFile = SD.open("abc.txt", FILE_WRITE);
    OpenFile = SD.open("abc.txt", FILE_WRITE);
    OpenFile.println(1);
    OpenFile.close();
  }
}

void printHex2Digit(byte b)
{
  if (b <= 0xF) Serial.print("0");
  Serial.print(b, HEX);
  //  OpenFile.println(b);

}

void loop()
{
  // put your main code here, to run repeatedly
  // File OpenFile = SD.open("abc.txt", FILE_WRITE);
  OpenFile = SD.open("abc.txt", FILE_WRITE);
  //error with ; after if statement
  //if (OpenFile and Serial3.available());
  if (OpenFile and Serial3.available())
  {
    int r = Serial3.read();
    if (r != -1)
    {
      //Serial.write(r);
      printHex2Digit(r);
      OpenFile.println(r);
      OpenFile.close();
    }
  }  
}

@nimit393, do not cross-post. Other thread locked.

Trying to read the attached document and send the values over serial port to other arduino. The values received are

300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A330D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A310D0A300D0A340D0A390D0A310D0A300D0A31350D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A320D0A300D0A3133300D0A330D0A310D0A38320D0A34380D0A3130380D0A300D0A3235350D0A3235340D0A3235350D0A3235340D0A3132390D0A3235340D0A300D0A36320D0A300D0A33300D0A300D0A33300D0A300D0A31340D0A300D0A31340D0A300D0A31340D0A300D0A31340D0A300D0A360D0A300D0A360D0A300D0A360D0A300D0A360D0A3132380D0A360D0A3132380D0A360D0A3132380D0A31340D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A300D0A3136300D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A320D0A300D0A3133300D0A36300D0A3138330D0A3133370D0A39310D0A39360D0A34380D0A3136310D0A32340D0A34360D0A3138330D0A39360D0A32340D0A38330D0A35360D0A3136310D0A3231360D0A39300D0A3138380D0A37350D0A3234320D0A39300D0A36330D0A33340D0A3131340D0A35320D0A32320D0A37330D0A3231370D0A33390D0A3138370D0A3230310D0A3131350D0A34300D0A36330D0A3133350D0A3234390D0A3130300D0A3137360D0A3133390D0A35340D0A34340D0A35390D0A3135380D0A35340D0A37300D0A31340D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A320D0A300D0A3133300D0A330D0A310D0A38340D0A35300D0A3130360D0A300D0A3235350D0A3235340D0A3235350D0A3235340D0A3232350D0A3235340D0A3139320D0A3235340D0A300D0A3235340D0A300D0A3132360D0A300D0A36320D0A300D0A33300D0A300D0A33300D0A300D0A31340D0A300D0A31340D0A300D0A31340D0A300D0A360D0A300D0A360D0A300D0A360D0A300D0A360D0A3139320D0A31340D0A300D0A300D0A300D0A300D0A300D0A300D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A380D0A300D0A3133300D0A33380D0A3135300D0A3133380D0A3135350D0A35340D0A3136350D0A3136310D0A3235310D0A36310D0A3136390D0A3230320D0A3231390D0A36340D0A34350D0A3136310D0A3132330D0A36300D0A35330D0A3133390D0A3132330D0A35330D0A36340D0A33320D0A32370D0A36300D0A32310D0A3136310D0A3132300D0A38330D0A33360D0A31330D0A3135320D0A36350D0A3137370D0A39390D0A32340D0A39310D0A3139300D0A3230300D0A3231360D0A37310D0A35300D0A3130300D0A3234390D0A36350D0A3233370D0A3233390D0A310D0A3235350D0A3235350D0A3235350D0A3235350D0A310D0A300D0A360D0A360D0A310D0A300D0A31360D0A300D0A33300D0A

However, I am expecting HEX equivalent of each of the decimal values stored in the text file.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include <SPI.h>
#include <SD.h>
char r;
int i;
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  mySerial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("ABCDEF.txt", FILE_WRITE);

  // re-open the file for reading:
  myFile = SD.open("ABCDEF.txt");
  if (myFile) {
    Serial.println("ABCDEF.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      r=myFile.read();
      i=atoi(r);
      Serial.write(r);
      mySerial.write(r);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening ABCDEF.txt");
  }
}

void loop() {
  // nothing happens after setup

  //mySerial.write("1");
}

[code]

can you please help me out?

ABCDEF.TXT (1.02 KB)

Please provide the code which stored the file you attached.

However, I am expecting HEX equivalent of each of the decimal values stored in the text file.

You're actually getting that, but the output formatting is confusing you. The 0D0A are the Hex representations of CR/LF. Please reference an ASCI table.

300D0A = "0" with a CR/LF, so your data is starting with a string of 0's. It's unclear why the file begins like this.

At some point after this you get 3233390D0A310D0A3235350D0A3235350D0A....

With some added spaces for clarity you have the first 4 values of your list.

32 33 39 0D0A = 239 CR/LF
31 0D0A = 1 CR/LF
32 35 35 0D0A = 255 CR/LF
32 35 35 0D0A = 255 CR/LF

and the last 3 values are

31360D0A = 16
300D0A = 0
33300D0A= 30

What is your output with Serial.print() instead of Serial.write()?

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include <SPI.h>
#include <SD.h>
int i;
String s;
char a[1]={16};
int b=20;
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  mySerial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("ABCDEF.txt", FILE_WRITE);

  // re-open the file for reading:
  myFile = SD.open("ABCDEF.txt");
  if (myFile) {
    Serial.println("ABCDEF.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      i=myFile.read();
      
      if (isDigit(i)) {
      // convert the incoming byte to a char and add it to the string:
      s += (char)i;
    }

    if (i == '\n') {
      Serial.println(s.toInt(),HEX);
      mySerial.println(s.toInt(),HEX);
      s = "";
    }
    
      //Serial.write(r);
      //mySerial.write(r);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening ABCDEF.txt");
  }
}

void loop() {
  // nothing happens after setup
  //mySerial.write("1");
}

I modified my code a bit....now I am getting correct HEX values on serial monitor of the same arduino....however the serial data received on the other arduino is still the same as posted earlier..which is the incorrect one.

however the serial data received on the other arduino is still the same as posted earlier

Please post the code for the receiving Arduino.

#define fingerSerial Serial3

void setup() {
  Serial.begin(115200);
  fingerSerial.begin(57600);
  delay(1000);  // one second delay to let the sensor 'boot up'
}

void printHex2Digit(byte b)
{
  if (b <= 0xF) Serial.print("0");
  Serial.print(b, HEX);
}

void loop() {
  static unsigned long tSerial = 0;
  static unsigned long tFinger = 0;

  while (Serial.available()) {
    int r = Serial.read();
    if (millis() - tSerial >= 100) Serial.print(F("\nSending to Sensor: "));
    tSerial = millis();
    printHex2Digit((byte) r);
    if (r != -1) fingerSerial.write((byte) r);
  }

  while (fingerSerial.available()) {
    if (millis() - tFinger >= 100) Serial.print(F("\nReceived from Sensor: "));
    tFinger = millis();
    int r = fingerSerial.read();
    if (r != -1) printHex2Digit((byte) r);
  }
}

Understood my mistake...should had used Serial.write instead of Serial.print

String s;

Use of the String object class can create memory leaks and problems for Arduino's. It is preferable to use null terminated character arrays and the functions which use them. I have replaced the String which holds the reading from the file with a character array, and used with the .readBytesUntil() function to fill it.

Here's the SD read, print and send routine which takes the decimal integer file of the format xxx\r\n.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include <SPI.h>
#include <SD.h>
//String s;
char readBuffer[5];//size for 3 digits + \r\n ;
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  mySerial.begin(9600);//software serial is not reliable at high baud rates
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("ABCDEF.txt", FILE_WRITE);

  // re-open the file for reading:
  myFile = SD.open("ABCDEF.txt");
  if (myFile) {
    Serial.println("ABCDEF.txt:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      byte numChars = myFile.readBytesUntil('\n', readBuffer, 5); //3 digits \r\n max read
      readBuffer[numChars - 1] = '\0'; //replace \r with null terminator
      int fileValue = atoi(readBuffer);//convert the read characters to integer
      Serial.println(fileValue, HEX);

      //send original file entry out over software serial
      mySerial.print(fileValue); //will send decimal fileValue as chars
      mySerial.write('\r');
      mySerial.write('\n');

    }
    myFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening ABCDEF.txt");
  }
}

void loop() {}

I'm confused by the receiving Arduino code, and I'm not sure which on channel the date is being received, but you should be able to adopt the reading method to mirror what I've posted here since it sends the data in the original format.