Help turning off LED on disconnect

I am currently using a Wemos D1 Mini which has been working well. It stays connected and syncs on connect perfectly. The problem is that for safety reasons I need to shut off certain pins if there is ever a loss in connection. The code below should perform this on pin 2 easily but it does not work and I am stumped. The serial monitor accurately displays connection status but the pin never goes low. Does anyone know what is going on?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial

bool Connected2Blynk = false;

char auth[] = "xxxxxx";

char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";

BlynkTimer timer;

const int ledPin = 2;
int ledState = LOW;


BLYNK_CONNECTED() {
  Blynk.syncVirtual(V2);
}


BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}


void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
  timer.setInterval(2000L, CheckConnection);
}

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
     Serial.println("Not connected to Blynk server");
     digitalWrite(2, 0); //Why does this not work? Tried ledPin, LOW. LED stays on.
  }
  else{
     Serial.println("Connected to Blynk server");    
  }
}

void loop()
{
   Blynk.run();  
   timer.run();
}

Oh the humiliation. The 0 should be a 1. Hah. Sorry about that folks. Moderators please delete. :roll_eyes:

1 Like