Try to send time to azure

Hello,
I’m trying to send time and date values by json to azure, but when I send the values I get strange characters.

someone could help me or explain how I can send date and time values; I know that the date and time of publication is sent by json but I need to publish the values that I obtain in my code.

Code:

void loop(){
    
        delay(10);                      // even cicuits need a break
        digitalWrite(D2, HIGH);         // activate trigger
        delayMicroseconds(10);
        digitalWrite(D2, LOW);          // de-activate trigger

        duration = pulseIn(D6, HIGH);   // how long until a reply?
        distance=(duration/2)/29.1;
                                        // a blocking call so may wait a few seconds
    if(clienteStart<1){
        if(distance<distMax){
        startTime=Time.format(Time.now(), "%H:%M:%S");
        cliente=cliente+1;
        clienteStart=1;
        }
    }

    if(clienteStart>0 && cliente>0){
        if(distance>distMax){
        endTime=Time.format(Time.now(), "%H:%M:%S");
        
        char payload[255]; 
        
        snprintf(payload, sizeof(payload), "{\"organization\":\"%s\",\"agencia\":\"%s\",\"ventanilla\":\"%s\",\"cliente\":%d,\"timeini\":\"%s\",\"timefin\":\"%s\"}", Org, Agen, Vent, cliente,(String) startTime,(String) endTime);  
        Particle.publish("event-data-ab01", payload);
        
        publishDisplay((String) cliente,startTime,endTime);
    //    Particle.publish("StartTime");
        clienteStart=0;
        }
    }
    //  publishDisplay((String) distance,(String) cliente,(String) clienteStart); 
      delay(1000);  
}

event data:

{"data":"{\"organization\":\"XXX\",\"agencia\":\"Molina01\",\"ventanilla\":\"02\",\"cliente\":3,\"timeini\":\"\u0018@\",\"timefin\":\"Ѐ\"}","ttl":60,"published_at":"2018-07-16T01:22:07.832Z","coreid":"230038000247353137323334","name":"event-data-ab01"}

thanks.

snprintf() has no formatting scheme for String objects, so what you effectively are doing is fooling snprintf() into believing that the address of the String object is the address of the acutal string (character array), which it isn’t.
You should rather write this

        snprintf(payload, sizeof(payload), "{\"organization\":\"%s\",\"agencia\":\"%s\",\"ventanilla\":\"%s\",\"cliente\":%d,\"timeini\":\"%s\",\"timefin\":\"%s\"}", Org, Agen, Vent, cliente,(const char*)startTime,(const char*)endTime);  
        Particle.publish("event-data-ab01", payload, PRIVATE);

The String class has a dedicated cast operator overload (const char*) that actually returns the address of the internal string.

BTW, I’d avoid using String wherever possible on embedded devices and rather go with pure C char arrays.
Also if you only want to have the current time you’d only write endTime=Time.format("%H:%M:%S");

2 Likes

thanks @ScruffR, now send correct data on console, but i have a problem now.

Well, this is my first application sending azure information. The problem I have is that I can not store in the blob storage that I created.
I took the steps that photon recommends in the documentation to connect with azure and perform every step that indicates azure to save the messages in the Blos Storage but when I review the information it appears empty.
I apologize for this, but I can not find a solution to this problem.
thank you for your patience.

This is a configuration about particle and azure:
configuration

I’m not using Azure, since they don’t seem to offer a permanently free account for testing in Europe and I’d not have any other need for it than assisting the community.

So I’ll have to defer you to someone who has actual Azure experience - e.g. @rickkas7

1 Like

thanks @ScruffR,
Hello @rickkas7, can you help me please? I’ll be very greatful

Unfortunately I’ve never used the Azure integration either.

Thanks my friends,

i try to resolve this problem in other topic.