Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Working Arduino MQTT client that connects to Lelylan.
/* -------------------------------------------------------
* Install https://github.com/andreareginato/pubsubclient
* It sets the max packet size to 512 bytes.
* ------------------------------------------------------- */
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xA0, 0xA0, 0xBA, 0xAC, 0xAE, 0x12 };
/* device credentials */
char* deviceId = "51dfdc22d033a99721000001"; // set your device id (will be the MQTT client username)
char* deviceSecret = "8eb6a0c2d0bf6140e9b72c2eb7c2222b3252c5e4d765f7f67da810ad8da8742f"; // set your device secret (will be the MQTT client password)
/* device topics */
char* outTopic = "devices/51dfdc22d033a99721000001/set"; // where physical updates are published
char* inTopic = "devices/51dfdc22d033a99721000001/get"; // where lelylan updates are received
/* sample payload published to lelylan */
/* (to get the desired property-id go to the device settings and click on the type link) */
char* payload = "{\"properties\":[{ \"id\": \"518c92abd033a9473f000001\", \"value\": \"on\" }]}";
/* access settings */
byte server[] = { 192, 168, 1, 101 }; // MQTT server address
char* clientId = "mqtt-client-1"; // MQTT client id (max 23 bytes)
/* communication definition */
void callback(char* topic, byte* payload, unsigned int length); // subscription callback
EthernetClient ethClient; // ethernet client
PubSubClient client(server, 1883, callback, ethClient); // mqtt client
byte ip[] = { 192, 168, 1, 100 }; // LOCAL
/* arduino setup */
void setup() {
Serial.begin(9600);
delay(500);
Ethernet.begin(mac, ip);
Serial.print("Connected with IP: ");
Serial.println(Ethernet.localIP());
lelylanConnection(); // MQTT server connection
}
/* arduino loop */
void loop() {
lelylanConnection();
}
/* MQTT server connection */
void lelylanConnection() {
// add reconnection logics
if (!client.connected()) {
// connection to MQTT server
if (client.connect(clientId, deviceId, deviceSecret)) {
Serial.print("[OK] Connected with MQTT");
lelylanSubscribe(); // topic subscription
lelylanPublish(); // topic publishing
}
}
client.loop();
}
/* MQTT publish */
void lelylanPublish() {
client.publish(outTopic, payload);
}
/* MQTT subscribe */
void lelylanSubscribe() {
client.subscribe(inTopic);
}
// debug to show the received message
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Receiving subscribed message");
Serial.println(topic);
Serial.write(payload, length);
}
@7hillssathy

This comment has been minimized.

Copy link

commented Jun 7, 2014

Hi ya,

I am trying to learn arduino and MQTT can you please explain the code in detail. i want to use the code with my local MQTT server.

Regards
Satyam

@andreareginato

This comment has been minimized.

Copy link
Owner Author

commented Dec 1, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.