#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-) ComputhermRF rf = ComputhermRF(5, 255); //sender: /* bool on = true; String address = "123AB"; */ //---------- 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: //pinMode(2, OUTPUT); } //---------- CALLBACK ----------// void callback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i < length; i++) { Serial.print((char) payload[i]); } Serial.println(); Serial.println("-----------------------"); } //---------- LOOP ----------// void loop() { client.loop(); //sender: /* Serial.print(address + ": "); digitalWrite(2, LOW); rf.sendMessage(address, on); digitalWrite(2, HIGH); Serial.println(on?"ON":"OFF"); on = !on; delay(5000); */ //receiver: if (rf.isDataAvailable()) { computhermMessage msg = rf.getData(); Serial.println("New message caught. Address: " + msg.address + " command: " + msg.command); /* String payload = "\""; payload += msg.address; payload += "\":{\"state\":"; payload += msg.command; payload += "}"; // Send payload char attributes[100]; payload.toCharArray( attributes, 100 ); //client.publish( (char *)debug_topic.c_str(), attributes ); client.publish(topic, attributes); */ 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("MQTT topic: "); Serial.print ( topicattrib ); Serial.print (" Message: "); Serial.println ( attributes ); } }