Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Arduino demo source code for KC868-A4]-14 MQTT demo
#1
Code:
// MQTT demo source code for KC868-A4
// Remote control relay ON/OFF by any MQTT broker
// Digital input use by switch button for ON/OFF and demo show state feedback
// You can use by MQTTBOX for test or KinCony KBOX android phone app

#define Relay1  2
#define Relay2  15
#define Relay3  5
#define Relay4  4

#define Key1  36
#define Key2  39
#define Key3  27
#define Key4  14


int KEY_NUM1 = 0;    //key_1 value
int KEY_NUM2 = 0;    //key_2 value
int KEY_NUM3 = 0;    //key_3 value
int KEY_NUM4 = 0;    //key_4 value

#include <string.h>
#include <WiFi.h>
#include <PubSubClient.h>


// WiFi
const char *ssid = "KinCony"; // Enter your WiFi name
const char *password = "a12345678";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "iot.kincony.com";  // or your MQTT broker IP/Domain
const char *topic_command = "relay4/123456/set";  // if use by KBOX android phone app, use relay4/xxxxxx/set      for command
const char *topic_state = "relay4/123456/state";  // if use by KBOX android phone app, use relay4/xxxxxx/state    for feedback state
const char *mqtt_username = "mqtt";
const char *mqtt_password = "123";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  pinMode(Relay1,OUTPUT);   //Relay1 IO2
  pinMode(Relay2,OUTPUT);   //Relay2 IO15
  pinMode(Relay3,OUTPUT);   //Relay3 IO2
  pinMode(Relay4,OUTPUT);   //Relay4 IO2

  pinMode(Key1,INPUT);  //Digital input1 IO36
  pinMode(Key2,INPUT);  //Digital input2 IO39
  pinMode(Key3,INPUT);  //Digital input3 IO27
  pinMode(Key4,INPUT);  //Digital input4 IO14

  pinMode(2, OUTPUT);   //relay1
  pinMode(15, OUTPUT);  //relay2
  pinMode(4, OUTPUT);   //relay3
  pinMode(5, OUTPUT);   //relay4

  digitalWrite(2, LOW);   // turn the LED OFF (HIGH is the voltage level)
  digitalWrite(15,LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

 
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
     String client_id = "esp32-client-";
     client_id += String(WiFi.macAddress());
     Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
     if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
         Serial.println("Public emqx mqtt broker connected");
     } else {
         Serial.print("failed with state ");
         Serial.print(client.state());
         delay(2000);
     }
}
// publish and subscribe
client.publish(topic_state, "Hi I am KC868-A4");
client.subscribe(topic_command);
}

void callback(char *topic_command, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic_command);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
     Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");

if (!strncmp((char *)payload, "{\"relay1\":{\"on\":1}}", length)) digitalWrite(Relay1, HIGH);
else if (!strncmp((char *)payload, "{\"relay1\":{\"on\":0}}", length)) digitalWrite(Relay1, LOW);

if (!strncmp((char *)payload, "{\"relay2\":{\"on\":1}}", length)) digitalWrite(Relay2, HIGH);
else if (!strncmp((char *)payload, "{\"relay2\":{\"on\":0}}", length)) digitalWrite(Relay2, LOW);

if (!strncmp((char *)payload, "{\"relay3\":{\"on\":1}}", length)) digitalWrite(Relay3, HIGH);
else if (!strncmp((char *)payload, "{\"relay3\":{\"on\":0}}", length)) digitalWrite(Relay3, LOW);

if (!strncmp((char *)payload, "{\"relay4\":{\"on\":1}}", length)) digitalWrite(Relay4, HIGH);
else if (!strncmp((char *)payload, "{\"relay4\":{\"on\":0}}", length)) digitalWrite(Relay4, LOW);



if (!strncmp((char *)payload, "{\"relay4\":{\"read\":1}}", length)) {   //for app read all state, replace with your read relay and input code
      client.publish(topic_state, "{\"relay1\":{\"on\":0},\"relay2\":{\"on\":0},\"relay3\":{\"on\":0},\"relay4\":{\"on\":0},\"input1\":{\"on\":0},\"input2\":{\"on\":0},\"input3\":{\"on\":0},\"input4\":{\"on\":0}}");
    }


}

void loop() {
  ScanKey1();            //scan key1
  ScanKey2();            //scan key2
  ScanKey3();            //scan key3
  ScanKey4();            //scan key4
client.loop();
}

void ScanKey1()           
{
  String temp="{\"relay1\":{\"on\":";

  KEY_NUM1 = 0;                     
  if(digitalRead(Key1) == LOW)         //key1 pressed
  {
    delay(20);                        //anti-interference
    if(digitalRead(Key1) == LOW)       //key1 pressed again
    {
      KEY_NUM1 = 1;                  
      while(digitalRead(Key1) == LOW); //wait remove hand
    }
  }
  if(KEY_NUM1 == 1)              //if key1 pressed
  {  
    digitalWrite(Relay1,!digitalRead(Relay1));    // toggle relay1 state
    if(digitalRead(Relay1) == LOW) temp=temp+"0}}";
    if(digitalRead(Relay1) == HIGH) temp=temp+"1}}";

    client.publish(topic_state, temp.c_str());
  } 
}

void ScanKey2()           
{
  String temp="{\"relay2\":{\"on\":";
  KEY_NUM2 = 0;                     
  if(digitalRead(Key2) == LOW)         //key2 pressed
  {
    delay(20);                        //anti-interference
    if(digitalRead(Key2) == LOW)       //key2 pressed again
    {
      KEY_NUM2 = 1;                  
      while(digitalRead(Key2) == LOW); //wait remove hand
    }
  }
  if(KEY_NUM2 == 1)              //if key2 pressed
  {  
    digitalWrite(Relay2,!digitalRead(Relay2));    // toggle relay2 state
    if(digitalRead(Relay2) == LOW) temp=temp+"0}}";
    if(digitalRead(Relay2) == HIGH) temp=temp+"1}}";
    client.publish(topic_state, temp.c_str());
  }
}

void ScanKey3()           
{
  String temp="{\"relay3\":{\"on\":";
  KEY_NUM3 = 0;                     
  if(digitalRead(Key3) == LOW)         //key3 pressed
  {
    delay(20);                        //anti-interference
    if(digitalRead(Key3) == LOW)       //key3 pressed again
    {
      KEY_NUM3 = 1;                  
      while(digitalRead(Key3) == LOW); //wait remove hand
    }
  }
  if(KEY_NUM3 == 1)              //if key3 pressed
  {  
    digitalWrite(Relay3,!digitalRead(Relay3));    // toggle relay3 state
    if(digitalRead(Relay3) == LOW) temp=temp+"0}}";
    if(digitalRead(Relay3) == HIGH) temp=temp+"1}}";
    client.publish(topic_state, temp.c_str());
  }
}

void ScanKey4()           
{
  String temp="{\"relay4\":{\"on\":";
  KEY_NUM4 = 0;                     
  if(digitalRead(Key4) == LOW)         //key4 pressed
  {
    delay(20);                        //anti-interference
    if(digitalRead(Key4) == LOW)       //key4 pressed again
    {
      KEY_NUM4 = 1;                  
      while(digitalRead(Key4) == LOW); //wait remove hand
    }
  }
  if(KEY_NUM4 == 1)              //if key4 pressed
  {  
    digitalWrite(Relay4,!digitalRead(Relay4));    // toggle relay4 state
    if(digitalRead(Relay4) == LOW) temp=temp+"0}}";
    if(digitalRead(Relay4) == HIGH) temp=temp+"1}}";
    client.publish(topic_state, temp.c_str());
  }
}
   

.zip   a4-mqtt.zip (Size: 2.05 KB / Downloads: 392)
Reply
#2
[pièce jointe=2033] bonjour je recherche un code .ino pour communiquer avec home assistant (sans tasmota ni esphome) 
j'ai essayé celui ci mais je n'ai rien qui s'affiche dans le broker ?

[pièce jointe=2034]
[pièce jointe=2035]


Attached Files Image(s)
       
Reply
#3
can you write in English? sorry, i don't understand your question.
Reply
#4
sorry
I am looking for an .ino code to communicate with home assistant (without tasmota or esphome)
I tried this one but I have nothing that appears in the broker?
Reply
#5
which product board you are using?
Reply
#6
(03-04-2023, 08:14 AM)administrateur Wrote: quelle carte de produit vous utilisez?

kc868-A4 rev 1.2.3
Reply
#7
you can download "KCS" firmware, so that can integrate to home assistant by MQTT without ESPHome.
here is online guide: https://www.kincony.com/kcs-firmware-esp32-board.html
here is video tour: https://youtu.be/gJTUb1SGnII
"KCS" the newest firmware download: https://www.kincony.com/forum/showthread.php?tid=2542
"KCS" MQTT document: https://www.kincony.com/forum/showthread.php?tid=2529
Reply
#8
(03-04-2023, 11:52 AM)admin Wrote: you can download "KCS" firmware, so that can integrate to home assistant by MQTT without ESPHome.
here is online guide: https://www.kincony.com/kcs-firmware-esp32-board.html
here is video tour: https://youtu.be/gJTUb1SGnII
"KCS" the newest firmware download: https://www.kincony.com/forum/showthread.php?tid=2542
"KCS" MQTT document: https://www.kincony.com/forum/showthread.php?tid=2529
sorry
I created a program to manage my heating and I would like to upload the data in home assistant
I don't want to install kcs or espeasy or other firmware
 is it possible to upload the data in mqtt?
Reply
#9
ok, you can write your own arduino mqtt code to ESP32.
Reply
#10
(03-04-2023, 12:36 PM)admin Wrote: ok, you can write your own arduino mqtt code to ESP32.

I looked for a sample code but I couldn't find it.
the code above, if I enter my ip address of my mqtt server and user, psw I should be able to add them in home assistant?
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)