Project ini merupakan contoh penggunaan Geeknesia sebagai sarana komunikasi perintah antar 2 device. Pada kasus ini komunikasi antara dua buah ESP8266 yang dimana masing-masing ESP8266 diberi output Led sebagai indikator sekaligus perintah triger antar device.


Misalkan kondisi awal LED ke-2 device adalah "OFF" ,kemudian salah satu diantara device diperintah triger, misal device1 diberi perintah triger maka LED device1 "ON" dan memberi perintah untuk menyalakan LED device2, ketika LED device2 "ON" device2 memberi perintah ke device1 untuk mematikan LED device1 dan seterusnya, maka seolah-olah LED akan terlihat "ON" dan "OFF" secara bergantian.

Hal pertama yang harus dilakukan adalah Create Project,Create Gateway, dan Create Device, untuk mendapatkan device details seperti Device Id,Username,Password,API Key, dan Publish Topic.


Pada saat membuat gateway baru yang diperlukan pada aplikasi ini adalah pengisian pada kolom "Control" yang berfungsi untuk meng-subrcibe atau men-triger dari cloud ke device.


Kemudian buka program Arduino IDE dan upload program di bawah ini ke Esp8266 ke-1 atau yang dianggap sebagai device1.

 

//Code Device_1
#include 
#include
//Setting wifi id
const char *ssid = "SSID"; // SSID
const char *pass = "Password"; // Password
const int ledPin = 2; // pin output led
int buttonState=0;
String flag;
int data;
#define BUFFER_SIZE 100
//id details device 1
char *deviceId1 = "device-b77d83a298fb94dcd616403f4c3xxxxx";
char *deviceUsername1 = "493d307cf8946500032825e2b25xxxxx";
char *devicePassword1 = "6c8a81ed54a7acfda9ac5fa7d21xxxxx";
char *deviceApiKey1 = "b09ce3fc6532798203092f7adfexxxxx";
char *deviceTopic1 = "topic-b77d83a298fb94dcd616403f4c3fxxxxx";
String deviceCredential1 = String(deviceUsername1) + ":" + String(devicePassword1);
//id details device 2
char *deviceId2 = "device-ac53d5613c42445e9fa01543996xxxxx";
char *deviceUsername2 = "daa364a0dce8075cd2ba891894fxxxxx";
char *devicePassword2 = "58ba26c9a98b6f3da5867124023xxxxx";
char *deviceApiKey2 = "4d291b91bf83b203b41a579c61exxxxx";
char *deviceTopic2 = "topic-ac53d5613c42445e9fa01543996xxxxx";
String deviceCredential2 = String(deviceUsername2) + ":" + String(devicePassword2);

// IP Geeknesia.com
WiFiClient wclient;
PubSubClient client(wclient, (String) "geeknesia.com", 1883);

void callback(const MQTT::Publish& pub) {
Serial.print(pub.topic());
Serial.print(" => ");
Serial.println(pub.payload_string());
String command = pub.payload_string();
if(command.equals("on")){
digitalWrite(ledPin, HIGH);
Serial.println("ON");
delay(5000);
senddata(deviceId1,deviceCredential1,"status", 1);
sendcontrol(deviceTopic2,"on");
Serial.println("send command on");
}
if(command.equals("off")){
digitalWrite(ledPin, LOW);
Serial.println("OFF");
delay(5000);
senddata(deviceId1, deviceCredential1,"status", 0);
sendcontrol(deviceTopic2,"off");
Serial.println("send command off");
}
Serial.println("End of callback");
}
void setup() {
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
pinMode(ledPin, OUTPUT);
}
void senddata(char* topic,String credential, char *var1, int nilai1 ){
String pubString = "{\"code\":\"";
pubString += credential;
pubString +="\",";
pubString +="\"attributes\":{\"";
//pubString +=String(var1)+"\":\""+nilai1+"\",\"";
//pubString +=String(var2)+"\":\""+nilai2+"\",\"";
pubString +=String(var1)+"\":\""+nilai1+"\"";
pubString +="}}";
char message_buff[pubString.length()+1];
pubString.toCharArray(message_buff, pubString.length()+1);
//Serial.println(message_buff);
client.publish("iot/data",pubString);

}
void sendcontrol(char* topic, String action){
client.publish(topic,action);
}

void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect(deviceId1, "iot/will",0,0,deviceId1)) {
client.set_callback(callback);
if (client.subscribe(deviceTopic1)) {
Serial.println("subscribed on " + (String) deviceTopic1);
}
}
}
if (client.connected())
client.loop();
}
}

 

Upload program di bawah ini ke Esp8266 ke-2 atau yang dianggap sebagai device2.

 

//Code Device_2
#include 
#include
//Setting wifi id
const char *ssid = "SSID"; // SSID
const char *pass = "Password"; // Password
const int ledPin = 2; // pin output led
int buttonState=0;
String flag;
int data;
#define BUFFER_SIZE 100
//id details device 1
char *deviceId2 = "device-b77d83a298fb94dcd616403f4c3xxxxx";
char *deviceUsername2 = "493d307cf8946500032825e2b25xxxxx";
char *devicePassword2 = "6c8a81ed54a7acfda9ac5fa7d21xxxxx";
char *deviceApiKey2 = "b09ce3fc6532798203092f7adfexxxxx";
char *deviceTopic2 = "topic-b77d83a298fb94dcd616403f4c3fxxxxx";
String deviceCredential2 = String(deviceUsername2) + ":" + String(devicePassword2);

//id details device 2
char *deviceId1 = "device-ac53d5613c42445e9fa01543996xxxxx";
char *deviceUsername1 = "daa364a0dce8075cd2ba891894fxxxxx";
char *devicePassword1 = "58ba26c9a98b6f3da5867124023xxxxx";
char *deviceApiKey1 = "4d291b91bf83b203b41a579c61exxxxx";
char *deviceTopic1 = "topic-ac53d5613c42445e9fa01543996xxxxx";
String deviceCredential1 = String(deviceUsername1) + ":" + String(devicePassword1);

// IP Geeknesia.com
WiFiClient wclient;
PubSubClient client(wclient, (String) "geeknesia.com", 1883);

void callback(const MQTT::Publish& pub) {
Serial.print(pub.topic());
Serial.print(" => ");
Serial.println(pub.payload_string());
String command = pub.payload_string();
if(command.equals("on")){
digitalWrite(ledPin, HIGH);
Serial.println("ON");
delay(5000);
senddata(deviceId1,deviceCredential1,"status", 1);
sendcontrol(deviceTopic2,"on");
Serial.println("send command on");
}
if(command.equals("off")){
digitalWrite(ledPin, LOW);
Serial.println("OFF");
delay(5000);
senddata(deviceId1, deviceCredential1,"status", 0);
sendcontrol(deviceTopic2,"off");
Serial.println("send command off");
}
Serial.println("End of callback");
}
void setup() {
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
pinMode(ledPin, OUTPUT);
}
void senddata(char* topic,String credential, char *var1, int nilai1 ){
String pubString = "{\"code\":\"";
pubString += credential;
pubString +="\",";
pubString +="\"attributes\":{\"";
//pubString +=String(var1)+"\":\""+nilai1+"\",\"";
//pubString +=String(var2)+"\":\""+nilai2+"\",\"";
pubString +=String(var1)+"\":\""+nilai1+"\"";
pubString +="}}";
char message_buff[pubString.length()+1];
pubString.toCharArray(message_buff, pubString.length()+1);
//Serial.println(message_buff);
client.publish("iot/data",pubString);

}
void sendcontrol(char* topic, String action){
client.publish(topic,action);
}

void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect(deviceId1, "iot/will",0,0,deviceId1)) {
client.set_callback(callback);
if (client.subscribe(deviceTopic1)) {
Serial.println("subscribed on " + (String) deviceTopic1);
}
}
}
if (client.connected())
client.loop();
}
}

 

Jika sudah berhasil ter-upload,kemudian wiring masih-masing Esp8266 seperti rangkaian di bawah ini:

Catatan:Button tidak perlu diwiring jika tidak diperlukan.


Untuk mengetahui berhasil atau tidak aplikasi yang dibuat bisa dilihat di Geeknesia.com dan login, pilih project yang telah disiapkan untuk aplikasi device to device ini. Lihat gambar hasil tes di bawah ini.

Pertama yang harus dilakukan untuk menjalankan aplikasi ini adalah triger salah satu button "ON" seperti yang telah ditunjukan tanda panah warna hijau di atas, maka setelah salah satu ditriger device1 dan device2 akan saling memberi perintah untuk menyalakan atau mematikan Led pada output yang telah dirangkai secara terus menerus selama koneksi internet tetap terhubung.