Controlling LED ESP01

I am trying to control LED via mqtt

I am following this link https://www.emqx.com/en/blog/esp8266-connects-to-the-public-mqtt-broker

Here is my code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// GPIO 5 D1
#define LED 0

// WiFi
const char *ssid = "vi"; // Enter your WiFi name
const char *password = "12345";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "192.168.43.23";
const char *topic = "esp8266/led";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

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()) {
        
        if (client.connect(" ")) {
            Serial.println("Public emqx mqtt broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }
    // publish and subscribe
    client.publish(topic, "hello emqx");
    client.subscribe(topic);
}

void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message:");
    String message;
    for (int i = 0; i < length; i++) {
        message = message + (char) payload[i];  // convert *byte to string
    }
    Serial.print(message);
    if (message == "on") { digitalWrite(LED, LOW); }   // LED on
    if (message == "off") { digitalWrite(LED, HIGH); } // LED off
    Serial.println();
    Serial.println("-----------------------");
}

void loop() {
    client.loop();
}

I have tested broker connection successfully. I tested to subscribe message that I was publishing from ESP01

I don't know why My led doesn't turn on or off when I publish message from broker (PC)

Here's some dumb questions:

Do you get the other indications that you are receiving messages, or whatever you call them that you are Serial printing?

Does the LED go on and off with the simplest sketch you can find or write that is designed to just turn the LED on and off?

a7

At first make sure that you're not using a bad LED. Then check all the points mentioned in the reply above mine.

Yes LED is working fine with simple sketch I see the problem I am not getting print data on terminal

Step 1:

Connections before to upload sketch

(Find the code in attachments)

ESP8266:-------------- >Arduino:

GND -------------------------- GND

GPIO-2 -------------------------- Not connected (open)

GPIO-0 -------------------------- GND

RXD -------------------------- RX

TXD -------------------------- TX

CHPD ------------------------ 3.3V

RST -------------------------- Not connected (open)

VCC -------------------------- 3.3V

Step 2:

Connections after uploading sketch

ESP8266:-------------- >Arduino:

GND -------------------------- GND

GPIO-2 -------------------------- Not connected (open)

GPIO-0 -------------------------- Not connected (open)

RXD -------------------------- TX

TXD -------------------------- RX

CHPD ------------------------ 3.3V

RST -------------------------- Not connected (open)

VCC -------------------------- 3.3V

You could move this along faster by realizing we can't see what you see…

Do you get this printing?

  Serial.println("Public emqx mqtt broker connected");

How about this?

  Serial.print("Message arrived in topic: ");

Have you run a complete test of the hardware following an example from any source of knowledge, an example you did not tinker with?

Did that work? Please post the code of a working example you did not modify.

a7

Actually I am not getting printed anything on terminal

This is sketch I have uploaded for experiment. I was expecting it should be print

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid         = "vi";
const char* password     = "12345";
const char* mqttBroker   = "192.168.43.23";
const int   mqttPort     = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

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
}
void loop() {
    client.loop();
}

Did you set the baud to 115200 in the Serial output window?

Have you used the serial monitor window before?

You say

 not getting printed anything on terminal

meaning you don't see this message?

      Serial.println("Connecting to WiFi..");

Try

 Serial.println("Good Morning Sunshine!");
 Serial.flush();

 while (1);  // just hangs.

right after you Serial.begin().

a7

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