Artik Cloud with Particle HW

Hello,

I have been trying to expand the horizons of my particle hw, and be able to post some data to a samsung cloud.
Can anyone give me a clue of how can I call the following json lines within my particle code:

the url:
https://api.artik.cloud/v1.1/messages
the headers are:

{
  "Content-Type": "application/json",
  "Authorization": "Bearer 3e927##############ab6345"
}

the message is:

{
    "data": {
        "VariableArrayName": [
            1000,
            2025,
            3425
        ]
    },
  "sdid": "########id############",
  "type": "message"
}

How can I implement this in my particle HW

Please help,

Butilbenceno

What have you tried so far ;)?

I tried to create a webhook, at that point it went all through within particle console, meaning that webhook was succesfully created. Then, I tried to run the webhook using the "cmd : particle publish eventname"
and nothing happened, it didnt return an error, it in fact said that event was published, but still variable within Artik didnt change, and when returning to my particle console and looking the history of the webhook, it says “has not been triggered”… hmm! I kept calling it and no change!!.

Then I kept looking, but still I think that this can be done straight within the particle code, although no idea how!

1 Like

woow!! it actually worked, seems like I was missing to set up the response template for the Webhook.
Ok, the process helped, however that brings another question, and perhaps this question will reveal my ignorance! :smile:.

As can be seen from the JSON lines, right where the data payload is, I have numbers, obviusly I would like to change that and use an variable array, that will be updated in the process of the Particle code.

How can I use array variable within that webhook?

thanks,
Butilbenceno

hi @butilbenceno. Not sure if the exact answer you need is in these linked threads… but they are on same topic…
Have you tried these?


Thanks… it helped, a lot!!, particulary to understand the webhook set up and usage, however the way he uses the data is the opposite direction, meaning that he pulls data from uweather, and use it in the board.

I want to massage data in the board about 100 long variables (16bits), have them included in one array and then use the webhook to send it out to artik.cloud. if you take a look to the JSON lines that artik is requiering me to use to POST the data, data has an array. (here it is…)

{
“data”: {
“VariableArrayName”: [
1000,
2025,
3425
]
},
“sdid”: “########id############”,
“type”: “message”
}

I have fixed values now just for the sake of testing, and it worked.
At this point i created the webhook with fixed values and particle console suggests the following call out for the publish instruction:

void loop() {
// Get some data
String data = String(10);
// Trigger the webhook
Particle.publish(“artikHook”, data, PRIVATE);
// Wait 60 seconds
delay(60000);
}

And maybe, just maybe, and I am thinking while typing I just need to massage the message increase the string size, and input the data variable with JSON string format including my refreshed data.
I don’t know if that would work.

On the other hand the data field not just includes the data, it includes the “sdid” and “type”, which are fixed and already included in my webhook, should I just erase the entire content of data in my webhook and have it been created within my particle HW, or perhaps by just excluding the raw data part from my webhook, and call will merge the raw data part along with the rest of the sdid and type values.

I guess it is a quick test, but my hardware is running something else… mhhmm!

Thanks,
Butilbenceno

Hi @butilbenceno.
I am not an expert (just learning)…but re your question:

And maybe, just maybe, and I am thinking while typing I just need to massage the message increase the string size, and input the data variable with JSON string format including my refreshed data.
I don’t know if that would work.

I am pretty sure it would. Whilst I didnt have a large array to deal with, I asked this question of the forum:
https://community.particle.io/t/how-to-pass-multiple-data-items-from-particle-device-to-webhook/?source_topic_id=25700

I too needed to pass multiple separate variables from the device to my webhook, then have the webhook parse these into the correct JSON variables when setting up a POST. The great tutorial by @rickkas7, at https://github.com/rickkas7/particle-webhooks which I quoted above explains nicely how to do that by creating a JSON string for the particle device “data” variable in the webhook, then using mustache templates to extract the variables up at the webhook.

To quote @rickkas7
> Sending Complex Data

In the examples above we pass a simple string in the “value” field in JSON. But what if you have multiple pieces of data you want to send to the server? Mustache can help you there too!

In the Google Elevation API example below we sent data from the Photon in JSON format.

The Photon code looks like this:

float lat = 39.73915360;
float lng = -104.98470340;

char data[256];
snprintf(data, sizeof(data), “{“lat”:%f, “lng”:%f}”, lat, lng);

Particle.publish(“getElevation”, data, PRIVATE);
This will sent the data up to the cloud formatted like this:

{“lat”:39.73915360, “lng”:-104.98470340}
Then, in your query and json webhook templates, you can now use {{lat}} and {{lng}} Mustache variables to access the values of those fields.

Also handy: If you need to generate an access token on your Particle device, you can pass it up along with other data in JSON format, then include it in an Authorization header or in a URL query parameter using Mustache.

In my case I solved my problem (of having to send two variables - an authorisation token and a message body to a SMS API service) with this Photon code

  if (token.length() > 0)  {
    String message = "{\"SMSTokString\": \"Bearer ";
    message.concat(token);
    message.concat("\", \"body\": \"");
    message.concat(body);
    message.concat("\"}");
    Particle.publish("SendSMS",message,60,PRIVATE); 

and this JSON webhook setup text file (setting the webhook up from the CLI using a text file is definitely the way to go).

{
“event”: “SendSMS”,
“url”: “https://api.telstra.com/v1/sms/messages”,
“requestType”: “POST”,
“headers”: {
“Authorization”: “{{SMSTokString}}”
},
“json”: {
“to”: “cell number”,
“body”: “{{body}}”

},

“mydevices”: true,
“noDefaults”: true
}

I hope this helps

it does help, all of the sudden it is like all the pieces I have read here and there fall together and start making sense.

Now!!, I can see what the next challenge may be, I wonder if those {{ }} mustache variables would accept or not arrays. Anyway I’ll give it a try

Thanks, keep you posted!
Butilbenceno

Arrays can sort of be used in mustache templates. There is no “include all of the elements in the array” option at this time, but you can specify and include individual elements in an array in output.

It actually compiled, I have not been able to try it, though!.
But now with what you said Ill keep my expectations low.

@butilbenceno hi, did you try it with Artik cloud. ? Could you successfully send values from code to Artik cloud ?

Hello all,
I have tried the above solutions, unfortunately I didn’t have success with ARTIK cloud.

This is in my code, trying to send 3 values to Artik cloud using web-hook

if((millis() - lastTime) > 40000)  
  {
    char buf[1000];
	sprintf(buf, "{ \"Humidity\": %d, \"Temperature\": 60, \"BatteryVolt\": 7}", 5 );
				//hum, temp, batt);
    Particle.publish("myEvent", buf, PRIVATE);
    Serial.println("Event published");
    Serial.print(buf);
    lastTime = millis();
  }

buf string printed on Terminal is perfect.
{ “Humidity”: 5, “Temperature”: 60, “BatteryVolt”: 7}

But in the Web-Hook, escape character before double quotes is not removed. ( as seen from my Particle Console).
“data”: “{ “Humidity”: 5, “Temperature”: 60, “BatteryVolt”: 7}”,

In the above link I found, particle cloud does not escape the back slash when sending the text to Web-hook.

Since the back slash is not escaped, Artik cloud is not able to read the incoming JSON data… Even though I could successfully POST the data to Artik cloud, the data is not interpreted and Artik cloud shows No Data.

Can anyone help me how to resolve this ? Is it possible to escape the \ in the text sent to Web-hook.?
During testing, I created the web-hook with static JSON data and Artik cloud is able to read the value. I suspect only with this escape character it is not able to. :frowning:

Thanks.