Home Automation with light dimmer and fan control

Greetings,

I am currently part of a project where we are using an Arduino Uno and 2 Arduino Nanos. There is 1 nano for controlling the light and the other for controlling the fan. When I say controlling I mean, allowing it to turn on/off and using sliders to change the light intensity/ fan speed. The Blynk is meant to control the Arduino Uno (which acts as a hub), and this hub sends messages to the corresponding nanos. The communication from Arduino Uno to nano, happens through Zigbee, using xbee module. Using arduino uno r3 with wifi shield (esp 13).

My question: Is it possible to send the message from the Blynk app to Arduino Uno, and output this to the Nano all wirelessly. If so, could you please help me with the coding for Arduino Uno to achieve this?
To be clear, I require a value to be outputted to the Nano from the Arduino Uno, through Blynk.

One more question: Are there information as to how to use the button and slider on the Blynk app and control their logic or modify what they are meant to do, in Arduino IDE code. As i am new to Arduino IDE and Blynk, i just to learn the structure of controlling what the button/slider should do.

Sorry for this long question

Thank you

If your Uno can already interface wirelessly with the Nano’s (with Zigbee and an xbee module) then yes it should be possible.

Do you have Ethernet with the Uno, if you don’t then it would need to be hooked up to a PC and use a USB connection for access to the internet. Alternatively use the nightmare connection method of Uno with an ESP but I wouldn’t recommend that.

Thanks for the quick reply. But could u please help me in coding.

Yes, my partner has used a nano (as a hub) to control the nano controlling light and the other controlling fan. But I am not sure how to use Blynk to send the message all the way

I am not sure how to use the button or slider and program them in Arduino IDE, to output a value to the corresponding nano, so that it can run its logic.

Could you please help me codingwise. Thank you again

Sure, can you harvest my olives?

1 Like

Once again, this would be so much easier if you threw away the Arduinos and used ESP8266’s instead. They would connect directly to Wifi and talk directly to Blynk. They have more processing power than the Arduino, but can be programmed via the Arduino IDE software.

Pete.

1 Like

Sorry for my wording. I really need help since I am not sure of the structure for programming the button or slider on Arduino IDE. I just require a base code, to help me in the right path.

Sorry if I said something I shouldnt have, just in the stress of running out of time for our project.

Generally variables declared as global.

int virtualbuttonState;
int sliderValue;

A button set to switch mode on V0:

BLYNK_WRITE(V0){
  virtualbuttonState = param.asInt();  // virtualbuttonState will be 0 or 1 
}

A slider set from 0 to 10 on V1:

BLYNK_WRITE(V1){
  sliderValue = param.asInt();  // sliderValue will be between 0 and 10
}

HTH

1 Like

Thanks for your answer @PeteKnight . But as per our project proposal we hav included these components and in our initial research, everything seemed fine. But there are areas such as Blynk configuration, that we are experiencing difficulties now.

From what you are saying, do I just need 3 ESP8266 and I could complete all the communication of messages. Could the ESP8266’s communicate with each?
For example, a message from Blynk app to turn on the light is sent to the ESP8266 acting as a hub and this is wirelessly communicated to the corresponding ESP8266, responsible for light control.

Or does it go like this: from the Blynk app, it goes straight to the ESP8266 responsible for light control, not affecting the other ESP8266 for fan control.

Thank you for your quick reply. I will work with this, and get back to you.

You’d go for the latter option, but it depends on whether you want some some sort of manual control as well. If you did, then the third ESP8266 could have physical buttons connected to a third ESP device that would send data to Blynk, then back to the appropriate ESP connected to your fan or light.

If you’re investigating ESP hardware then take a look at the Wemos D1 Mini.

Pete.

1 Like
#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

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

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);


int lightbtnState;
int fanbtnState;

int lightsliderValue;
int fansliderValue;

const int lightPin= 3;  // Digital pin, pwm
const int fanPin= 5;


void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pinMode(lightPin, OUTPUT);
  pinMode(fanPin, OUTPUT);

  BLYNK_CONNECTED() {
    Blynk.syncAll(); //restores all the Widget’s values based on the last saved values on the server. All analog and digital pin states will be restored
  }
}
  
}

void loop()
{
  Blynk.run();

BLYNK_WRITE(V0){
  lightbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(lightbtnState == 1){
      digitalWrite(lightPin, HIGH);   // Digital pin 3
      }
     else{
      digitalWrite(lightPin, LOW);
  }
}


BLYNK_WRITE(V1){
  fanbtnState = param.asInt();  // Virtual lightbtnState will be 0 or 1 
    if(fanbtnState == 1){
      digitalWrite(fanPin, HIGH);    // Digital pin 5
      }
     else{
      digitalWrite(fanPin, LOW);
  }
}

BLYNK_WRITE(V2){          // Slider for Light
  lightsliderValue = param.asInt();  // sliderValue will be between 0 and 100, representing percentages
  
}

BLYNK_WRITE(V3){          // Slider for Fan
  fansliderValue = param.asInt();  // sliderValue will be between 0 and 100
}

  
}

I require help with my coding. I would like to know whether i could press a button in the app and assign a ‘1’ value in digital pin 3, in BLYNK_WRITE(V0) function. Since my arduino uno is connected to nano (which controls fan and light), i require assigning a value out digital pin 3, so that the arduino nano could process it. I am not sure if “HIGH” is used for this. For now, the arduino uno and nano are connected wired, but i need to make the connection wireless, through zigbee.

I also need to apply the same for sliders, where i need to use the slider and assign a light intensity/fan speed value (in percentage) value through the digital pin 5, so that nano could process it. But not sure how to code sliders in the arduino ide.
Sorry for the long writeup, Thank you

@Costas @PeteKnight

Wouldn’t you be better using Virtual pins rather than digital pins to control your fan speed and light intensity?

Pete.

1 Like

@PeteKnight Thanks for your reply. But I am not sure what you mean. Could you please guide me?

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/what-is-virtual-pins

You’re already using virtual pins for your On/Off switches. You could do the same for your Fan Speed and Light Intensity sliders.
You could set the Fan Speed slider up with a range of 1-100 and use this as a percentage that you feed to the Nano controlling the fan speed.
If your coding ability isn’t sufficiently well developed to do this then I’m not sure how you’ve managed to get current system working. Maybe you need to do some more research on Blynk coding, such as the link that @Gunner has kindly provided.

Pete.

Sorry to bother you. i was wondering if the below function would work. I would like to press the fan button and want to send a ‘high’ or some value out the tx pin (pin 1), for serial communication between the arduino uno to nano, since I cant seem to get zigbee wireless communication between them working. Note: fanpin = 1

BLYNK_WRITE(V1){
fanbtnState = param.asInt(); // Virtual lightbtnState will be 0 or 1
if(fanbtnState == 1){
digitalWrite(fanPin, HIGH); // Digital pin 1
}
else{
digitalWrite(fanPin, LOW);
}
}

On the receiving nano board, could i use digitalread function to get the value and use it?

I’ve never used serial communication between two Arduino boards so can’t really comment on that part.
You may want to consider denouncing the Blynk switch.

I still think you’re going about this the wrong way. Replace the Nano connected to the fan with a Wemos D1 Mini or similar that’s connected to your Wi-Fi and control it directly from Blynk.

Pete.

@panth3r if you really must do MCU to MCU via serial take a look at this excellent tutorial http://forum.arduino.cc/index.php?topic=396450

1 Like