char array to String?

Hello,

I am building a project to function with Blynk, and in order to send my RTC readings along to the app, I first collected the readings into a char array

      char timeStamp[11];
      char dateStamp[11];
      sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
      sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());

but I was advised that I can't pass along the char array. So now I need to convert both arrays into 2 separate Strings. Is this possible? If yes, can someone give me a link that depicts how, or show me in this thread?

Thank you in advance!

Hello, have you tried:

String s;
s = String(timestamp);

Paul

Thanks Paul, I just tried that a few different ways, but no go. It is more than likely misuse of Blynk code, I just wanted to know if the arrays could easily convert to Strings, and I'm confident that what you just shared with me is how it is done. So thanks a million!

myggle:
I was advised that I can't pass along the char array.

What do you mean by this? Pass it where; to another function, the Serial port?

but I was advised that I can't pass along the char array.

What id-ten-t advised that?

Delta_G:
There is nothing that you can do with String that you can't do with a char array.

Well, there is one thing. You can make Swiss cheese of your memory using a String, while you can't do that with a string.

Apologies for the delayed reply. Paul's original suggestion panned out for me. I guess the way the Blynk app/library works is that it can't handle 2 char arrays in the same "lcd.print" command, but once the 2 arrays are converted to Strings, it somehow becomes easier to feed across their server and onto the app on my smartphone. I realize I may have sacrificed some memory, but it is a necessary evil I guess. Here is my sketch;

      #include <SPI.h>
      #include <Ethernet.h>
      #include <BlynkSimpleEthernet.h>
      #include <Wire.h>                 
      #include "DHT.h"                
      #include <SimpleTimer.h>          
      #include "RTClib.h" // RealTimeClock Library for DS1307 and DS3231
      #define BLYNK_PRINT Serial
      #include <SPI.h>
    
//*******Sensor Model********************************    
      #define DHTTYPE DHT22
      #define DHTPIN 13 

      RTC_DS1307 RTC;
      float UTCOffset = -5.0;    // Your timezone relative to UTC (http://en.wikipedia.org/wiki/UTC_offset)

      char auth[] = "AuthKey";

      DHT dht(DHTPIN, DHTTYPE);
      byte h;  
      byte f;  
                
      SimpleTimer timer;
    
      WidgetLCD lcdA(V2);  //Set LCD widget Input to PUSH

    void setup() 
    {
      Serial.begin(9600);  
      //Serial2.begin() // For other baud rates
      //Serial3.begin() // For other baud rates          
      Blynk.begin(auth);
      pinMode(DHTPIN, OUTPUT);           
      dht.begin();

      RTC.adjust(DateTime(__DATE__, __TIME__)); 
      RTC.begin();   
     
      while (Blynk.connect() == false) {}

      timer.setInterval(5000L, climateCheck); // 5 second intervals between DHT readings
      timer.setInterval(3000L, RTCdisplay); // 3 second intervals between RTC readings
    }
      
    void loop() 
    {
      Blynk.run();
      timer.run();
    }
    
    void climateCheck()
    {
      h = dht.readHumidity();
      f = dht.readTemperature(true);

      Blynk.virtualWrite(V0, f);    //  Set Virtual Pin 0 frequency to PUSH in Blynk app
      Blynk.virtualWrite(V1, h);      //  Set Virtual Pin 1 frequency to PUSH in Blynk app
      //Serial.print(f);
      //Serial.print(h);
    }
    
    void RTCdisplay()
    { 
      DateTime now = RTC.now();  // reads time at beginning of loop
  
      byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
      byte zeroHour = 12;                // Variable use to convert "0" zero hour to display it as 12:00+
      byte displayHour;
      byte MIN = now.minute();
      byte SEC = now.second();
      char* meridian;
  
      if (now.hour() == 0)  // First we test if the hour reads "0"
    { 
      displayHour = zeroHour;
      meridian = "AM";         
    }
      else if (now.hour() >= 13)  // if no, Second we test if the hour reads "13 or more"
    { 
      displayHour = twelveHour;
      meridian = "PM";      
    }
      else 
    { 
      displayHour = now.hour();
      meridian = "AM"; 
    }
     
      char timeStamp[11];
      char dateStamp[11];
      sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
      sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
      String ts;
      String ds;
      ts = timeStamp;
      ds = dateStamp;
       
      lcdA.clear();                   //Advanced Mode
      lcdA.print(0, 0, ts + ds);    //Advanced Mode

    }

It reads a DHT22 sensor and an RTC, converts the RTC's output to "display" in 12 hour format, then collects all of those bytes, formats them with : or / characters, converts all that into Strings, then passes those values through the Blynk server to my cell phone. There is one minor glitch yet to be ironed out in that the 2 arrays kind of stack up in the virtual 16x2 LCD in the app, leaving me with 1.5 Strings printed to line 1, and .5 Strings printed to line 2. I expect to have this resolved by the end of tonight.

      char timeStamp[11];
      char dateStamp[11];
      sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
      sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
      String ts;
      String ds;
      ts = timeStamp;
      ds = dateStamp;
       
      lcdA.clear();                   //Advanced Mode
      lcdA.print(0, 0, ts + ds);    //Advanced Mode

Or

      char dateAndTimeStamp[22];
      sprintf(dateAndTimeStamp, "%02d:%02d:%02d-%02s %02d/%02d/%04d", displayHour, MIN, SEC, meridian, now.month(), now.day(), now.year());

      lcdA.clear(); 
      lcdA.print(0, 0, dateAndTimeStamp);

(with no useless Strings...

The way the Blynk LCD widget is set up in the app, the code has to reflect that each String is listed on their own line so that the output is displayed as;

12:00:00 AM
3/27/2016

I tried to concatenate them together as you stated, but the output returned as;

12:00:00 AM 3/
27/2016

As my project progresses, I will seek to slim the code down as needed when practical, but until I learn more, I don't want to rearrange it just yet. It works great now as;

      char timeStamp[11];
      char dateStamp[11];
      sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
      sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
      String ts;
      String ds;
      ts = timeStamp;
      ds = dateStamp;
       
      lcdA.clear();            //Advanced Mode
      lcdA.print(3, 0, ts);    //Advanced Mode
      lcdA.print(3, 1, ds);    //Advanced Mode

myggle:
As my project progresses, I will seek to slim the code down as needed when practical, but until I learn more, I don't want to rearrange it just yet. It works great now as;

      char timeStamp[11];

char dateStamp[11];
      sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
      sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
      String ts;
      String ds;
      ts = timeStamp;
      ds = dateStamp;
     
      lcdA.clear();            //Advanced Mode
      lcdA.print(3, 0, ts);    //Advanced Mode
      lcdA.print(3, 1, ds);    //Advanced Mode

Ever considered that you more than likely can use the below??

lcdA.print(3,0, timeStamp);
lcdA.print(3.0, dateStamp);

Your arrays are too short to hold those strings....

Regards,
Ray L.

First, if your display supports char array, as sterretje suggested, get rid of the String class. It eats memory more than it needs to and it can fragment it as well. Second, sprintf() is a powerful function, but rarely does one program use all of that power. As a result, it, too, chews up a lot of resources. The code suggested earlier with the String class and sprintf() uses 4888 bytes in the code below, if the appropriate lines are uncommented/commented. The revised code, even though I added a new function, is only 2522 bytes but has similar functionality.

#define DATE 1
#define TIME 2

void setup() {
  // put your setup code here, to run once:
  char timeStamp[12] = {0};
  char dateStamp[11] = {0};
  int displayHour = 12;
  int minute = 22;
  int sec = 33;

  Serial.begin(9600);

  DTFormatter(timeStamp, displayHour, minute, sec, TIME);
  DTFormatter(dateStamp,  5, 3, 2015, DATE);                    // Replace with now() calls...

  Serial.println(timeStamp);
  Serial.println(dateStamp);
  
  
//  sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, minute, sec, meridian);
//  sprintf(dateStamp, "%02d/%02d/%04d", 5, 3, 2016);
//  String ts;
//  String ds;
//  ts = timeStamp;
//  ds = dateStamp;

//  Serial.println(ts);
//  Serial.println(ds);
}

/*****
 Purpose: To format time and dates into a string

 Parameter list:
  char *result    where the resulting result is stored
  int hm          a dual-function integer that holds either the 'h'our or 'm'month
  int md                                                        'm'inutes or 'd'ay
  int sy                                                        's'econds or 'y'ear
  int which       1 = date, 2 = time

 Return value:
   void
 */
void DTFormatter(char *result, int hm, int md, int sy, int which)
{
  char meridian[] = {" AM PM"};
  char temp[5];
  
  if (hm < 10) {          // Do hour or month
    strcpy(result, "0");
  }
  strcat(result, itoa(hm, temp, 10));
  strcat(result, which == DATE?"/":":");
  
  if (md < 10) {          // Do minutes or days
    strcat(result, "0");
  }
  strcat(result, itoa(md, temp, 10));
  strcat(result, which == DATE?"/":":");

  switch (which) {
    case TIME:
      strcat(result, itoa(sy, temp, 10));  
      if (hm >= 12) {
        strncat(result, &meridian[3], 3);  
      } else {
        strncat(result, meridian, 3);     
      }
      break;
    case DATE:
      if (sy < 2000)              // Only works for this century, although you'd expect 4 digits otherwise
        sy += 2000;
      strcat(result, itoa(sy, temp, 10));     
      break;
      
    default:
      strcpy(result, "Error");
      break;
  }
  if (sy < 10) {          // Do minutes or days
    strcpy(result, "0");
  }
}

void loop() {

}

There are probably some tweaks that could be made to the formatting function, too.

Edit: I forgot to remove the meridian array in setup().