#include extern "C" { #include "freertos/FreeRTOS.h" #include "freertos/timers.h" } #include #include #include // #include // #include // #define Relay1 2 #define Relay2 15 #define Relay3 5 #define Relay4 4 #define Key1 36 #define Key2 39 #define Key3 27 #define Key4 14 #define oneWireBus 13 //const int oneWireBus = 13; OneWire oneWire(oneWireBus); // Configurez une instance oneWire pour communiquer avec n'importe quel appareil OneWire DallasTemperature sensors(&oneWire); // Passer notre référence oneWire au capteur de température Dallas 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 // WiFi const char *ssid = "B"; // Enter your WiFi name const char *password = "Bac"; // Enter WiFi password // MQTT Broker const char *mqtt_broker = "192.168.3.105"; // or your MQTT broker IP/Domain const char *topic_command = "KC868-A4/12/set"; // if use by KBOX android phone app, use relay4/xxxxxx/set for command const char *topic_state = "KC868-A4/123456/state"; // if use by KBOX android phone app, use relay4/xxxxxx/state for feedback state #define MQTT_PUB_TEMP "esp32/ds18b20/temperature" const char *mqtt_username = ""; const char *mqtt_password = ""; const int mqtt_port = 1883; unsigned long previousMillis = 0; // Stores last time temperature was published const long interval = 5000; // Interval at which to publish sensor readings float temperature1; // Valeur de température AsyncMqttClient mqttClient; TimerHandle_t mqttReconnectTimer; TimerHandle_t wifiReconnectTimer; WiFiClient espClient; PubSubClient client(espClient); void connectToMqtt() { Serial.println("Connecting to MQTT..."); mqttClient.connect(); } void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent); } void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { Serial.println("Disconnected from MQTT."); if (WiFi.isConnected()) { xTimerStart(mqttReconnectTimer, 0); } } void onMqttPublish(uint16_t packetId) { Serial.println("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } 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); // Start the DS18B20 sensor sensors.begin(); // Set software serial baud to 115200; Serial.begin(115200); mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast(connectToMqtt)); mqttClient.onConnect(onMqttConnect); mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(mqtt_broker, mqtt_port); // 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, "carte KC868-A4 connecté"); 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() { unsigned long currentMillis = millis(); // Every X number of seconds (interval = 10 seconds) // it publishes a new MQTT message if (currentMillis - previousMillis >= interval) { // Save the last time a new reading was published previousMillis = currentMillis; // New temperature readings sensors.requestTemperatures(); // Temperature in Celsius degrees temperature1 = sensors.getTempCByIndex(0); // Temperature in Fahrenheit degrees //temp = sensors.getTempFByIndex(0); // Publish an MQTT message on topic esp32/ds18b20/temperature uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temperature1).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_TEMP); Serial.println(packetIdPub1); Serial.printf("Message: %.2f /n", sensors.getTempCByIndex(0)); } 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()); }