Skip to main content
added 101 characters in body
Source Link
guyd
  • 1k
  • 2
  • 26
  • 62

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

Appreciate any helpEDIT1:

  1. I tried adding a null termination in time stamp, but it just added \0 to time stamp Appreciate any help

Guy

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

Appreciate any help

Guy

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:

  1. I tried adding a null termination in time stamp, but it just added \0 to time stamp Appreciate any help

Guy

Source Link
guyd
  • 1k
  • 2
  • 26
  • 62

Using MQTT to send time stamp - to Arduino

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

Appreciate any help

Guy