Meeo over MQTT
At its core, Meeo runs on top of the MQTT protocol. If you need to make use of MQTT in your project, it is easy to use Meeo as a backend service! Instead of deploying a cloud-based MQTT server or using an local/offline MQTT server (lame), use Meeo’s MQTT server!
To get started using Meeo over MQTT, make sure that you have read the guide Meeo Credentials.
To connect to Meeo MQTT server, use mq.meeo.io
. Use port 1883
to connect from your devices or any TCP-capable device. This is the standard port for MQTT. If you are going to develop an application using Javascript (i.e. NodeJS, MQTT.js), connect through port 443
or 80
via Websockets.
Test Meeo using MQTT.fx
MQTT.fx is one of the many tools out there that is used for MQTT. Download the latest version on this link: http://www.mqttfx.org/.
After installing the app, run the app. The first thing you need to do is to add a “Connection Profile” for Meeo. To do this, navigate through the application and click/press the “Settings” button (it is represented with a gear icon). The “Edit Connection Profile” box should pop-up. Navigate through it and click/press the “Add” button. On the “Profile Name” and “Broker Address” field, type: mq.meeo.io
. On the “Client ID” field, just click/press the “Generate” button near it. Next, click/press the “User Credentials” tab. Enter your Meeo Credentials (namespace
and accesskey
) to the “Username” and “Password” field respectively. When everything is okay, click/press the “Apply” button and then click/press the “OK” button.
You can now connect to the “Connection Profile” you just created by selecting the profile name on the combobox and press the “Connect” button. You will be able to know if you are connected by simply looking at the status light. The image below shows that the tool has connected to the server and the status light is green.
To publish or send data to one of your widgets, make sure that you are in the “Publish” tab. Simply supply the MQTT topic of the widget and press the “Publish” button. The image below shows that a payload of 0
was published on the MQTT topic tdf-b20hfmlb/kitchen-lights
.
To subscribe or receive data from one of your widgets, make sure that you are in the “Subscribe” tab. Simply supply the MQTT topic of the widget and press the “Subscribe” button. The image below shows that a data of 0
was received from the MQTT topic tdf-b20hfmlb/kitchen-lights
.
Use Meeo via Web (MQTT.js)
Assuming that you know Javascript and you’re familiar with NodeJS, using Meeo is easy with the MQTT.js library!
Make sure that you have NodeJS installed. Next, assuming that you have package.json
on your project directory, you can now install the library by running this line:
npm install mqtt --save
If there are no errors on the installation, you can now use the library! The code block below shows how to configure the library to use Meeo:
var mqtt = require('mqtt');var client = mqtt.connect('mqtts://mq.meeo.io', {
clientId: "Meeo-1ason9djanajkfn",
username: "my-namespace",
password: "my-access-key"
});
Use Meeo via Android (Eclipse Paho)
Assuming that you know how to develop an Android Application, using Meeo is easy with the Eclipse Paho library!
The first thing you need to do is to add the library to both of your top-level and module-level build.gradle
files. On the top-level build file, add the line below under the tworepositories
block:
maven {
url “https://repo.eclipse.org/content/repositories/paho-releases/"
}
On the project-level build file, add the lines below under the dependencies block:
compile ‘org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0’
compile ‘org.eclipse.paho:org.eclipse.paho.android.service:1.1.0’
Now that your build.gradle
files are setup, you need to add some lines to your AndroidManifest.xml
file. Add the following permissions:
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.WAKE_LOCK”/>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />
Next, add the Eclipse Paho service below the activity tag:
<service android:name=”org.eclipse.paho.android.service.MqttService”/>
With all the setup done, you can now use the library! To configure the library to work with Meeo, follow the code block below and add it on MainActivity.java
:
MqttAndroidClient mqttAndroidClient = new MqttAndroidClient( getApplicationContext(), "tcp://mq.meeo.io:1883", MqttClient.generateClientId() );
mqttAndroidClient.setCallback(this); //Make sure that you implement MqttCallbackExtended methods on your Activity...MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect( true );
mqttConnectOptions.setCleanSession( false );
mqttConnectOptions.setUserName( "my-namespace" );
mqttConnectOptions.setPassword( "my-access-key".toCharArray() );
...
try {
MqttAndroidClient.connect( mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess( IMqttToken asyncActionToken ) {
Toast.makeText( getApplicationContext(), "Connected to server", Toast.LENGTH_SHORT ).show();
}
@Override
public void onFailure( IMqttToken asyncActionToken, Throwable exception ) {
Toast.makeText( getApplicationContext(), "Cannot connect to server", Toast.LENGTH_SHORT ).show();
}
} );
}
Use Meeo via iOS (CocoaMQTT)
Assuming that you know how to develop an iOS Application with Swift, using Meeo is easy with the CocoaMQTT library!
Setting up the CocoaMQTT library is easy to do with CocoaPods
. You can check how to setup the library here. To install CocoaPods
, follow the installation guide here.
When you have installed the library though CocoaPods
, it will generate a .xcworkspace
file. Make sure that you open that file in Xcode. You should be able to use the classes inside the library.
The code block below shows how to configure the library to work with Meeo:
let clientID = "Meeo-" + (UIDevice.current.identifierForVendor?.uuidString)!var mqtt: CocoaMQTT? = CocoaMQTT(clientID: clientID, host: "mq.meeo.io", port: 1883)
mqtt!.username = "my-namespace"
mqtt!.password = "my-access-key"
mqtt!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
mqtt!.connect()