添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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?

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

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

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);
      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)
    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

UKHeliBob:

tell us whether you have installed the PubSubClient library and, if so, how you did it

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

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