Device to Device Controlling



//
Posted on November 16, 2015 at 1:54 AM

//

Project percobaan pengiriman control command dari Device ke Device secara lansung




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 ESP 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 device 1 diberi perintah triger maka LED device1 "ON" dan memberi perintah untuk menyalakan LED device 2, ketika LED device 2 "ON" device 2 memberi perintah ke device 1 untuk mematikan LED device 1 dan seterusnya, maka seolah-olah LED akan terlihat "ON" dan "OFF" secara bergantian.  

//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 1
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();
  }
}

 

 


Leave a Comment:

Please Sign in First