Button does not work with sensors

Hi! I need a little help with my project…
I have 3 sensors as gauges and a button which turns on a digital output with a relay. The point is that when I upload the code only the 3 sensor work properly but the button don’t. When I upload the code without the sensors the button works. I hope you can help me! thank you all.


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN  16 //D3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define pino_sinal_analogico A0   //SENSOR SOLO
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
char auth[] = "xxxxxxxxxxxxxxxxxxxxx";
/* WiFi credentials */
char ssid[] = "xxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxx";
/* TIMER */
#include <SimpleTimer.h>
SimpleTimer timer;
/* DS18B20 Temperature Sensor */
#include <OneWire.h>
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 5 // D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
#define rele_1 4

void sensor_temp_agua()
{
  float temp;
  DS18B20.requestTemperatures(); 
  temp = DS18B20.getTempCByIndex(0);
  Blynk.virtualWrite(10, temp); //virtual pin V10
  Serial.print("TEMP AGUA: ");
  Serial.print(temp);
  Serial.print(" ºC");
  Serial.print(" - ");
}

void sensor_agua()
{
   int valor_analogico;
   int linhaagua;
   valor_analogico = analogRead(pino_sinal_analogico);
   linhaagua = (100 - ((valor_analogico / 9.2) - 100));
   Blynk.virtualWrite(11, linhaagua); //virtual pin V11
   Serial.print("NIVEL AGUA: ");
   Serial.print(linhaagua);
   Serial.print(" %");
   Serial.print(" - ");
}

void sensor_dht_t()
{
   float t = dht.readTemperature();
   Serial.print("TEMP AMBIENTE: ");
   Serial.print(t);
   Serial.print(" ºC");
   Serial.print(" - ");
   Blynk.virtualWrite(12, t); //virtual pin V12
   
}

void sensor_dht_h()
{
   float h = dht.readHumidity();
   Serial.print("UMID AMBIENTE: ");
   Serial.print(h);
   Serial.print(" %");
   Serial.println(" - ");
   Blynk.virtualWrite(13, h); //virtual pin V13  
   
}

void setup() {
 Serial.begin(115200);
 pinMode(pino_sinal_analogico, INPUT);
 Blynk.begin(auth, ssid, pass);
 pinMode(rele_1, OUTPUT);
 digitalWrite(rele_1, HIGH);
 DS18B20.begin();
 dht.begin();
 timer.setInterval(1000L, sensor_agua); 
 timer.setInterval(1000L, sensor_temp_agua); 
 timer.setInterval(1000L, sensor_dht_t); 
 timer.setInterval(1000L, sensor_dht_h);  
  
}

void loop() {
 Blynk.run();
 timer.run(); // Initiates SimpleTimer
  }

Please!

Please read :wink:

First, properly format your posted code…

Then keep reading documents…
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-control-anything-with-blynk-app

Then work the problem… we will help as best able if you have specific questions, but we are NOT a code factory or code mechanic shop.

Four separate timers … but each running at the exact same time doesn’t help.

Also 1 second is NOT enough time for DHT sensors… they need more like 3-5 seconds between reads.

Thanks for the hints!

I don’t see anything related to button in your code. Just defined relay pin and one single digital write. Where’s the button code?

Hello, My issue is that I’m using a button widget controlling the gp4 output. But now I tried the button as a virtual with some code still without success…

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN  16  //D3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define pino_sinal_analogico A0   //SENSOR SOLO
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
char auth[] = "";
/* WiFi credentials */
char ssid[] = "";
char pass[] = "";
/* TIMER */
#include <SimpleTimer.h>
SimpleTimer timer;
/* DS18B20 Temperature Sensor */
#include <OneWire.h>
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 5 // D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
#define rele_1  4 //D4


void sensor_temp_agua()
{
  float temp;
  DS18B20.requestTemperatures(); 
  temp = DS18B20.getTempCByIndex(0);
  Blynk.virtualWrite(10, temp); //virtual pin V10
  Serial.print("TEMP AGUA: ");
  Serial.print(temp);
  Serial.print(" ºC");
  Serial.print(" - ");
  if(temp > 32)
    {
      Blynk.notify("TEMPERATURA DO AQUARIO ACIMA DE 27ºC");
    }
}

void sensor_agua()
{
   int valor_analogico;
   int linhaagua;
   valor_analogico = analogRead(pino_sinal_analogico);
   linhaagua = (100 - ((valor_analogico / 9.2) - 100));
   Blynk.virtualWrite(11, linhaagua); //virtual pin V11
   Serial.print("NIVEL AGUA: ");
   Serial.print(linhaagua);
   Serial.print(" %");
   Serial.print(" - ");
   if(linhaagua < 80)
    {
      Blynk.notify("REPOSIÇÃO DE ÁGUA");
    }
}

void sensor_dht_t()
{
   float t = dht.readTemperature();
   Blynk.virtualWrite(12, t); //virtual pin V12
   Serial.print("TEMP AMBIENTE: ");
   Serial.print(t);
   Serial.print(" ºC");
   Serial.print(" - ");
    
}

void sensor_dht_h()
{
   float h = dht.readHumidity();
   Blynk.virtualWrite(13, h); //virtual pin V13
   Serial.print("UMID AMBIENTE: ");
   Serial.print(h);
   Serial.print(" %");
   Serial.println(" - ");
   Serial.println("IOT.BRS");
     
}

BLYNK_WRITE(V0) // Run this function when V1 button pressed.
{
     int pinValue = param.asInt();  // Get status of V1.
    if (pinValue == 1) {  // If status of V1 is 1 then do stuff in if().
      digitalWrite(4, LOW); // Turn on LED.
        }
}

void setup() {
 Serial.begin(115200);
 pinMode(rele_1, OUTPUT);
 digitalWrite(rele_1, HIGH);
 pinMode(pino_sinal_analogico, INPUT);
 Blynk.begin(auth, ssid, pass);
 DS18B20.begin();
 dht.begin();
 delay(1000);
 timer.setInterval(1000L, sensor_agua); 
 timer.setInterval(1100L, sensor_temp_agua); 
 timer.setInterval(1200L, sensor_dht_t); 
 timer.setInterval(1300L, sensor_dht_h);
    
  
}

void loop() {
 Blynk.run();
 timer.run(); // Initiates SimpleTimer

     }

GPIO4 is not on D4.

here a screenshot

It is! When I upload the code without the timers the button works…

What MCU are you using?

I’m using a wemos D1 board

https://hackspark.fr/pub/media/catalog/product/cache/image/700x700/9b53d7ee6c576e27421bdbeafdf2e7a2/e/s/esp8266_board_arduino_uno_headers_large.jpg

the right pinout table is allright

Side note… use backtick keys for code formatting, not commas or apostrophes :wink:

This function will run whenever the associated Button Widget is pressed… and is completely independent of whether you use timers or not.

However this function, as you have it written, only does one thing… turns that pin LOW… What do you use to turn it HIGH again?

So are you using D1R1 or D1R2 board?

It’s D1R1

Ok, can you pls post your code properly and with defined virtual pin for the button?

I can also see that you’re expecting too much from your sensors, so your Wemos is blocked all the time. Readings should be at 5000-10000L ranges.

I will complete the but while I’m testing only write this part of the code and it doesn’t set the gp4 to LOW level.

This delay(1000) is NO with Blynk. All setIntervals are too high. And you should divide timers as well, you have four of them.

Thanks, I will increase time…
here the code:

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN  16  //D3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define pino_sinal_analogico A0   //SENSOR SOLO
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
char auth[] = "";
/* WiFi credentials */
char ssid[] = "";
char pass[] = "";
/* TIMER */
#include <SimpleTimer.h>
SimpleTimer timer;
/* DS18B20 Temperature Sensor */
#include <OneWire.h>
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 5 // D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
#define rele_1  4 //D4


void sensor_temp_agua()
{
  float temp;
  DS18B20.requestTemperatures(); 
  temp = DS18B20.getTempCByIndex(0);
  Blynk.virtualWrite(10, temp); //virtual pin V10
  Serial.print("TEMP AGUA: ");
  Serial.print(temp);
  Serial.print(" ºC");
  Serial.print(" - ");
  if(temp > 32)
    {
      Blynk.notify("TEMPERATURA DO AQUARIO ACIMA DE 27ºC");
    }
}

void sensor_agua()
{
   int valor_analogico;
   int linhaagua;
   valor_analogico = analogRead(pino_sinal_analogico);
   linhaagua = (100 - ((valor_analogico / 9.2) - 100));
   Blynk.virtualWrite(11, linhaagua); //virtual pin V11
   Serial.print("NIVEL AGUA: ");
   Serial.print(linhaagua);
   Serial.print(" %");
   Serial.print(" - ");
   if(linhaagua < 80)
    {
      Blynk.notify("REPOSIÇÃO DE ÁGUA");
    }
}

void sensor_dht_t()
{
   float t = dht.readTemperature();
   Blynk.virtualWrite(12, t); //virtual pin V12
   Serial.print("TEMP AMBIENTE: ");
   Serial.print(t);
   Serial.print(" ºC");
   Serial.print(" - ");
    
}

void sensor_dht_h()
{
   float h = dht.readHumidity();
   Blynk.virtualWrite(13, h); //virtual pin V13
   Serial.print("UMID AMBIENTE: ");
   Serial.print(h);
   Serial.print(" %");
   Serial.println(" - ");
   Serial.println("IOT.BRS");
     
}

BLYNK_WRITE(V0) // Run this function when V1 button pressed.
{
     int pinValue = param.asInt();  // Get status of V1.
    if (pinValue == 1) {  // If status of V1 is 1 then do stuff in if().
      digitalWrite(4, LOW); // Turn on LED.
        }
}

void setup() {
 Serial.begin(115200);
 pinMode(rele_1, OUTPUT);
 digitalWrite(rele_1, HIGH);
 pinMode(pino_sinal_analogico, INPUT);
 Blynk.begin(auth, ssid, pass);
 DS18B20.begin();
 dht.begin();
 delay(1000);
 timer.setInterval(1000L, sensor_agua); 
 timer.setInterval(1100L, sensor_temp_agua); 
 timer.setInterval(1200L, sensor_dht_t); 
 timer.setInterval(1300L, sensor_dht_h);
    
  
}

void loop() {
 Blynk.run();
 timer.run(); // Initiates SimpleTimer

     }

Increase reading times for sensors and DELETE delay(1000), or change it to delay(50) if you think it’s important to have it there.

And that’s definitely not properly formated code :slight_smile:

Actually i don’t really need the delay that was using just for obtain any success… I’ll try

void setup() {
 Serial.begin(115200);
 pinMode(rele_1, OUTPUT);
 digitalWrite(rele_1, HIGH);
 pinMode(pino_sinal_analogico, INPUT);
 Blynk.begin(auth, ssid, pass);
 DS18B20.begin();
 dht.begin();
 timer.setInterval(4000L, sensor_agua); 
 timer.setInterval(5000L, sensor_temp_agua); 
 timer.setInterval(6000L, sensor_dht_t); 
 timer.setInterval(7000L, sensor_dht_h);