Hello friends
I've been working with the PubSubClient for Arduino Yun the past day, and it is really promising. I basically got it to work through test.mosquitto.org, where i can subscribe to a topic(bobby/temp/office) - using the terminal. Then I'm publishing a message using :
mosquitto_pub -m test.mosquitto.org -t bobby/temp -m "1"
To start a ds18 broadcasting to the before mentioned topic every second, using the Timer library. This is super cool, and I also implemented the "stop" function - and some other functions for changing the update time etc - just to play a little with the things.
However, before going into redesigning my sensor network to use MQTT, I'm wondering how to structure my topics of the MQTT. It makes perfect sense when broadcasting sensor readings to use "myHome/somePlace/whatSensor" and then subscribe to that, from whatever interface I'm going to use What I'm doing on the ds18 now:
void tick(void* context) {
ds18.requestTemperatures();
char* temp = new char[32];
String(ds18.getTempCByIndex(0)).toCharArray(temp, 32);
mqtt.publish("bobby/temp/kontor", temp);
free(temp);
}
But when it comes to analysing incoming publications to a given topic, I feel that the byte* payload argument in the callback function of PubSubClient seems a little ineffective? And I need to first convert it into a char* array, to then typecast it to a String to actually use the data - and now I'm even converting the String to an integer using toInt() afterwards. I know that memory issues will occur down the line - worst nightmare! This is what I do in the function at the moment (changing the update ms time using the message from my MQTT publication):
void callback(char* topic, byte* payload, unsigned int length) {
char* message = new char[length];
for(int i = 0; i < length; i++) {
message[i] = payload[i];
}
Serial.println(message);
t.stop(timer);
timer = t.every(String(message).toInt(), tick, (void*)0);
free(message);
}
And maybe I want to do something like this, to build my own API-ish thing:
void callback(char* topic, byte* payload, unsigned int length) {
char* message = new char[length];
for(int i = 0; i < length; i++) {
message[i] = payload[i];
}
Serial.println(message);
if(topic == "bobby/temp/set") {
if(message == "1") {
Serial.println("running temp");
t.stop(timer);
timer = t.every(1000, tick, (void*)0);
}
if(message == "0") {
Serial.println("stopping temp");
t.stop(timer);
}
}
free(message);
}
Also, I know that lininio has something to offer using the Process library and stuff like this, but it's a completely new thing to me, as I've only played with arduino - actually programming in general - for under a year.
So, does anyone have some suggestions on how to structure MQTT topics for incoming events? How can I limit the memory used, as it seems a lot of Strings has to be implemented to analyse the data, which I really hate because of the memory use.
I'm running aJSON library on my MEGA, which has 250KB to use - and I love the way that I can just parse and construct JSON strings easily. But this is completely impossible on Yun because of limited memory. If you have a suggestion to use JSON with the MQTT you're very welcome Maybe there's something in the Bridge that I dont know about?
Hope you understand my questions, would be very pleased with some ideas Thank you in advance!