• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Getting “fatal error: ESPmDNS.h: No such file or directory” when trying to compile the ESP8266 test exercise (Section 5.1 in the Smart Home course)

Q&A Forum › Category: Home Automation › Getting “fatal error: ESPmDNS.h: No such file or directory” when trying to compile the ESP8266 test exercise (Section 5.1 in the Smart Home course)
0 Vote Up Vote Down
Liam Woffindin asked 8 months ago

edited for format

It’s possible I downloaded the library and the dependencies in the wrong order. It asks me on page 152 to download them in a certain order. I’ve tried deleting the dependencies and reinstalling the libraries. I’ve tried removing the esp32 and 8266 board manager then reinstalling. Nothing is working for me I keep getting this error. Any thoughts?
Error in full (I’ve replaced sensitive info with REDACTED)

In file included from c:\Users\lREDACTED\Documents\Arduino\libraries\AsyncMqttClient\src/AsyncMqttClient.h:4,
from C:\Users\REDACTED\Dropbox\REDACTED\0 Arduino code\0 Smart home\firstattempt\firstattempt.ino:13:
c:\Users\REDACTED\Documents\Arduino\libraries\AsyncMqttClient\src/AsyncMqttClient.hpp:20:10: fatal error: ESPmDNS.h: No such file or directory
20 | #include <ESPmDNS.h>
| ^~~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: exit status 1

Full code (copy pasted from github, with my own stuff added, nothing has been changed)

Pastebin link

/*********
  Rui Santos
  Complete instructions at https://RandomNerdTutorials.com/smart-home-ebook/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>

#define WIFI_SSID "admin"
#define WIFI_PASSWORD "Trapped91!"

#define MQTT_HOST IPAddress(192, 168, 1, 162)
#define MQTT_PORT 1883
#define BROKER_USER "admin"
#define BROKER_PASS "Trapped91!"

AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;


unsigned long previousMillis = 0;   // Stores last time a message was published
const long interval = 10000;        // Interval at which to publish values

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  Serial.println("Connected to Wi-Fi.");
  connectToMqtt();
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("Disconnected from Wi-Fi.");
  mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  wifiReconnectTimer.once(2, connectToWifi);
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);

  // Subscribe to topic "led" when it connects to the broker
  uint16_t packetIdSub = mqttClient.subscribe("led", 2);
  Serial.print("Subscribing at QoS 2, packetId: ");
  Serial.println(packetIdSub);

  // Publish on the "test" topic with qos 0
  mqttClient.publish("test", 0, true, "test 1");
  Serial.println("Publishing at QoS 0");
 // Publish on the "test" topic with qos 1
  uint16_t packetIdPub1 = mqttClient.publish("test", 1, true, "test 2");
  Serial.print("Publishing at QoS 1, packetId: ");
  Serial.println(packetIdPub1);
  // Publish on the "test" topic with qos 2
  uint16_t packetIdPub2 = mqttClient.publish("test", 2, true, "test 3");
  Serial.print("Publishing at QoS 2, packetId: ");
  Serial.println(packetIdPub2);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");

  if (WiFi.isConnected()) {
    mqttReconnectTimer.once(2, connectToMqtt);
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  Serial.print("  qos: ");
  Serial.println(qos);
}

void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  // Do whatever you want when you receive a message

  // Save the message in a variable
  String receivedMessage;
  for (int i = 0; i < len; i++) {
    Serial.println((char)payload[i]);
    receivedMessage += (char)payload[i];
  }
  // Turn the LED on or off accordingly to the message content
  if (strcmp(topic, "led") == 0) {
    if (receivedMessage == "true"){
      digitalWrite(LED_BUILTIN, HIGH);
    }
    if (receivedMessage == "false"){
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
  Serial.println("Publish received.");
  Serial.print("  topic: ");
  Serial.println(topic);
  Serial.print("  qos: ");
  Serial.println(properties.qos);
  Serial.print("  dup: ");
  Serial.println(properties.dup);
  Serial.print("  retain: ");
  Serial.println(properties.retain);
  Serial.print("  len: ");
  Serial.println(len);
  Serial.print("  index: ");
  Serial.println(index);
  Serial.print("  total: ");
  Serial.println(total);
}

void onMqttPublish(uint16_t packetId) {
  Serial.println("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  pinMode (LED_BUILTIN, OUTPUT);

  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onSubscribe(onMqttSubscribe);
  mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onMessage(onMqttMessage);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  mqttClient.setCredentials(BROKER_USER, BROKER_PASS);


  connectToWifi();
}

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;

  // Publish an MQTT message on topic counter
  uint16_t packetIdPub1 = mqttClient.publish("counter", 1, true, String(random(0,20)).c_str());
  Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", "counter", packetIdPub1);
  }
}
Question Tags: Help
6 Answers
1 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
There is currently an issue with the original AsyncMQTTClient library.
We forked that library to fix the issue. We have a new download link for that library.
That download link is updated on the latest version of the eBook version 1.5 updated September 2, 2024.
 
Make sure you are using the latest version of the eBook (version 1.5) and use the new link on the new version to download the library.
Remove any previous libraries with the same name.
 
I hope this is clear.
Let me know if this solves the issue.
 
Regards,
Sara

0 Vote Up Vote Down
Liam Woffindin answered 8 months ago

Hello Sara, thank you for your help, it is much appreciated.
“Make sure you are using the latest version of the eBook (version 1.5)”
I confirm I’m using 1.5, I purchased it recently.

EDIT: I seem to be using the image inserter in this forum incorrectly because when I copy the imgur link into the inserter tool, it doesn’t work, so I’ve put a link to each screenshot where it is relevent. I don’t know what I’m doing wrong. I tried using the “embed” link and it didn’t seem to work either. I’ve just linked the url to each screenshot

I’ve made a public imgur post here with screenshots. Imgur link. As my OP suggests I had already removed and reinstalled all the libraries. I uninstalled the IDE but noticed that when reinstalled, it was all still present so I just did a complete wipe of all library, document and system files.
Still have the same issue

Here’s what I have found.

Searching reveals no library present…

https://imgur.com/S8aPSbV

But the libraries are still present in the documents folder
https://imgur.com/n4uRmXm

The dependencies are in the manager (I had removed them from the manager at the time I took the screenshot as I was troubleshooting)

https://imgur.com/WK0p5eP

When I try adding the .ZIP file again, the IDE recognises I’ve already done that and asks me to confirm the overwrite

https://imgur.com/sMgCxFA

I deleted all libraries, reinstalled them as before, still having issues. I closed down the IDE and restarted, made a new sketch, the library is still not present in the manager. There’s something it doesn’t like about the AsyncMqttClient library it seems

https://imgur.com/UiyqtWH

When I click “install” on the dependencies, I get the message that they’re also missing their own dependencies (I guess these are present in AsyncMqttClient?). It looks to me like there is something broken between the documents/arduino/libraries folder and the library manager.

https://imgur.com/IDee5zS

0 Vote Up Vote Down
Liam Woffindin answered 8 months ago

I’ve made progress (I think) but I’m not going to mark this as solved as I have yet to apply my changes to the rest of the section (5.1, using node red with the ESP8266 etc). And I’m not sure this fix will work with everyone.

I’ve managed to get the code to compile and upload to the ESP8266, I have yet to test if the code works (I will carry on the course and see if what I did was compatiable with the rest of the instructions). Here were the steps I took to solve this.

  1. I deleted all folders in documents/arduino/libraries
  2. I went to the github page for the library AsyncMqttClient (https://github.com/kurtgo/AsyncMqttClient)
  3. Clicked CODE, clicked download zip
  4. In the IDE, tabs at the top, Sketch -> Manage Libraries -> Add zip to library (as normal)
  5. Selected to the downloaded zip
  6. Repeated step 4 and 5 for the two dependencies as per page 152 in the course material
  7. Installed the two dependencies in the library manager (Still couldn’t find AsyncMqttClient)
  8. Restarted the IDE. Updated all libraries.
  9. Added my username/password/IP address as instructed later on in the section.

It compiled, I got the following message that I’ve never seen before in the console.

 

. Variables and constants in RAM (global, static), used 29376 / 80192 bytes (36%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1496 initialized variables
╠══ RODATA 1552 constants
╚══ BSS 26328 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60407 / 65536 bytes (92%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 27639 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 259920 / 1048576 bytes (24%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 259920 code in flash

I don’t know if this is important.

As I say, I will continue the course and see if it works going forward before I declare this as a solution.

0 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
Great.
I’m glad you made it work.
But, the link in the eBook to download the “fixed” library is this one
Async MQTT Client: https://randomnerdtutorials.com/async-mqtt-client-download
 
Regards,
Sara

0 Vote Up Vote Down
Rafael Campos answered 8 months ago

Hey, how you guys been doing?
I’ve made the same path as Liam Woffindin and it seems to work now.
In my point of view to get the thing right:
1. Clean your library folder(if you are a Linux user, the path maybe something like that “home/Arduino/libraries”);
2. Go to the github(https://github.com/kurtgo/AsyncMqttClient) >>  click “<> Code”  >> then .ZIP;
3. Add on Arduino IDE: Sketch > Include LIbrary > Add .Zip library > add your downloaded library;
4. Do this same for the dependencies(links on ebook);
4.1. Install these 2 dependencies;
5. Close Arduino IDE;
6. Run Arduino IDE again and it’ll pop up the message “update all”, click on it(updates the library that you’ve put);
7. Compile the code (“V” sign) and upload it (“->” sign);
8. It will gave something like that:

. Variables and constants in RAM (global, static), used 29408 / 80192 bytes (36%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1496 initialized variables
╠══ RODATA 1576 constants
╚══ BSS 26336 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60407 / 65536 bytes (92%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 27639 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 259920 / 1048576 bytes (24%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 259920 code in flash
esptool.py v3.0
Serial port /dev/ttyUSB0
Connecting….
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: c4:d8:d5:03:8d:18
Uploading stub…
Running stub…
Stub running…
Configuring flash size…
Auto-detected Flash size: 4MB
Compressed 294784 bytes to 213234…
Writing at 0x00000000… (7 %)
Writing at 0x00004000… (14 %)
Writing at 0x00008000… (21 %)
Writing at 0x0000c000… (28 %)
Writing at 0x00010000… (35 %)
Writing at 0x00014000… (42 %)
Writing at 0x00018000… (50 %)
Writing at 0x0001c000… (57 %)
Writing at 0x00020000… (64 %)
Writing at 0x00024000… (71 %)
Writing at 0x00028000… (78 %)
Writing at 0x0002c000… (85 %)
Writing at 0x00030000… (92 %)
Writing at 0x00034000… (100 %)
Wrote 294784 bytes (213234 compressed) at 0x00000000 in 18.8 seconds (effective 125.7 kbit/s)…
Hash of data verified.
Leaving…
Hard resetting via RTS pin…

 
Then go to Serial Monitor, set 115200 baud and its done.
 

1 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
im glad it is working, but I recommend using the links in the ebook so that you know you’re using the same versions as we are.
Regards.
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.