Hi all,
I'm using the time library for my home energy monitoring project. In this project, I count the pulses coming from my electricity, gas and water meter. I also read out a temp/humidity sensor (SHT15) located in my garage.
Each time a change in one of the "sensors" occurs (i.e pulse or value change from temp/humidity sensor) , a UDP message with a small XML string is sent to a monitoring system. Below is an example of such a message (when a value change occurs for the humidity sensor) :
<e>
<s id="6" vChMSRo="0" vChMS="1599330" vChDT="2011-09-15T10:06:01" val="487" />
</e>
Explanation :
id = sensor identification
vChMSRo = value change millis rollover (I count the number of times the millis() rolled over since start-up)
vChMS = number of milliseconds at which the value change occurred
vChDT = date/time at which the value change occurred
val = the actual value
And below is one for a pulse counter (I'm using interrupt service routines for counting pulses) :
<e>
<s id="1" pIDRo="0" pID="3" pStartMSRo="0" pStartMS="8162174" pStartDT="2011-09-15T11:55:27" pDurMS="138" pIntMS="0" state="0" />
</e>
Explanation :
id = sensor identification
pIDRo = number of times the pulse ID rolled over
pID = pulse ID
pStartMSRo = number of times the millis() rolled over at the time the pulse started
pStartMS = millis() value at the time the pulse started
pStartDT = date/time the pulse started
pDurMS = duration of the pulse in ms
pIntMS = interval between this pulse and the previous pulse (in ms)
state = current state of the pulse
The XML string contains - among other info - the date and time at which the event occurred.
For each (pulse) sensor, the date/time is stored in a time object.
For reporting, I'm using the following function to convert the time object to a date/time string :
char* XMLDateTime(time_t t)
{
char chrDT[21];
PString strDT(chrDT, sizeof(chrDT));
strDT.print(year(t));
strDT.print("-");
if (month(t) < 10)
{
strDT.print("0");
};
strDT.print(month(t));
strDT.print("-");
if (day(t) < 10)
{
strDT.print("0");
};
strDT.print(day(t));
strDT.print("T");
if (hour(t) < 10)
{
strDT.print("0");
};
strDT.print(hour(t));
strDT.print(":");
if (minute(t) < 10)
{
strDT.print("0");
};
strDT.print(minute(t));
strDT.print(":");
if (second(t) < 10)
{
strDT.print("0");
};
strDT.print(second(t));
strDT.print("Z");
return chrDT;
}
The problem :
Very frequently I get crappy output in the date/time string part, like this :
<s id="5" vChMSRo="0" vChMS="8462268" vChDT="2011-09-15 !Ó" val="243" />
<s id="6" vChMSRo="0" vChMS="8433906" vChDT="2011-09!ÓS" val="507" />
It seems that sometimes a kind of buffer overflow occurs. Initially, I used the String object, but that made it even worse! (PString is a buffer overflow save string object).
Could you please check what could be the reason for this strange output?
Kind regards,
Bart.