Go Down

Topic: Saving Time as String (Read 814 times) previous topic - next topic

nixxit

I'am trying to write a program that simulates a device that communicates via Serial over TCP/IP.

I figured out I need to save everything in a single String before transmitting or the data gets fragmented and the PC software cant interpret it. Got that working. Now I need a working time stamp... it doesn't have to be correct time... I don't even car if its 24 or 12 hr format

It has to be formatted as hours:minutes:seconds

I'm getting frustrated and need help lol the time librarys seem so complicated     

here is what I have working

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>

// mac and ip
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0xC6, 0x10 };
IPAddress ip(10,0,16,3);


// initilaze servers
EthernetServer server1(4001);
EthernetServer server2(4002);

// String variables
String Sblender, Schemadd;

// data feilds - clean,drv disch,drv clean,pass disch,pass clean,
//               auger1,auger2,auger3,auger4,meas dens, target,
//               calculated
float blender[] = {
  50.2, 48.5, 24.1, 1.7, 26.0,
  25, 15, 0, 25, 0.27, 0.25, 0.25};
int chemadd[] = {
  1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};

// buffers counters and junk
char tmp[4];


void setup() {

  // start connection and server:
  Ethernet.begin(mac, ip);
  server1.begin();
  server2.begin();
}

void loop() {
  // append data feilds to string
  Sblender = ("10:23:67");          // constant for now NEED TO FIX
  for( int i = 0; i < 12; i++){
    Sblender += (",");
    dtostrf(blender[i],1,2,tmp);
    Sblender += tmp;
  }
  // Transmit no more than once a second
  delay(1000);
  // talk to me !
  server1.println(Sblender);


}









nixxit

OK guess i just needed to take a break, I got it working. I just modified the popular digitalClockDisplay() and printDigits() functions to append to a string instead of printing.

Code: [Select]
#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>

// mac and ip
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0xC6, 0x10 };
IPAddress ip(10,0,16,3);


// initilaze servers
EthernetServer server1(4001);
EthernetServer server2(4002);

// String variables
String Sblender, Schemadd, stamp ;

// data feilds - clean,drv disch,drv clean,pass disch,pass clean,auger1,auger2,auger3,auger4,meas dens, target,calculated
float blender[] = {
  50.2, 48.5, 24.1, 1.7, 26.0,
  25, 15, 0, 25, 0.27, 0.25, 0.25};
int chemadd[] = {
  1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};

// buffers counters and junk
char tmp[4];
int i = 0;

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

  // start connection and server:
  Ethernet.begin(mac, ip);
  server1.begin();
  server2.begin();
}

void loop() {
  timeStamp();
  // append data feilds to string
  Sblender = stamp ;          // constant for now NEED TO FIX
  for(int i = 0; i < 12; i++){
    Sblender += (",");
    dtostrf(blender[i],1,2,tmp);
    Sblender += tmp;
  }

  Schemadd = stamp ;
  for(int i = 0; i < 7; i++);
  {
    Schemadd += (",");
    dtostrf(chemadd[i],1,2,tmp);
    Schemadd += tmp;
  }

  // Transmit no more than once a second
  delay(1000);
  // talk to me !
  server1.println(Sblender);
  server2.println(Schemadd);
}

void timeStamp(){
  // set time to something
  stamp = ("");
  //get hours and format
  if (hour() < 10){
    stamp += ("0");
  }
  stamp += (hour());
  printDigits(minute());
  printDigits(second());
}

void printDigits(int digits){
  stamp += (":");
  //leading 0's
  if (digits <10){
    stamp += ("0");
  }
  stamp +=(digits);
}

PeterH


the data gets fragmented and the PC software cant interpret it.


To me that suggests there's something wrong with the PC software.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
  http://arduino.cc/forum/index.php/topic,145765
http://www.gammon.com.au/electronics

nixxit

Quote
Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
  http://arduino.cc/forum/index.php/topic,145765


I did not know that, you may have saved me a big headache Thank you

Quote
To me that suggests there's something wrong with the PC software.

Your probably rite but its a third party.  I think the software should wait for the end of line character before trying to process the string but the string must be written very fast if not in a single write command. 

Go Up