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());
}
}
a4-mqtt.zip (Size: 2.05 KB / Downloads: 387)