Touch And Blynk

Hey, Whats ups!

I have Error With my TTP223B.

i Making Similar To Sonoff With Touch Module.

This is my code:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Wire.h>
#include <TimeLib.h>
#include <WidgetRTC.h>


char auth[] = "zOCf_MY7kOyNJQmdCV4YFL_9sGxAoPrR";

char ssid[] = "Jarvis";
char pass[] = "3181cq61";

BlynkTimer timer;


const int LuzPrincipal = D8;
const int TeclaLuzPrincipal = D6;

int EstadoLuzPrincipal = LOW;
int EstadoTeclaPrincipal = LOW;
int EstadoLuzVelador1 = LOW;
int EstadoLuzVelador2 = LOW;
int EstadoTeclaVelador1 = LOW;
int EstadoTeclaVelador2 = LOW;



int isFirstConnect = true;

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

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

  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 14), 8080);

  pinMode(LuzPrincipal, OUTPUT);

  pinMode(TeclaLuzPrincipal, INPUT);


  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)



  timer.setInterval(1000L, checkconnect);
  timer.setInterval(100L, checkPhysicalButton);

}

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

void checkPhysicalButton()
{
  if (digitalRead(TeclaLuzPrincipal) == HIGH) {
    Serial.println("Apretado Boton");
    if (EstadoTeclaPrincipal != LOW) {

      // Toggle LED state
      EstadoLuzPrincipal = !EstadoLuzPrincipal;
      digitalWrite(LuzPrincipal, EstadoLuzPrincipal);

      // Update Button Widget
      Blynk.virtualWrite(V30, EstadoTeclaPrincipal);
    }
    EstadoTeclaPrincipal = HIGH;
  } else {
    EstadoTeclaPrincipal = LOW;
  }
}

BLYNK_CONNECTED() {

  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }

  Blynk.syncVirtual(V30);
  Blynk.syncVirtual(V31);
  Blynk.syncVirtual(V32);
}

BLYNK_WRITE(V30) {
  EstadoLuzPrincipal = param.asInt();
  digitalWrite(LuzPrincipal, EstadoLuzPrincipal);
  Serial.println("Apretado V30");
}
BLYNK_WRITE(V31) {
  EstadoLuzVelador1 = param.asInt();
  digitalWrite(LuzPrincipal, EstadoLuzVelador1);
}
BLYNK_WRITE(V32) {
  EstadoLuzVelador2 = param.asInt();
  digitalWrite(LuzPrincipal, EstadoLuzVelador2);

}

void checkconnect()
{
  bool result = Blynk.connected();
  Serial.println("Conectado");
  if (result == false) {
    Blynk.connect();

  }
}

i Make Video With Error.

If u see , Relay never stay in ON OR OFF when 1 touch 1 time.

Try setting your button on the app to switch instead of push.

if you don’t it will send a 1 when you press and a 0 when you release.

But what you are saying is related to software side !! He is referring to the hardware side. I feel this is nothing to do with the app setting.

It’s certainly a software issue. All the hardware does is go HIGH when it’s being touched, and LOW when it’s not.

I’m not convinced by @daveblynk’s idea, because a Blynk.virtualWrite should’t trigger a BLYNK_WRITE on the same virtual pin, but it’s certainly worth a try.

Personally, I’d stick lots more serial prints in there and try to work out what’s happening with each of the variables at each stage in the process. Trying to step through the code on my iPad isn’t working for my slightly frazzled brain at the moment :crazy_face:

Pete.

Check out my method of doing a similar thing with actual Sonoff… Note how I have a flag routine to prevent sequential action when holding the button down… or reacting to both button press and release.

1 Like

When Coping the Examples from the Sketch Builder, try to make sure that you copy them correctly. While changing the condition for the digital read is a valid change (depending on how the button is wired), I am not sure why you would also change the conditions for the flag.

From Sketch Builder:

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

From Your Code:

void checkPhysicalButton()
{
  if (digitalRead(TeclaLuzPrincipal) == HIGH) {
    Serial.println("Apretado Boton");
    if (EstadoTeclaPrincipal != LOW) {

      // Toggle LED state
      EstadoLuzPrincipal = !EstadoLuzPrincipal;
      digitalWrite(LuzPrincipal, EstadoLuzPrincipal);

      // Update Button Widget
      Blynk.virtualWrite(V30, EstadoTeclaPrincipal);
    }
    EstadoTeclaPrincipal = HIGH; //Change this back to EstadoTeclaPrincipal = LOW;
  } else {
    EstadoTeclaPrincipal = LOW;  //Change this back to EstadoTeclaPrincipal = HIGH;
  }
}
1 Like