BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Virtual button and send data do not work together


#1

I am using esp-01 and uno. I have updated my ESP with ESP8266_NONOS_SDK-master. Everything is great so far. With blynk I can send data with DHT22. and I can view it with iPhon. The problem starts with this point. There is no problem displaying data. but when I want to control the relay or LED with the virtual button, I can not achieve this. no problem just burning virtual buttons and leds. but the two are not together.
Or is it happening?
here is my code…


#include <SimpleTimer.h>
#include <avr/wdt.h>
#include <stdlib.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#define DHTPIN 12
#define DHTTYPE DHT22
#define RAINPIN A3
SimpleTimer timer;
char auth[] = "eXXX4";
char ssid[] = "denger";
char pass[] = "asdasdas";
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

// Select your pin with physical button
const int btnPin = 8;
int pin13 = 13;
int phpin = A5;
float t=0;
float n=0;
//float ph=0;
float h=0;
DHT dht(DHTPIN, DHTTYPE);
WidgetLED led1(V29);
WidgetLED led3(V31);


//BlynkTimer timer;

// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led3.on();
    } else {
      led3.off();
    }
    btnState = isPressed;
  }
}

void data(){
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float n = analogRead(A3) / 9.31;
  float ph = analogRead(phpin);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V21, h);
  Blynk.virtualWrite(V22, n);
  Blynk.virtualWrite(V23, ph);
  Serial.print("Ph=");
  Serial.println(ph);
   Serial.print("ISI=");
  Serial.println(t);
    Serial.print("NEM=");
  Serial.println(h);
    Serial.print("toprak nem=");
  Serial.println(n);
  
}

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

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

  // Setup physical button pin (active low)
  pinMode(btnPin, INPUT_PULLUP);

  timer.setInterval(500L, buttonLedWidget);
  timer.setInterval(500L, data);
  
}
BLYNK_WRITE(V4)
{int pinValue = param.asInt(); // Get the state of the VButton
if (pinValue == 0)
{(digitalWrite(pin13,LOW),led1.off());} else {digitalWrite(pin13,HIGH),led1.on();}
}
void loop()
{
  Blynk.run();
  timer.run();
}

#2

Hello and welcome to the forum… You seem to have missed reading the Welcome Topic… can you please do so, then edit you first post to format the code as required.

Thanks.


#3

thank you :slight_smile:


#4

Is this the function you are having issues with? And what are those issues… doesn’t compile, compiles fine but just no result… partial result… crashes… etc.

I am no good with condensed (shorthand) code, so I have no idea if that is the issue… but I think you need to try breaking it out and testing what does and doesn’t work e.g. the digital pin command & the virtual LED command


#5

Hello
I did some research on the problem yesterday and I organized my coding as follows. It works very well.
I just wonder something.
The virtual button works like this.
BLYNK_WRIT (V2)

{int y2 = param.asInt ();
if (y2> 0) {digitalWrite (role2, HIGH), led2.on ();
else {digitalWrite (role2, LOW), led2.off ();
Serial.print (“Second Button Status:”);
Serial.println (y2);}
“else” (y2) does not work if we do not tell what to do.
so;
BLYNK_WRITE (V2)

{int y2 = param.asInt ();
if (y2> 0) {digitalWrite (role2, HIGH), led2.on ();
Do not you have to work in the form of?

here is code;

#include <SimpleTimer.h>
#include <avr/wdt.h>
#include <stdlib.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <SoftwareSerial.h>
///////////////////////////////////////////////////
#define ESP8266_BAUD 9600
#define BLYNK_PRINT Serial
#define DHTPIN 12
#define DHTTYPE DHT22
#define RAINPIN A3
/////////////////////////////////////////////////
SimpleTimer timer;
/////////////////////////////////////////////////
char auth[] = "e1c6460XXXXXXXXXXXXXXX1eafbfc3404";
char ssid[] = "mushroom";
char pass[] = "revulation";
/////////////////////////////////////////////////
SoftwareSerial EspSerial(2, 3); // RX, TX
ESP8266 wifi(&EspSerial);
WidgetLED led1(V29);
WidgetLED led2(V30);
WidgetLED led3(V31);
DHT dht(DHTPIN, DHTTYPE);
////////////////////////////////////// butonların ayarlanması
const int btnPin = 5;//denemek için bir fiziksel buton eklendi
const int role2 = 6;
const int role3 = 7;
int sayi = 0;
int y = 0;
int phpin = A5;

float t=0;
float n=0;
float h=0;

boolean btnState = false;

/////////////////////////////////////////////////////////
void sanalbuton()
{
  // Read button
  boolean isPressed = (digitalRead(btnPin) == LOW);
  // If state has changed...
  if (isPressed != btnState)
  {if (isPressed) {led3.on();} else {led3.off();}
    btnState = isPressed;}}
////////////////////////////////////////////////////////
void data(){
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  float n = analogRead(A3) / 9.31;
  float ph = analogRead(phpin);// ph kalibrasyonu için bağzı ayarlar gerekli..
  
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V21, h);
  Blynk.virtualWrite(V22, n);
  Blynk.virtualWrite(V23, ph);
 
  sayi=sayi+1;
  Serial.println(sayi);
  Serial.print("Ph: ");
  Serial.println(ph);
  Serial.print("Toprak Nemi: ");
  Serial.println(n);
  Serial.print("Ortam Nemi: ");
  Serial.println(h);
  Serial.print("Ortam Isı: ");
  Serial.print(t);
  Serial.println(" *C ");
}

void setup()
{
  Serial.begin(9600);

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

  pinMode(btnPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(role2, OUTPUT);
  pinMode(role3, OUTPUT);

  timer.setInterval(1000L, sanalbuton);
  timer.setInterval(5000L, data);
  Blynk.syncAll();
}
/////////////////////////////////////////////////////////////
BLYNK_WRITE(V1)

{int y1 = param.asInt();
  if (y1 > 0){digitalWrite(LED_BUILTIN, HIGH),led1.on();}
  else{digitalWrite(LED_BUILTIN, LOW),led1.off();}
  Serial.print("birinci Buton Durumu: ");
  Serial.println(y1);}
///////////////////////////////////////////////////////////
 BLYNK_WRITE(V2)

{int y2 = param.asInt();
if (y2 > 0){digitalWrite(role2, HIGH),led2.on();}
  else{digitalWrite(role2, LOW),led2.off();}
  Serial.print("ikinci Butonun Durumu: ");
  Serial.println(y2);}
  ////////////////////////////////////////////////////////
  BLYNK_WRITE(V3)
  
  {int y3 = param.asInt();
if (y3 > 0){digitalWrite(role3, HIGH),led3.on();}
  else{digitalWrite(role3, LOW),led3.off();}
  Serial.print("Ucuncu Buton Durumu: ");
  Serial.println(y3);}
//////////////////////////////////////////////////////////  
void loop()
{Blynk.run();timer.run();}