user48730: In previous question in this forum - I asked about updating clock on an ESP8266, using MQTT broker to send a time stamp on demand, in following manner:
1) ESP8266 sends a publish containing the payload :"sync",
2) broker answers in string format ( this part is python )
def on_message(self, client, obj, msg):
    self.arrived_msg = msg.payload.decode()
    if self.arrived_msg == "sync":
        a = datetime.datetime.now()
        b = "%d,%d,%d,%d,%d,%d,%d" % (a.year, a.month, a.day, a.hour, a.minute, a.second, a.microsecond) .  <------ here a string time format is created
        self.client.publish(topic=self.topics[0], payload=b, qos=self.topic_qos) 

3) on a terminal (MAC OS), subscribed to relevant topic- cant see publish results. which are OK:
sync                           <----- ESP sends request
2018,9,18,12,44,18,903242      <----- string answer from broker

4) BUT when Arduino code get this time stamp:
void callback(char* topic, byte* payload, unsigned int length) {
        char incoming_msg[50];
        // ledBlink(30, 5);
        Serial.print("Message arrived [");
        Serial.print(topic);
        Serial.print("] ");
        for (int i = 0; i < length; i++) {
                Serial.print((char)payload[i]);  <----- print #1
                incoming_msg[i] = (char)payload[i];  <----- generate incoming msg
        }
        Serial.println("");

        if (strcmp(topic,clockTopic)==0) {       <------- detect relvant topic for clock sync
                Serial.print(incoming_msg);  <------ Print #2
        }

5) I get this result on serial monitor :
Message arrived [HomePi/Dvir/Clock] 2018,9,18,13,48,5,230009 <---- here time stamp is OK , Print #1
2018,9,18,13,48,5,230009$��?O: @ . <----- Print #2

EDIT1:
6) I tried adding a null termination in time stamp, but it just added \0 to time stamp 
Appreciate any help
Guy

user37622: You forgot to set the terminating zero of the c-ctring incoming_msg.
add incoming_msg[length] = 0;

user4143: In C a string is "null terminated".  That means that the very last character of every string must be character 0 (or NULL).
You need to ensure that you add that NULL character to the end of your incoming_msg string to terminate it, or force the entire string to be cleared to NULLs before you populate it.
The simplest way is to zero the string:
bzero(incoming_string, 50);

Or you can force a 0 to be appended after each byte is added: 
incoming_msg[i] = (char)payload[i];
incoming_msg[i+1] = 0;

Or just add one at the end:
incoming_msg[length] = 0;

There's many ways of doing it - as long as you get that NULL in there somehow.

48730: <p>In previous question in this forum - I asked about updating clock on an ESP8266, using MQTT broker to send a time stamp on demand, in following manner:</p>

<p>1) ESP8266 sends a publish containing the payload :"sync",</p>

<p>2) broker answers in string format ( this part is python )</p>

<pre><code>def on_message(self, client, obj, msg):
    self.arrived_msg = msg.payload.decode()
    if self.arrived_msg == "sync":
        a = datetime.datetime.now()
        b = "%d,%d,%d,%d,%d,%d,%d" % (a.year, a.month, a.day, a.hour, a.minute, a.second, a.microsecond) .  &lt;------ here a string time format is created
        self.client.publish(topic=self.topics[0], payload=b, qos=self.topic_qos) 
</code></pre>

<p>3) on a terminal (MAC OS), subscribed to relevant topic- cant see publish results. which are OK:</p>

<pre><code>sync                           &lt;----- ESP sends request
2018,9,18,12,44,18,903242      &lt;----- string answer from broker
</code></pre>

<p>4) <strong>BUT</strong> when Arduino code get this time stamp:</p>

<pre><code>void callback(char* topic, byte* payload, unsigned int length) {
        char incoming_msg[50];
        // ledBlink(30, 5);
        Serial.print("Message arrived [");
        Serial.print(topic);
        Serial.print("] ");
        for (int i = 0; i &lt; length; i++) {
                Serial.print((char)payload[i]);  &lt;----- print #1
                incoming_msg[i] = (char)payload[i];  &lt;----- generate incoming msg
        }
        Serial.println("");

        if (strcmp(topic,clockTopic)==0) {       &lt;------- detect relvant topic for clock sync
                Serial.print(incoming_msg);  &lt;------ Print #2
        }
</code></pre>

<p>5) I get this result on serial monitor :</p>

<pre><code>Message arrived [HomePi/Dvir/Clock] 2018,9,18,13,48,5,230009 &lt;---- here time stamp is OK , Print #1
2018,9,18,13,48,5,230009$��?O: @ . &lt;----- Print #2
</code></pre>

<p>EDIT1:</p>

<p>6) I tried adding a null termination in time stamp, but it just added <code>\0</code> to time stamp 
Appreciate any help</p>

<p>Guy</p>
 37622: <p>You forgot to set the terminating zero of the c-ctring <code>incoming_msg</code>.</p>

<p>add <code>incoming_msg[length] = 0;</code></p>
 4143: <p>In C a string is "null terminated".  That means that the very last character of every string <em>must</em> be character 0 (or NULL).</p>

<p>You need to ensure that you add that NULL character to the end of your <code>incoming_msg</code> string to terminate it, or force the entire string to be cleared to NULLs before you populate it.</p>

<p>The simplest way is to zero the string:</p>

<pre><code>bzero(incoming_string, 50);
</code></pre>

<p>Or you can force a 0 to be appended after each byte is added: </p>

<pre><code>incoming_msg[i] = (char)payload[i];
incoming_msg[i+1] = 0;
</code></pre>

<p>Or just add one at the end:</p>

<pre><code>incoming_msg[length] = 0;
</code></pre>

<p>There's many ways of doing it - as long as you get that NULL in there somehow.</p>
