So I’m having a hard time figuring out how to get a timestamp into a Ubidots mqtt publish.
formatted call:
client.add(“variableName”, int/float value, context as JSON char*, POSIX timestamp);
I’m using an Electron at 0.6.4.
As I understand it from the docs it takes a POSIX millisecond timestamp, essentially a UNIX timestamp with the milliseconds added to it. However a uint32_t (highest numeral value for an Electron) goes up to 4,294,967,295. The current UNIX stamp is 1,517,548,385 (added commas for easy reading) and the POSIX stamp that I will need to put in would be 1,517,548,385,000. Well beyond what a uint32_t can hold. So, how does one go about filling this value?
Here is the Ubidots example with some added in code as I was trying to make sense of what would and would not work. Thanks in advance.
/****************************************
* Include Libraries
****************************************/
#include <UbidotsMQTT.h>
/****************************************
* Define Constants
****************************************/
#ifndef TOKEN
#define TOKEN "TOKEN HERE" // Add here your Ubidots TOKEN
#endif
bool haveDeviceName = false;
String deviceName;
char deviceNameArray[25]; // device name must be 25 characters or less
float value = 1;
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println("payload obtained from server:");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]); // prints the answer of the broker for debug purpose
}
// Some stuff to make with the payload obtained
//
//
Serial.println();
}
/****************************************
* Instances
****************************************/
Ubidots client(TOKEN, callback);
/****************************************
* Sub Routines
****************************************/
void nameHandler(const char *topic, const char *data) {
deviceName = String(data);
deviceName.toCharArray(deviceNameArray, deviceName.length()+1);
haveDeviceName = true;
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
Particle.subscribe("particle/device/name", nameHandler);
Particle.publish("particle/device/name");
while(!haveDeviceName) {
Serial.println(F("waiting for device name"));
delay(2000);
}
Serial.print(F("We have a Name! "));
Serial.println(deviceName);
while(!Time.isValid()) {
Serial.println("Validating Time");
}
Time.zone(-5);
Serial.println(Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL));
Serial.println(Time.now());
Serial.println(Time.format(Time.local(), TIME_FORMAT_ISO8601_FULL));
Serial.println(Time.local());
client.initialize();
pinMode(D7, OUTPUT);
// Uncomment this line if you have a business Ubidots account
//client.ubidotsSetBroker("business.api.ubidots.com");
}
void loop() {
if(!client.isConnected()){
client.reconnect();
}
char buf[12]; // "-2147483648\0"
// Publish routine, if the device and variables are not created they will be created
for(byte i = 0; i < 6; i++){
int eventTime = Time.now();
String strVal = "string value";
char character = 'A';
String eventTimeStr(eventTime);
Serial.println("Sending value");
client.add("val", value); // Insert as first parameter your variable label
client.add("valcontext", value, "\"lat\":10.302, \"lng\":2.9384"); //Adds value with context
client.add("valString", character);
// client.add("valtime", value, NULL, (eventTime * 1000)); // Adds value with custom timestamp. timestamp is not formatted properly and reads constant January 1, 1970
// client.add("valcontexttime", value, "\"lat\":10.302, \"lng\":2.9384", eventTime); // will not send any data to ubidots with this format in publish
// client.add("valarrayContext", value, itoa(eventTime, buf, 10)); // will not send any data to ubidots with this format in publish
// client.add("valStringContext", value, eventTimeStr); // will not send any data to ubidots with this format in publish
// client.add("valarrayContexttime", value, itoa(eventTime, buf, 10), Time.now()); // will not send any data to ubidots with this format in publish
client.ubidotsPublish(deviceNameArray); // Insert your device label where the values will be stored in Ubidots
Serial.println(Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL));
Serial.println(Time.now());
Serial.println(eventTime);
digitalWrite(D7, HIGH);
delay(5000);
digitalWrite(D7, LOW);
value++;
}
client.loop(); // Client loop for publishing and to maintain the connection
}