Home automation using nodemcu + relay



#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d89adaa1a4f742b58ced23d605008047";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SmartAnSolver";
char pass[] = "admin123";



  const int Relay_1 = 16;       //D0
  const int Relay_2 = 5;        //D1
  const int Relay_3 = 4;        //D2
  const int Relay_4 = 0;        //D3
  const int Relay_5 = 2;        //D4
  const int Relay_6 = 14;       //D5
  const int Relay_7 = 12;       //D6
  const int Relay_8 = 13;       //D7
  const int Relay_9 = 15;       //D8





void setup()
{
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  pinMode(Relay_7, OUTPUT);
  pinMode(Relay_8, OUTPUT);
  pinMode(Relay_9, OUTPUT);

    digitalWrite(Relay_1, HIGH);
    digitalWrite(Relay_2, HIGH);
    digitalWrite(Relay_3, HIGH);
    digitalWrite(Relay_4, HIGH);
    digitalWrite(Relay_5, HIGH);
    digitalWrite(Relay_6, HIGH);
    digitalWrite(Relay_7, HIGH);
    digitalWrite(Relay_8, HIGH);

  
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);





}

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










/********************** Relay 1  ON/OFF V1 *******************************/
BLYNK_WRITE(V1)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_1, HIGH);
    Serial.println("Relay 1 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_1, LOW);
    Serial.println("Relay 1 Off");
    }
  }



/********************** Relay 2  ON/OFF V2 *******************************/
BLYNK_WRITE(V2)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_2, HIGH);
    Serial.println("Relay 2 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_2, LOW);
    Serial.println("Relay 2 Off");
    }
  }



/********************** Relay 3  ON/OFF V3 *******************************/
BLYNK_WRITE(V3)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_3, HIGH);
    Serial.println("Relay 3 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_3, LOW);
    Serial.println("Relay 3 Off");
    }
  }



/********************** Relay 4  ON/OFF V4 *******************************/
BLYNK_WRITE(V4)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_4, HIGH);
    Serial.println("Relay 4 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_4, LOW);
    Serial.println("Relay 4 Off");
    }
  }



/********************** Relay 5  ON/OFF V5 *******************************/
BLYNK_WRITE(V5)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_5, HIGH);
    Serial.println("Relay 5 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_5, LOW);
    Serial.println("Relay 5 Off");
    }
  }



/********************** Relay 6  ON/OFF V6 *******************************/
BLYNK_WRITE(V6)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_6, HIGH);
    Serial.println("Relay 6 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_6, LOW);
    Serial.println("Relay 6 Off");
    }
  }



/********************** Relay 7  ON/OFF V7 *******************************/
BLYNK_WRITE(V7)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_7, HIGH);
    Serial.println("Relay 7 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_7, LOW);
    Serial.println("Relay 7 Off");
    }
  }



/********************** Relay 8  ON/OFF V8 *******************************/
BLYNK_WRITE(V8)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_8, HIGH);
    Serial.println("Relay 8 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_8, LOW);
    Serial.println("Relay 8 Off");
    }
  }








2 Likes

yes because at start, nodemcu pin are LOW
and your relay board need HIGH to have relay off

so , at start I always put all relays pins HIGH.

ok sorry

ok thanku

bundle of thanks <3

can we make a web portal to use on desktop just like blynk app ?

@Blynk_Coeur

a little correction
not all Relays need High to be OFF
I got modules with high level trigger ( active high )

as an example: 4 relay module high/low level trigger
https://s3-ap-southeast-1.amazonaws.com/a2.datacaciques.com/wm/307176851/3849027817/2766353203.jpg

2 Likes

It depends what you’re doing with your relays, and whether you mind some of them being activated momentarily on startup.

I’d certainly avoid using GPIOs 0, 1, 15 & 16

This excellent topic is well worth a read:

Pete.

1 Like

Have any solution about the security or privacy forr this blynky app. ? Can we make diffirent privacy for diffirnet devices using in same project in blynk app?

Your App login is your key … there is no further segregation of devices. Although you could probably program various security measures that are required by the App user before they can control certain items… that is all up to your ingenuity and how well you can code though (button tap codes, terminal or Input widget PIN codes, “hidden” buttons in distant tabs, etc.)

1 Like

This was just an example

1 Like

help me please… when i upload sketch in my nodemu, it works well but when cut off power supply, it not work, mean code not upload permanently, just as flash

I don’t see how that’s possible… if you upload, and the code works at least once, then it is properly flashed into the MCU memory. Perhaps you need to ask on the esspressif site.

2 Likes

ok, i try to ask on their site. thanks

since flash memory is non-volatile, that make no sense :wink:

but i worked on other projects with other codes on same nodemcu… it works well

What’s probably happening is that you’ve uploaded code, or connected hardware, to your NodeMCU that’s causing it to go into a reset loop.

If you look at the serial monitor with the correct baud rate selected (probably 74880) then you’ll see the boot messages from the device. Chances are that you’ll see WDT reset messages and if you google these (as opposed to posting them here) you’ll get a clue about what is happening.

Pete.

1 Like

use serial monitor to see what happened.
I’m pretty sure it isn’t flash memory lost issue.
also try to push the reset button twice on nodemcu.
I was having the same issue, due to DHCP server.
now I fix it with static IP instead of dynamic .

1 Like

A post was split to a new topic: When the nodemcu units starts all the 4 relay on their self,