I used a MQTT code to control a led is working fine after few seconds of powering on
But at the starting or restarting of Arduino it blinks for a second
I tried debugging the code but i didn't figured it out please help me to stop blinking the led at starting of the code
It's working fine after starting of few seconds
Here the code
#include <WiFi.h>
#include <PubSubClient.h>
// Define NodeMCU D2 pin connect to Piezo LedPin
#define ledPin 2
// Update these with values suitable for your network.
const char* ssid = "AndtoidAP_5321"; //enter your wifi id
const char* password = "asdfghjkl9"; //enter your wi-fi passowrd
const char* mqtt_server = "192.168.55.107";
const char* Off = " Turning Off PC! ";
const char* On = " Turning On PC! ";
const char* Restart = " Force Restarting PC wait 10sec! ";
const char* Restarted = " Force Restarting Completed! ";
//const char* mqtt_server = "iot.eclipse.org";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command from MQTT broker is : [");
Serial.print(topic);
int p =(char)payload[0]-'0';
// if MQTT comes a 0 turn off LED on D2
if(p==0)
{
digitalWrite(ledPin, HIGH);
Serial.println(On);
client.publish("out.log",Onn);
}
// if MQTT comes a 1, turn on ledPin on pin D2
if(p==1)
{
digitalWrite(ledPin, LOW);
Serial.println(Off);
client.publish("out.log",Off);
}
Serial.println();
// if MQTT comes a 3, turn on and wait 10 seconds and off ledPin on pin D2
if(p==3)
{
digitalWrite(ledPin, HIGH);
Serial.println(Restart);
client.publish("out.log",Restart);
delay(10000);
digitalWrite(ledPin, LOW);
client.publish("out.log",Restarted);
}
Serial.println();
} //end callback
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
// clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.publish("outTopic","hello world");
//resubscribe
client.subscribe("EMS");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.setCallback(callback);
client.loop();
}