Blynk for Beginners and help with your project

A simple sketch to control relays with an ESP. Be sure to read ALL the notes. SimpleTimer is included as it is imperative for almost all Blynk sketches. Only a very experienced coder could even attempt a Blynk project using millis(). This sketch was posted in response to the “LED widget” thread at LED and LCD widgets

If anything is not clear please ask for clarification.

/*************************************************************
 LED_Relay.ino for ESP's with Blynk  http://community.blynk.cc/t/led-widget/11582/5
 Mod of PushData.ino from Ethernet to ESP by Costas 10 Feb 2017
 Control a relay and show status with virtual LED, tested on WeMos D1 Mini
 Pinout details for WeMos D1 Mini and Mini Pro https://www.wemos.cc/product/d1-mini-pro.html
 
 For active LOW relays like the onboard LED on a WeMos, 
 Swap lines at rows 41 and 47 for active HIGH relays
 
 V0 - Virtual Button in SWITCH mode
 V1 - Virtual LED
 V2 - Value Display widget demonstrating SimpleTimer, set as PUSH READING FREQUENCY

 ESP8266 slected as Device in app, not WeMos
 *************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define vRelayBtn V0
#define vLED      V1
#define vDisplay  V2 

#define vLEDHigh 255
#define vLEDLow    0
#define relayPin   2   // (D4 on WeMos, builtin LED, active LOW)

char auth[]   = "91xxxxxxxxxxxxxxxx2e22a";
char ssid[]   = "xxxxxxxxxx";
char pass[]   = "xxxxxxxxxx";
char server[] = "blynk-cloud.com";
unsigned int vRelayStatus;

SimpleTimer testingTimer;

BLYNK_WRITE(vRelayBtn) {     // Blynk app WRITES button status to server
  vRelayStatus = param.asInt();
  if(vRelayStatus == 1){
    digitalWrite(relayPin, LOW);        // see note at line 8
    Blynk.virtualWrite(vLED, vLEDHigh);
    Serial.println(F("Relay is now ON"));    
  }
  else{
    digitalWrite(relayPin, HIGH);       // see note at line 8
    Blynk.virtualWrite(vLED, vLEDLow);
    Serial.println(F("Relay is now OFF"));   
  }
}


void myTimerEvent()  // included because SimpleTimer is essential for "ALL" Blynk sketches
{
  Blynk.virtualWrite(vDisplay, millis() / 1000);        // max 10 values per second
  // any other function can also go here, perhaps read a physical button HIGH / LOW status
}

void setup()
{
  pinMode(relayPin, OUTPUT);
  Serial.begin(115200);                           // Debug console
  Blynk.begin(auth, ssid, pass, server);
  testingTimer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
}

void loop()
{
  Blynk.run();
  testingTimer.run(); // Initiates SimpleTimer
}
4 Likes