Compilation error: PubSubCilent.h: No such file or directory

Hi Good Afternnon guys,
i dk why i already put mosquitto broker in services done open the cmd but still cant compile in arduino... My project is doing ESP32 Wroom DE Module
Got idea to resolve my problem?

Welcome to the forum

Have you installed the PubSubClient library ?

Please post your sketch using code tags when you do

Well Before i post the sketch may i know my ESP32 Wroom got bluelight is ady connected to the wifi or not?

If your sketch does not compile then no, the ESP32 will not be connected to WiFi unless you previously uploaded a sketch that does connect

Stop messing about and post your MQTT sketch and tell us whether you have installed the PubSubClient library and, if so, how you did it

I post my MQTT Sketch install like this and test sensor data worked

There is not much point in posting a link to a video when the problem is with the code on your project, particularly the video has nothing to do with PubSubClient on the ESP32

One more time

Post your sketch here

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "ssid";
const char* password = "password";
const char* brokerUser = "MQTT";
const char* brokerPassword = "Password";
const char* broker = "test.mosquitto.org";
const char* outTopic = "topic/count";
const char* outTopic2 = "topic/test";
const char* outTopic3 = "topic/sample";
const char* outTopic4 = "topic/example";
const char* inTopic = "topic/sample";
int inPinRun = 5;
int inPinAlarm = 6;
int inPinOutput = 7;
int inPin4Rej = 8;

WiFiClient espClient;
PubSubClient client(espClient);
long currentTime, lastTime;
int count = 0;
int test = 1;
int sample = 2;
int counter = 3;
char messages[20];

// WIFI CONNECT
void setupWifi(){
delay(100);
Serial.println("Conneting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while(WiFi.status() != WL_CONNECTED){
delay(200);
Serial.print("-");
}

Serial.print("Connected to ");
Serial.println(ssid);
}

// MQTT CONNECT
void reconnect(){
while(!client.connected()){
Serial.print("\nConnected to ");
Serial.println(broker);
if(client.connect("MHMQTT",brokerUser, brokerPassword)){
Serial.print("\nConnected to ");
Serial.println(broker);
client.subscribe(inTopic);
} else {
Serial.println("Try to connnect again");
delay(4500);
}
}
}

// DATA SUBSCRIBE
void callback(char* topic, byte* payload, unsigned int length){
Serial.print("Received messages; ");
Serial.println(topic) ;
for(int i=0; i<length; i++){
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW);
Serial.println("True");
client.publish(outTopic2,"True");
// Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP)
} else {
digitalWrite(BUILTIN_LED, HIGH);
Serial.println("False");
client.publish(outTopic2,"False");
// Turn the LED off by making the voltage HIGH
}
}

void setup(){
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point");

//configure wifi and get IP address
setupWifi();
IPAddress myIP = WiFi.begin();
Serial.print("AP IP adress: ");
Serial.print(myIP);
client.setServer(broker,****);
client.setCallback(callback);

}

// OUTPUT
void loop(){
if(!client.connected()){
reconnect();
}

client.loop();

currentTime = millis();
if(currentTime - lastTime > 2000){
sample = random(2);
snprintf(messages, 80, "%ld", sample);
Serial.print("Sending messages: ");
Serial.println(messages);
client.publish(outTopic3,messages);
lastTime = millis();
delay(1000);
}
}
This is my sketch in arduino ide

And here is your sketch Auto formatted in the IDE to improve its layout and posted here in code tags as I asked you to do but you ignored

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "ssid";
const char* password = "password";
const char* brokerUser = "MQTT";
const char* brokerPassword = "Password";
const char* broker = "test.mosquitto.org";
const char* outTopic = "topic/count";
const char* outTopic2 = "topic/test";
const char* outTopic3 = "topic/sample";
const char* outTopic4 = "topic/example";
const char* inTopic = "topic/sample";
int inPinRun = 5;
int inPinAlarm = 6;
int inPinOutput = 7;
int inPin4Rej = 8;

WiFiClient espClient;
PubSubClient client(espClient);
long currentTime, lastTime;
int count = 0;
int test = 1;
int sample = 2;
int counter = 3;
char messages[20];

// WIFI CONNECT
void setupWifi()
{
  delay(100);
  Serial.println("Conneting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(200);
    Serial.print("-");
  }
  Serial.print("Connected to ");
  Serial.println(ssid);
}

// MQTT CONNECT
void reconnect()
{
  while (!client.connected())
  {
    Serial.print("\nConnected to ");
    Serial.println(broker);
    if (client.connect("MHMQTT", brokerUser, brokerPassword))
    {
      Serial.print("\nConnected to ");
      Serial.println(broker);
      client.subscribe(inTopic);
    }
    else
    {
      Serial.println("Try to connnect again");
      delay(4500);
    }
  }
}

// DATA SUBSCRIBE
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Received messages; ");
  Serial.println(topic) ;
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '1')
  {
    digitalWrite(BUILTIN_LED, LOW);
    Serial.println("True");
    client.publish(outTopic2, "True");
    // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP)
  }
  else
  {
    digitalWrite(BUILTIN_LED, HIGH);
    Serial.println("False");
    client.publish(outTopic2, "False");
    // Turn the LED off by making the voltage HIGH
  }
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point");
  //configure wifi and get IP address
  setupWifi();
  IPAddress myIP = WiFi.begin();
  Serial.print("AP IP adress: ");
  Serial.print(myIP);
  client.setServer(broker, ****);
  client.setCallback(callback);
}

// OUTPUT
void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
  currentTime = millis();
  if (currentTime - lastTime > 2000)
  {
    sample = random(2);
    snprintf(messages, 80, "%ld", sample);
    Serial.print("Sending messages: ");
    Serial.println(messages);
    client.publish(outTopic3, messages);
    lastTime = millis();
    delay(1000);
  }
}

Please use code tags in future when posting code here

You still have not answered my question

I think im not install that PubSubClient Library so where should i go to install this one

You can install it using the Library Manager in the IDE

Where did you get the sketch from ?

My Company Boss. Btw im so sry about this is my first time to use arduino forum. so dk how to code tags

so which one i need to install
is the cloud4rpi-esp or the ESPMQTT one

Neither

Install this one

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

the problem is resolve
Thanks you so much brother and
apologize that i am new to forum

I am glad that it is working

Good luck with your project

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.