This is directed mostly at @RWB but anyone else who can help please chime in.
I have been spending the better part of a week trying to get MQTT to work with my Photon because I was interested in the subscription feature. I had no success with that so I found myself going with nothing but the example. No joy.
Thank you @RWB for introducing me to Ubidots back in March of this year, I have been and am currently using the HTTP method to publish to Ubidots on several projects since then without any difficulty, so when I recently read that you were using MQTT to publish data to Ubidots from an Electron I had to try it too since saving power is important to me also.
At the most basic level I am using the published code with my TOKEN without success. I read somewhere else on the forum that Serial was interfering with MQTT so I commented that out, but still no success.
The code below is taken straight from the example on Ubidots and the only difference is that it only publishes every 15 seconds instead of hundreds of times a second, the code as posted loops infinitely fast and I thought Ubidots wouldn’t appreciate that much data in such a short period of time.
The example states “After running this function, you should see a new data source in your account with the name “my-device” and two variables “temperature” and “humidity”, with the corresponding analog readings inside each variable.” This implies that it is pretty much automatic or automagic. I have even created this data source and the variables and it still doesn’t update. Yes, I replaced “my-device” with “nudibranch” as in the code below.
If you or anybody else sees anything missing and/or bungled in the code below please let me know. I’d really like to get this working and then move on to using MQTTs Subscribe feature.
// This #include statement was automatically added by the Particle IDE.
#include "UbidotsMQTT/UbidotsMQTT.h"
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
#define TOKEN "xxxxxxxxxx" // Add here your Ubidots TOKEN
#define VARIABLE_IDENTIFIER_ONE "humidity" // Add a variable identifier, it must be in lowercase
#define VARIABLE_IDENTIFIER_TWO "temperature" // Add a variable identifier, it must be in lowercase
#define DATA_SOURCE_NAME "nudibranch"
void callback(char* topic, byte* payload, unsigned int length);
Ubidots client(TOKEN, callback);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
//Serial.write(payload, length);
//Serial.println(topic);
}
void setup() {
//Serial.begin(115200);
while (client.connect());
client.setDataSourceLabel(DATA_SOURCE_NAME);
}
void loop() {
int hum = random(100);
int temp = random(1000, 5000);
client.add(VARIABLE_IDENTIFIER_ONE, hum);
client.add(VARIABLE_IDENTIFIER_TWO, temp);
client.sendValues();
delay(15000);
}
Thanks in advance for any assistance,
Joerg Seiler