Slider Param for A0 Not Working

Hi. This is my first post. Apologies in advance if this has been discussed before.
below is the code I am working on. I am able to control only 1 relay connected to humidity data from DHT11. My challenge is I am not able to trigger second relay connected to temperature data. Blynk app led reflects changes basis virtual write but the second relay is not changing state. Relays are fine. Its a 5v 4 relay module that I am using.

Below is the code…


#define USE_NODE_MCU_BOARD
#define APP_DEBUG
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include "BlynkProvisioning.h"
#include <Arduino.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial
SimpleTimer timer;
#define DHTPIN D1
#define DHTTYPE DHT11
const int relay1 = D5;
const int relay2 = D6;
const int relay3 = D7;
const int relay4 = D8;

int humLowTrigger;
int TempLowTrigger;

DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);
BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

BLYNK_WRITE(V8) {
  updateTemp(param.asInt());
}

void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);

 
  if (h <= humLowTrigger) 
{   digitalWrite(relay1, LOW);   
    Blynk.virtualWrite(V26, 0);
 
    } else {
  if (h >= humLowTrigger)
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);

  }

if (t <= TempLowTrigger) 
{   digitalWrite(relay2, LOW); 
    Blynk.virtualWrite(V27, 0);
 
    } else {
  if (t >= TempLowTrigger)
    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V27, 255);

  }



  
}
void updateHum(int param) {
  humLowTrigger = param;  
}


void updateTemp(int param) {
  TempLowTrigger = param;  
}

void setup() {
  delay(500);
  Serial.begin(115200);

  BlynkProvisioning.begin();
timer.setInterval(2000, Readdata);
  dht.begin();
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);

 pinMode(relay2, OUTPUT);
  digitalWrite(relay2, LOW);

 pinMode(relay3, OUTPUT);
  digitalWrite(relay3, LOW);

   pinMode(relay4, OUTPUT);
  digitalWrite(relay4, LOW);

}

void loop() {
  // This handles the network and cloud connection
  BlynkProvisioning.run();
timer.run();

}

2 Likes

I’ve had a quick look through the code and nothing jumps out at me as being a problem, although I’m on an iPad, so it’s more difficult than pasting it into the Arduino IDE.

I’m not a great fan of the “const int relay1 = D5” way if referencing the GPIO pins and if by some chance you’ve selected the wrong board type in Arduino then these won’t work correctly. I think I’d start by putting a meter on each of the GPIO pins and see what’s happening when you trigger each of the relays in the code, just to make sure that it’s not a different pin that’s going high/low.

You also mention that you’re using 5v relays. The ESP8266 uses 3.3v logic levels, and although most 5v relay boards that have opto isolators onboard can operate on 3.3v logic, you still need to be careful how you power them. It’s possible that you’ve killed one of the GPIO outputs on your ESP by connecting it up wrongly at some point.

Pete.

1 Like

Thanks Knight for the quick reply. I have checked the code many times but not able to identify the issue.
Board is nodeMCU correctly selected with all settings in the IDE.
Powering the board using 5v via USB connected to 5v2A charger
4 Relay Module is also powered directly from the same charger.
NodeMCU’s logic pins (D5, D6, D7, D8) are connected to Relay Module

I think connections and board selection is right as the Blynk app shows humidity and temperature readings correctly. And humidity Relay is triggering with the slider value connected at V7

Also, I need some help with electronics (maybe something related to Pull up resistors) -

  1. NodeMCU fails to boot if the Relay module is on. I first power the nodeMCU and then manually power the relay board by connecting the 5v pin. Else NodeMCU won boot.
  2. Humidity Pin D5 triggers the relay but the LED on relay module is slightly on instead of completely off.
  3. On Pin state LOW Relay Module is ON.

Relay Module here - https://images-na.ssl-images-amazon.com/images/I/51R7ty4sW%2BL.jpg
NodeMCU is LOLIN type

Thanks for your reply.
PS: which meter are you mentioning, multimeter or virtual meter on the pins in Blynk app?

Multimeter - you need to check that the pins are going to 0v and +3.3v when the code tells the NodeMCU to switch them low and high.

Pulling GPIO15 (D8) High at boot-up will put the NodeMCU into “SD Card Boot Mode”. Having Relay 4 connected to D8 may be what’s causing this.

Pete.

Will check with the multimeter and update. Issue is I don’t have a working multimeter at the moment :frowning:
I have commented the relay4 from D8. Still the same issue :confused:

Rather than commenting-out the D8 line in the code, you should pull the wire off of pin D8 on the board.

Maybe time for a bit of Christmas shopping. Just remember to wrap it up after you’ve used it and pop it under the tree :wink:

Pete.

Have pulled the wire also at that time. Just did not mention. No luck!
What is the reason that all the 5v relay is on when the PIN is LOW? I think it needs to be connected with resistor.

Instead of multimeter, I just tried it with led. Its not getting on.

I am not able to identify what is causing this issue. Really frustrated at this moment… been 3 days :frowning:

Hahaha. yes the shopping list is increasing every day :christmas_tree:

Shubham.

To fix the boot issue. try pulling each wire between the MCU and the relay board in turn until you find which one is the issue.
Also try switching the other relays, is it just relay 2 that’s not behaving? try connecting relay 2 to a different pin, does that work?

You have an “Active Low” relay board, its the way it’s designed. Some boards have the ability to switch between active low and active high by means of a jumper or solder pads that need to be bridged/cut, but its not really an issue as you can write the code to work with the active low switching.

Its just a case of getting the priorities right - your goodies first, followed by SWMBO then the kids/dogs/cats/in-laws etc. :wink:

Pete.

Knight. Thanks a lot for your support. Yes your idea worked.
I changed the pins to D1,D2,D3,D4 and things are now working fine. Strange.

Also, After removing D8 completely from circuit, Node is booting properly. Thanks for the info. I was not aware.

This was my first post on any forum like this. Always been on the consuming side… hahahaaa… Thanks. Everything is working fine now. I’ll update this post with revised code.

Here is the updated code -

#define USE_NODE_MCU_BOARD       
#define APP_DEBUG 

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include "BlynkProvisioning.h"
#include <Arduino.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial
SimpleTimer timer;
#define DHTPIN D5 
#define DHTTYPE DHT11
const int relay1 = D1;
const int relay2 = D2;
const int relay3 = D3;
const int relay4 = D4;

int humLowTrigger;
int TempLowTrigger;
int SoilLowTrigger;



DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);
BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

BLYNK_WRITE(V8) {
  updateTemp(param.asInt());
}

void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);

 
  if (h <= humLowTrigger) 
{   digitalWrite(relay1, LOW); 
    Blynk.virtualWrite(V26, 0);
 
    } else {
  if (h >= humLowTrigger)
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);

  }

if (t <= TempLowTrigger) 
{   digitalWrite(relay2, LOW); 
    Blynk.virtualWrite(V27, 0);
 
    } else {
  if (t >= TempLowTrigger)
    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V27, 255);

  }



  
}
void updateHum(int param) {
  humLowTrigger = param;  
}


void updateTemp(int param) {
  TempLowTrigger = param;  
}

void setup() {
  delay(500);
  Serial.begin(115200);

  BlynkProvisioning.begin();
timer.setInterval(2000, Readdata);
  dht.begin();
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);

 pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);

  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);

   pinMode(relay4, OUTPUT);
  digitalWrite(relay4, LOW);

}

void loop() {

  BlynkProvisioning.run();
timer.run();

}

Need small help… In the below code, everything is working fine but the virtual LED in Blynk app connected at V29 is not changing state. Relay connected to Blynk timer is working fine but its V29 LED is not reflecting the changes. Below is the code

#define USE_NODE_MCU_BOARD   

#define APP_DEBUG   

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include "BlynkProvisioning.h"
#include <Arduino.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial
SimpleTimer timer;
#define DHTPIN D5 
#define DHTTYPE DHT11
const int relay1 = D1;
const int relay2 = D2;
const int relay3 = D3;
int relay4 = D4;

int humLowTrigger;
int TempLowTrigger;
int SoilLowTrigger;
int Soil = A0;

DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);
void updateSoil (int param);

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

BLYNK_WRITE(V8) {
  updateTemp(param.asInt());
}

BLYNK_WRITE(V9) {
updateSoil(param.asInt());
}


void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V30, Soil);

 
  if (h <= humLowTrigger) 
{   digitalWrite(relay1, LOW); 
    Blynk.virtualWrite(V26, 0);
 
    } else {
  if (h >= humLowTrigger)
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);

  }

if (t <= TempLowTrigger) 
{   digitalWrite(relay2, LOW); 
    Blynk.virtualWrite(V27, 0);
 
    } else {
  if (t >= TempLowTrigger)
    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V27, 255);

  }

  if (Soil <= SoilLowTrigger) 
  {   digitalWrite(relay3, LOW); 
  Blynk.virtualWrite(V28, 0);
 
 } else {
  if (Soil >= SoilLowTrigger)
  digitalWrite(relay3, HIGH);
  Blynk.virtualWrite(V28, 255);
  }

  if 
  (relay4 = LOW) 
  {   
  Blynk.virtualWrite(V29, 0);
  } 
 else 
 {
  if (relay4 = HIGH)
  
  Blynk.virtualWrite(V29, 255);
  }


 }


void updateHum(int param) {
  humLowTrigger = param;  
}


void updateTemp(int param) {
  TempLowTrigger = param;  
}


void updateSoil(int param) {
  SoilLowTrigger = param;  
}

void setup() {
  delay(500);
  Serial.begin(115200);

BlynkProvisioning.begin();
timer.setInterval(2000, Readdata);
  dht.begin();
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);

 pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);

  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);

   pinMode(relay4, OUTPUT);
  digitalWrite(relay4, HIGH);

}

void loop() {
  BlynkProvisioning.run();
timer.run();

}

Now you will have other problems. Maybe it would be useful for you to read NodeMCU docs first. Use D2, D5, D6 and D7 pins as relay connections.

To be honest, I’m probably not the best person to answer this, as I never write Blynk specific code in Arduino IDE. All of my interactions with Blynk are done in Node-Red.
Having said that, I cant see anything wrong with the V29 code, so I’d start bgy checking the setup of the LED in the the app and maybe delete and re-create that LED and re-link it to V29.

I do have a few comments about the code though…
Your IF statements that control the relays and the LED widgets are a bit odd. To begin with, you have overlapping conditional tests - i.e both tests contain an equals symbol):
if (Soil <= SoilLowTrigger)
if (Soil >= SoilLowTrigger)

Because of the way you’ve structured these, with the second IF being part of an ELSE then you get away with the fact that there are two opposing things that could be done when Soil is exactly equal to SoilLowTrigger. For the sake of your future sanity when you’re debugging this at a later date, I’d suggest that you do one thing if the value is less than or equal to, and another if the value is greater than. But, if you read on, you’ll see that in this instance you don’t need this anyway…

The IF immediately following the ELSE is redundant. The code would be much better like this:

  if (Soil <= SoilLowTrigger) 
  {
    digitalWrite(relay3, LOW); 
    Blynk.virtualWrite(V28, 0);
  }
  else
  {
    digitalWrite(relay3, HIGH);
    Blynk.virtualWrite(V28, 255);
  }

Hope this makes sense?

Pete.

Thanks Zodiac. I’ll reassign the pins.
Please can you help what is the issue if I use D1, D2, D3, D4 pins?

Thanks Knight. I used the mentioned If Else statement but for some reason it was not working. Will try again and repost the code here if it works fine. Thanks for the update.
Also, Earlier I was using Node red logic on RPI to control the Relay box. Was using camera functionality also. But I was not able to resolve the following issues -

  1. How to use it on a low cost device like NodeMCU. RPI with Arduino for Analogue pin read was expensive.
  2. I was not able to push the NodeMCU data to internet for the web dashboard or app. I have no clue how to set up Port forwarding and a far better option is to have the API functionality. Which again I have no idea how to implement.

I will post the JSON code of the NodeRed for you to help me.

Thanks once again for all the help. Really appreciate your time.

Thanks for the shared recommendations. I have updated the code. Now my relays are assigned to D1, D2, D4, D5 & DHT on D0
Everything is working fine now. There is one last issue. Looking forward to a solution.
Issue - Relay 3 connected to D5 i.e. SoilLowTrigger is getting triggered with just the slider movement is not comparing data with the Soilmoisture connected at A0.
Relay3 is getting triggered if the slider value is not 0. anything above 0 simplify triggers the relay.
I want trigger to set the param which is compared with soil reading and thn take action.
Kindly help me solve this last issue.

Below is the updated code:

#define USE_NODE_MCU_BOARD    
#define APP_DEBUG      
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include "BlynkProvisioning.h"
#include <Arduino.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial
SimpleTimer timer;
#define DHTPIN D0  
#define DHTTYPE DHT11
const int relay1 = D1;
const int relay2 = D2;
const int relay3 = D5;
int relay4 = D4;

int humLowTrigger;
int TempLowTrigger;
int SoilLowTrigger;
int Soil = A0;

DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);
void updateSoil (int param);

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

BLYNK_WRITE(V8) {
  updateTemp(param.asInt());
}

BLYNK_WRITE(V9) {
updateSoil(param.asInt());
}


void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V30, Soil);

 
  if (h <= humLowTrigger) 
{   digitalWrite(relay1, LOW); 
    Blynk.virtualWrite(V26, 0);
 
    } else {
  if (h >= humLowTrigger)
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);

  }

if (t <= TempLowTrigger) 
{   digitalWrite(relay2, LOW); 
    Blynk.virtualWrite(V27, 0);
 
    } else {
  if (t >= TempLowTrigger)
    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V27, 255);

  }

  if (Soil <= SoilLowTrigger) 
  {   digitalWrite(relay3, LOW); 
  Blynk.virtualWrite(V28, 0);
 
 } else {
  if (Soil >= SoilLowTrigger)
  digitalWrite(relay3, HIGH);
  Blynk.virtualWrite(V28, 255);
  }

  if 
  (relay4 = LOW) 
  {   
  Blynk.virtualWrite(V29, 0);
  } 
 else 
 {
  if (relay4 = HIGH)
  Blynk.virtualWrite(V29, 255);
  }
 }
void updateHum(int param) {
  humLowTrigger = param;  
}
void updateTemp(int param) {
  TempLowTrigger = param;  
}


void updateSoil(int param) {
  SoilLowTrigger = param;  
}

void setup() {
  delay(500);
  Serial.begin(115200);

  BlynkProvisioning.begin();
timer.setInterval(2000, Readdata);
  dht.begin();
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);

 pinMode(relay2, OUTPUT);
  digitalWrite(relay2, LOW);

  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, LOW);

 pinMode(relay4, OUTPUT);
 digitalWrite(relay4, LOW);

}

void loop() {
  BlynkProvisioning.run();
timer.run();

}
1 Like

If you’re going to use Node-Red on RPI then you should install the Blynk WS nodes.
You should then probably run Mosquitto on the RPI and use MQTT as a way of controlling your devices, relays etc.

This is how I use Blynk and it works very well. I don’t run any Blynk code on my ESP devices, just Wi-Fi and MQTT code then logic which controls what the device should do when it receives a particular MQTT message.
I’ve built quite a complex system with 6 different sensors, 7 devices that are being controlled and a Nextion touch screen that works alongside Blynk and Amazon Alexa. Using Node-Red and MQTT makes this a fairly straightforward process that easy to maintain and expand.

Pete.

1 Like