Two ESP8266 not acive at the same time!

Hi! i am working on ESP8266 for my project. I am using two esps each of them with loaded with identical codes with the only difference being in the keywords they detect & start vibrating. The problem is, even though i use different instances of the IDE & programs are uploaded fine, only the ESP which was recently uploaded detects the keyword & vibrates on it, other just does nothing. when i upload program again on the other esp, the first one becomes silent & does nothing while the other one responds. The keypoint is not the code being wrongly uploadec but the esp not being active at the same time(or so it seems).

Please help! :confused:

here's the code

#include <Bounce2.h> // Used for "debouncing" the pushbutton
#include <ESP8266WiFi.h> // Enables the ESP8266 to connect to the local network (via WiFi)
#include <PubSubClient.h> // Allows us to connect to, and publish to the MQTT broker

const int ledPin = 12; // This code uses the built-in led for visual feedback that the button has been pressed
const int buttonPin = 13; // Connect your button to pin #13
const int lPin = 0;
// WiFi
// Make sure to update this for your own WiFi network!
const char* ssid = "QMobile i6 Metal ONE";
const char* wifi_password = "25c736b880b8";

// MQTT
// Make sure to update this for your own MQTT Broker!
const char* mqtt_server = "192.168.43.72";
const char* mqtt_topic = "Flash_Message";
const char* mqtt_topic_sub = "flash";
const char* mqtt_username = "pi";
const char* mqtt_password = "pi123";
// The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg).
const char* clientID = "ESP01";

// Initialise the Pushbutton Bouncer object
Bounce bouncer = Bounce();

// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker

void ReceivedMessage(char* topic, byte* payload, unsigned int length) {
// Output the first character of the message to serial (debug)
Serial.println((char)payload[0]);

// Handle the message we received
// Here, we are only looking at the first character of the received message (payload[0])
// If it is 0, turn the led off.
// If it is 1, turn the led on.
if ((char)payload[0] == 't' && (char)payload[1] == 'e' && (char)payload[2] == 'n') {
Serial.println((char)payload[2]);
digitalWrite(ledPin, HIGH);
delay(4000);// Notice for the HUZZAH Pin 0, HIGH is OFF and LOW is ON. Normally it is the other way around.
digitalWrite(ledPin, LOW);
}
if ((char)payload[0] == '0') {
digitalWrite(ledPin, LOW);
delay(1000);
}
}

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);

// Switch the on-board LED off to start with
digitalWrite(ledPin, LOW);

// Setup pushbutton Bouncer object
bouncer.attach(buttonPin);
bouncer.interval(5);

// Begin Serial on 115200
// Remember to choose the correct Baudrate on the Serial monitor!
// This is just for debugging purposes
Serial.begin(115200);

Serial.print("Connecting to ");
Serial.println(ssid);

// Connect to the WiFi
WiFi.begin(ssid, wifi_password);

// Wait until the connection has been confirmed before continuing
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

// Debugging - Output the IP Address of the ESP8266
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
client.setCallback(ReceivedMessage);
// Connect to MQTT Broker
// client.connect returns a boolean value to let us know if the connection was successful.
// If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
if (client.connect(clientID, mqtt_username, mqtt_password)) {
client.subscribe(mqtt_topic_sub);
Serial.println("Connected to MQTT Broker!");
}
else {
Serial.println("Connection to MQTT Broker failed...");
}

}

bool Connect() {
// Connect to MQTT Server and subscribe to the topic
if (client.connect(clientID, mqtt_username, mqtt_password)) {
client.subscribe(mqtt_topic_sub);
return true;
}
else {
return false;
}
}

void loop() {
// Update button state
// This needs to be called so that the Bouncer object can check if the button has been pressed

if (!client.connected()) {
Connect();
bouncer.update();
if (bouncer.rose()) {
// Turn light on when button is pressed down
// (i.e. if the state of the button rose from 0 to 1 (not pressed to pressed))
digitalWrite(lPin, LOW);

// PUBLISH to the MQTT Broker (topic = mqtt_topic, defined at the beginning)
// Here, "Button pressed!" is the Payload, but this could be changed to a sensor reading, for example.
if (client.publish(mqtt_topic,"Ehmed is calling you!")) {
Serial.println("Button pushed and message sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(mqtt_topic,"Ehmed is calling you!");
}
}
else if (bouncer.fell()) {
// Turn light off when button is released
// i.e. if state goes from high (1) to low (0) (pressed to not pressed)
digitalWrite(lPin, HIGH);
}
client.loop();
}
}

I think you need to provide much more specific information if you want help.

What is the code?
What is the wiring?
Are they wired up together in any way?
If using Wifi do you have unique MAC and IP addresses.

I have edited the post for code.
wiring is that the coin vibrator is connected on D6(GPIO12) of esp8266 each.
no the esp are not wired up together, one is connected on COM5 & other is connected on COM4.
yes they have unique IPs

The mosquitto MQTT broker disconnects MQTT clients with duplicate names.

const char* clientID = "ESP01";

 if (client.connect(clientID, mqtt_username, mqtt_password)) {

Change the code so it uses clientIDs with random parts or append the WiFi MAC address which is globally unique.

gdsports:
The mosquitto MQTT broker disconnects MQTT clients with duplicate names.

const char* clientID = "ESP01";

if (client.connect(clientID, mqtt_username, mqtt_password)) {




Change the code so it uses clientIDs with random parts or append the WiFi MAC address which is globally unique.

Please guide me how can I change Wifi MAC address? Also I burned esp with clientID "ESP01" & the other with "ESP02", but still it didn't work. Should I make both of the IDs completely random like "one" & "two"?

The requirement is the MQTT client ID be unique. This does not mean changing the WiFi MAC address. Each WiFi MAC address is already supposed to be globally unique so appending the MAC to the client ID makes the client ID unique.

Changing the clientID manually should also work. You may have to turn on verbose debug on the MQTT broker/server and/or capture packets with wireshark to find out what is happening.

You can create unique client IDs like this but I do not have time to test this code so beware.

String mqttClientID;
mqttClientID = "ESP-" + WiFi.macAddress();
mqttClient.connect(mqttClientID.c_str(),...);