Hi, friend! I just wrote that i tried to use the ip address of my device instead HOSTNAMEOFDEVICE. I thought maybe problem is in my eps8266 device? I use a NodeMCU V3 with the last basic firmware. It’s code, which i used.
#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
/* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
//Only edit the settings in this section
/* WIFI Settings /
// Name of wifi network
const char ssid = “IoT”;
// Password to wifi network
const char* password = “Qq111111”;
/* Web Updater Settings /
// Host Name of Device
const char host = “MK-SprinklerSystem1”;
// Path to access firmware update page (Not Neccessary to change)
const char* update_path = “/firmware”;
// Username to access the web update page
const char* update_username = “admin”;
// Password to access the web update page
const char* update_password = “password”;
/* MQTT Settings /
// Topic which listens for commands
char subscribeTopic = “MK-SmartHouse/utilities/MK-SprinklerSystem1”;
//MQTT Server IP Address
const char* server = “192.168.100.4”;
//Unique device ID
const char* mqttDeviceID = “MK-SmartHouseDevice1”;
/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
int channel1 = D4;
//webserver
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
//MQTT
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
//Connect to WiFI and MQTT
void connect();
//Setup pins, wifi, webserver and MQTT
void setup()
{
// set pin modes
pinMode(channel1, OUTPUT);
digitalWrite(channel1, LOW);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.begin(server, net);
client.onMessage(messageReceived);
connect();
MDNS.begin(host);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService(“http”, “tcp”, 80);
}
//Connect to wifi and MQTT
void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
while (!client.connect(mqttDeviceID))
{
delay(1000);
}
client.subscribe(subscribeTopic);
}
void loop()
{
// MQTT Loop
client.loop();
delay(10);
// Make sure device is connected
if(!client.connected())
{
connect();
}
httpServer.handleClient();
}
// Change the state of a relay based on the MQTT Message
void messageReceived(String &topic, String &payload)
{
String msgString = payload;
if (msgString == “CH1ON”)
{
digitalWrite(channel1, HIGH);
delay(250);
}
else if (msgString == “CH1OFF”)
{
digitalWrite(channel1, LOW);
delay(250);
}
}