Ajax and NTP

I’d appreciate some help.

I have a sketch running on a Mega which uses Software Serial to query our central heating boiler and output the results in human readable form on an Ajax based webpage. It works well, and I’ll post the lot if anyone cares, but it weighs in at 50k, so reading it is not for the faithearted. The tester sketch illustrates all the salient points, with the boiler comms specifics commented out and emulated and with miles of detailed data manipulation and reporting removed.

I use getNTP() to collect and format a timestamp, which is used for a starting time. I also want to record and timestamp all state changes (from boiler on to boiler off, etc, etc).

When there is only a single getNTP() request it works perfectly (see Start: , which I’ve artificially fixed for the first 15 sec or so). When there is more than one request they all update on the Ajax output, I can’t find any way to make results static, so that individual events can be timestamped. I could do this using HTML rather than Ajax, but that requires browser f5 refreshing, which I’d like to avoid.

So how do I turn a dynamic variable into a static result? Can anyone advise please.

_Boiler_monitor_Ajax_NTP_tester_1_0.ino (15 KB)

I have a sketch running on a Mega which uses Software Serial

while not using any of the other 3 hardware serial ports. Why?

You return a pointer to a static memory location, every time you call getNTP(). You store that pointer in several places. Each time you call getNTP(), you change the static memory location, and return another pointer to that location. So, all the pointers point to the same place, so they all point to the same value.

You should pass an array to getNTP(), and populate that array. That way, you'd not be stepping on the data other functions use.

Paul, thanks, history I suppose, and because it works. Would I gain anything significant by changing the serial arrangement?

Anyway, I've been reading around on passing an array, but I've obviously got it wrong somewhere. Fact is I don't really understand how to do it. What I have works, but exactly as before, with both values updating.

...
char* myArray[10];

void setup {
...
}
void loop {
...
GetAjaxData(client);
...
}

void GetAjaxData(EthernetClient cl)  {

...
getNTP(myArray,0);
Serial.print("Start: );
Serial.print(myArray[0]);
...
getNTP(myArray,1);
Serial.print("State change: );
Serial.print(myArray[1]);
....
}

void getNTP(char* thisArray[10],int n) {
....
 sprintf(t0, "%s:%s:%s",hh,mm,ss);
thisArray[n] = t0;
....
}

char* myArray[10];

This is NOT an array of characters. It is an array of pointers.

You MIGHT want a 2D array of arrays:

   char times[10][10]; // 10 times of 10 characters each

In your getNTP() code, t0 is a pointer, so that latest code just changed which pointer is being replicated.

   getNTP(times[4]); // Get the time in the 5th postion
void getNTP(char buff[10])
{
   // Get the hours, minutes, and seconds
   sprint(buff, ""%s:%s:%s",hh,mm,ss);
}

That works very well indeed, thankyou so much. I've just listed 50 artificial state changes, now to find out how long the boiler takes to do that many.

Thanks again.