Last active
December 24, 2015 01:39
-
-
Save andreareginato/6725485 to your computer and use it in GitHub Desktop.
Working Arduino MQTT client that connects to Lelylan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ------------------------------------------------------- | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A bit late. http://lelylan.github.io/lab-projects/arduino-ethernet-light/#code