#include #include #include // WiFi const char *ssid = "FlogiWRT320N"; // Enter your WiFi name const char *password = "flogiwifirouterPWD222"; // Enter WiFi password // MQTT Broker const char *mqtt_broker = "192.168.1.122"; const char *topic = "wifito868gw/computherm"; const char *mqtt_username = "flogihamqtt"; const char *mqtt_password = "flogiMQTTpwd222"; const int mqtt_port = 1883; WiFiClient espClient; PubSubClient client(espClient); //ComputhermRF(receiver: GPIO5-D1, sender:GPIO4-D2) ComputhermRF rf = ComputhermRF(5, 4); //---------- SETUP ----------// void setup() { // 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 = "esp8266-wifito868gw-"; 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("Mqtt broker connected"); } else { Serial.print("failed with state "); Serial.print(client.state()); delay(2000); } } // publish and subscribe client.publish(topic, "OFF"); client.subscribe(topic); //Computherm Serial.println(); rf.startReceiver(); Serial.println("Computherm receiver started."); //sender GPIO2 - Adás idejére az ESP alaplapi kék ledjét bekapcsoljuk, ezért outputra állítjuk: pinMode(2, OUTPUT); } //---------- CALLBACK ----------// void callback(char *topic, byte *payload, unsigned int length) { // Conver the incoming byte array to a string payload[length] = '\0'; // Null terminator used to terminate the char array String message = (char*)payload; Serial.print("Message arrived in MQTT topic: ["); Serial.print(topic); Serial.print("] Message: ["); Serial.print(message); Serial.println("]"); // Split the readString by a pre-defined delimiter in a simple way. ';'(percentage) is defined as the delimiter in this project. int delimiter, delimiter_1, delimiter_2; delimiter = message.indexOf(";"); delimiter_1 = message.indexOf(";", delimiter + 1); delimiter_2 = message.indexOf(";", delimiter_1 +1); // Define variables to be executed on the code later by collecting information from the readString as substrings. String inmqttmsg = message.substring(delimiter + 1, delimiter_1); String ttid = message.substring(delimiter_1 + 1, delimiter_2); bool vez = 0; //sender: if(inmqttmsg == "ON"){ vez = 1; } if(inmqttmsg == "OFF"){ vez = 0; } Serial.print("RF message sent: ID: ["); Serial.print(ttid + "] Message: ["); digitalWrite(2, LOW); rf.sendMessage(ttid, vez); digitalWrite(2, HIGH); Serial.print(inmqttmsg); Serial.println("]"); String topicmod = topic; topicmod += "/"; topicmod += ttid; char topicattrib[100]; topicmod.toCharArray( topicattrib, 100 ); //String payload = inmqttmsg; // Send payload char attributes[100]; inmqttmsg.toCharArray( attributes, 100 ); client.publish(topicattrib, attributes); Serial.print("Message sent in MQTT topic: ["); Serial.print ( topicattrib ); Serial.print ("] Message: ["); Serial.print ( attributes ); Serial.println ("]"); } //---------- LOOP ----------// void loop() { client.loop(); //receiver: if (rf.isDataAvailable()) { computhermMessage msg = rf.getData(); Serial.println("New RF message caught. Address: [" + msg.address + "] command: [" + msg.command + "]"); //Create new topic String topicmod = topic; topicmod += "/"; topicmod += msg.address; char topicattrib[100]; topicmod.toCharArray( topicattrib, 100 ); String payload = msg.command; // Send payload char attributes[100]; payload.toCharArray( attributes, 100 ); client.publish(topicattrib, attributes); Serial.print("Message sent in MQTT topic: ["); Serial.print ( topicattrib ); Serial.print ("] Message: ["); Serial.print ( attributes ); Serial.println ("]"); } }