I have connected an LED to my NodeMCU in the following way: Circuit Diagram and have successfully controlled it using the serial port of my laptop. Now I want to control it remotely through Wifi and Blynk App on my phone. I have added all the required specifications like board type and pin number etc. in the app. I have been able to send the LDR data successfully to ThingSpeak site, which means the connection of NodeMCU to the wifi is fine. But when I turn ON the light using button on Blynk App, the LED lights up for less than a second and then turns off. The serial monitor on Arduino IDE keeps showing the light is off. Here is the code that I uploaded on NodeMCU:
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
const char* ssid = "WifiName";
const char* password = "WifiPassword";
char auth[] = "AuthCode from Blynk App";
const int ldrPin = A0;
int ldrVal = 0;
int inputVal = 0;
const int ledPin = 5; //D1 pin on NodeMCU
WiFiClient client;
long myChannelNumber = ThingSpeak channel number;
const char myWriteApiKey[] = "Thingspeak write api key";
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, password);
delay(100);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, 0);
pinMode(ldrPin, INPUT);
Serial.println();
Serial.println();
Serial.print("Connecting To: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
delay(200);
Serial.println("...");
}
Serial.println("");
Serial.println("Wifi Connected");
Serial.print("Ip Address: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
while(Serial.available()>0){
inputVal = Serial.read();
}
ldrVal = analogRead(ldrPin);
Serial.println(ldrVal);
if(inputVal=='1'){
digitalWrite(ledPin, HIGH);
Serial.println("ON");
}
else{
digitalWrite(ledPin, LOW);
Serial.println("OFF");
}
Serial.println("");
ThingSpeak.writeField(myChannelNumber, 1, ldrVal, myWriteApiKey);
ThingSpeak.writeField(myChannelNumber, 2, inputVal, myWriteApiKey);
}
I have added Blynk Functionality to the already running code of my project where I take input from serial port and turn the LED on or off based on that input. Why is the LED not turning on properly?