The Tinamous MQTT server provides support for status messages, measurements, alerts as well as pub/sub for your own messages. Status messages, measurements and alerts posted through the web site or REST API are published to the MQTT server and can be subscribed to.
Naturally MQTT doesn't support the full Tinamous API, their is no support for clearing messages, getting historical measurements/status posts or adding users, however it does provide a real time stream of messages, measurements and alerts which is ideal for many small connected devices and saves you having to learn the Tinamous REST API.
The server also does not support the full features of the MQTT protocol, please keep an eye on the Tinamous Trello page for future developments.
// -----------------------------------------------------------------
// Add the Arduino MQTT library from the library manager
// See https://github.com/256dpi/arduino-mqtt for more details.
// This example uses the WiFi101 library as well running on
// an Arduino MKR 1000
// -----------------------------------------------------------------
// Unsecure (port 1833)
//WiFiClient networkClient;
// SSL based connection (port 8883)
WiFiSSLClient networkClient;
// Specify the buffer size as the default is fairly small.
MQTTClient mqttClient(2048);
void setup() {
// Ensure your network is connected.
WiFi.begin(ssid, pass);
// replace 'demo' with your Tinamous account name.
// Connect on port 1883 for UNSECURE connection
// or use 8883 for SSL secured if your platform supports it.
// Note the different WiFiClient needs to be used.
mqttClient.begin(demo.tinamous.com, 8883, networkClient);
// If you subscribe to to MQTT topics, add the message handler.
mqttClient.onMessage(messageReceived);
connect();
}
void connect() {
// Connect as the device "OfficeFans" to the Tinamous account "demo"
// using the "OfficeFansPassword" as the password.
// Username and password should have been set when adding the device through Tinamous devices page.
if (mqttClient.connect("ClientId-23", "OfficeFans.demo", "OfficeFansPassword")) {
// Connected
return;
}
// Handle connection failure.
// Error code -10 (LWMQTT_CONNECTION_DENIED) is a username/password error.
if (mqttClient.lastError() == LWMQTT_CONNECTION_DENIED) {
Serial.println("Access denied. Check your username and password. Username should be 'DeviceName.AccountName' e.g. MySensor.MyHome");
}
}
// Publish status messages.
unsigned long lastMessageSent = 0;
void loop() {
// Once a minute publish a message
if (millis() - lastMessageSent > 60000) {
// Publish a "Hello World" message to the timamous status Timeline
mqttClient.publish("/Tinamous/V1/Status", "Hello World");
lastMessageSent = millis();
}
}
// Receive status post messages sent To this deivce.
// To send a message, from the Tinamous Timeline page.
// enter a message in the "Post a status message... "text box.
// e.g. "@OfficeFans Turn on the fans!"
void setup() {
// ... WiFi and MQTT connect here....
// Be sure to handle onMessage
mqttClient.onMessage(messageReceived);
// Subscrie to the status to for messages sent
// directly To the device "OfficeFans" (i.e. this device)
mqttClient.subscribe("/Tinamous/V1/Status.To/OfficeFans");
}
void messageReceived(String &topic, String &payload) {
Serial.println("Message from Tinamous on topic: " + topic + " - " + payload);
}
You can publish measurements in different ways, either as a simple measurement value to the appropriate device/channel/field or as a json object with multiple fields.
void publishMeasurements() {
// Make sure that the client was set-up with a decent size buffer
// as the SenML message can overrin the default 128 bytes quickly.
// And do one as senml...
String senml = "{'e':[";
// First sensor
senml = senml + "{'n':'Light',";
senml = senml + "'v':100,";
senml = senml + "'u':'lux'},";
// Second sensor
senml = senml + "{'n':'VOC',";
senml = senml + "'v':'1001',";
senml = senml + "'u':'ppm'}";
senml = senml + "]}";
// Senml should look like...
{'e':[{'n':'Light','v':100,'u':'lux'},{'n':'VOC','v':'1001','u':'ppm'}]}
mqttClient.publish("/Tinamous/V1/Measurements/SenML", senml);
}
void publishMeasurements() {
// Make sure that the client was set-up with a decent size buffer
// as the message can overrin the default 128 bytes quickly.
// Plain old JSON.
String json = "{'Temperature':";
json = json + "23.5";
json = json + ",'Humidity':";
json = json + "'60'";
json = json + "}";
// json should look like...
{'Temperature':23.5,'Humidity':'60'}
mqttClient.publish("/Tinamous/V1/Measurements/Json", json);
}
Alerts support 3 levels, "Critical", "Error" and "Warning". With the alert a message is also added.
Commands are instructions to devices to do something, i.e. switch a fan on.