Hi there @chipmc, I will advise two ways that come to my mind to solve your problem:
1. Using Particle Webhooks
Let’s create a simple script to send 8 variables using Particle Webhooks. Notice that we will also send an unique timestamp.
const int ARRAY_LENGTH = 8;
long values[ARRAY_LENGTH];
char *labels[ARRAY_LENGTH] = {"t0","t1","t2","t3","t4","t5","t6","t7"};
char timestamp_ubi[13];
char payload[700];
void setup() {
Serial.begin(115200);
}
void loop() {
unsigned long timestamp_seconds = Time.now();
sprintf(timestamp_ubi, "%lu000", timestamp_seconds);
readValues(values);
buildPayload(payload);
Serial.println(payload);
//Particle.Publish("ubidots", payload, PRIVATE);
delay(5000);
}
void readValues(long values[]){
for (uint8_t i = 0; i < ARRAY_LENGTH; i++){
values[i] = analogRead(i + 10);
}
}
void buildPayload(char payload[]) {
sprintf(payload, "%s", "");
sprintf(payload, "{");
for (uint8_t i = 0; i < ARRAY_LENGTH;) {
sprintf(payload, "%s\"%s\":%d", payload, labels[i], values[i]);
i++;
if (i < ARRAY_LENGTH) {
sprintf(payload, "%s,", payload);
} else {
sprintf(payload, "%s,\"timestamp\":%s}", payload, timestamp_ubi);
}
}
}
The payload that will be sent will look like the one below:
{"t0": 1000, "t1": 1000, .... , "t7": 1000, "timestamp": 1568213275000}
Now, let’s create a webhook that will add the timestamp key to every value:
Settings:
Advanced settings: Add the timestamp to every key-value pair
Headers:
2. Ubifunctions:
This is the most flexible way, and there are multiple solutions that may fit your needs, I will write just one as example. Let’s create the format below for your values stream:
{"values": "value_1,value_2,...,value_n,timestamp"}
Note: You will need to implement in your firmware the routine to create the json above.
The Ubifunction code (in python) to decode the string above would look like:
def main(args):
'''
Main function - runs every time the function is executed.
"args" is a dictionary containing both the URL params and the HTTP body (for POST requests).
'''
stream = args.get("values")
payload = decoder(stream)
# Add here the logic to send
send_to_ubidots(payload)
def send_to_ubidots(payload):
'''
Define the function to send data, not written here to make the post short, please guide yourself from the
basic Ubifunction example
'''
pass
def decoder(stream):
'''
Decodes the custom stream
'''
counter = 0
values = values.split(",")
timestamp = values[len(values)-1]
for value in values[0:len(values)-1]:
payload[f"value-{counter}"] = {"value": value, "timestamp": timestamp}
counter += 1
return payload
Notice that the Ubifunction script is implemented to decode an already custom data buffer. It is important to mention here that Ubifunctions receives just json type data as arguments.
I hope that these examples may serve you as reference, if you have any further doubt, just let me know.