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

Blynk, Touch Sensor and Relay

Hi - first of all, sorry Gunner and Pete for the mistake in my first post with the code :slight_smile:

the Sketch works fine now but I´m working on a new project and need help as an absolute beginner.

First of all - what I´m gonna doing?

Controlling 2 wall sockets with a 2-way relay, nodeMCU, Blynk App and WS2812 led as indicator.
For now I can control the two relays and the leds via the blynk app. So if I push the button 1 - relay 1 is on and the led turns from red to green. Same with relay 2.

But now I want to add a simple touch sensor (TTP223) to controll relay1 / led 1 and relay2 / led 2 and update the state to blynk app.

If read the example of sync physical buttons with blynk, but as absolute Noob all looks like chinese chars :slight_smile:
If tried to implement codes from this example but nothing works - could everyone help please?

#define BLYNK_PRINT Serial
#define PIN 13
#define relay1 14
#define relay2 12

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

char auth[] = "xxxx";
char ssid[] = "xxx";
char pass[] = "xxx";
char server[] ="xxx";

void setup()
{

  strip.begin();
  strip.show();
  strip.setBrightness(64);
  
  Serial.begin(9600);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);

  Blynk.begin(auth, ssid, pass, server);
}


BLYNK_WRITE(V6)
  {
int relayOne = param.asInt();

if(relayOne == 1) 
{
  digitalWrite(relay1, LOW);
  strip.setPixelColor(0, 0, 127, 0);
  strip.show();
}
else 
{
  digitalWrite(relay1, HIGH);
  strip.setPixelColor(0, 127, 0, 0);
  strip.show();
}
Serial.print("Steckdose 1 ist: ");
Serial.println(relayOne);
  }

BLYNK_WRITE(V5)
  {
int relayTwo = param.asInt();

if(relayTwo == 1) 
{
  digitalWrite(relay2, LOW);
  strip.setPixelColor(11, 0, 127, 0);
  strip.show();
}
else 
{
  digitalWrite(relay2, HIGH);
  strip.setPixelColor(11, 127, 0, 0);
  strip.show();
}
Serial.print("Steckdose 2 ist: ");
Serial.println(relayTwo);
  }
void loop()
{
  Blynk.run();
}

it is like my sketch that controls a bulb with 433 Mhz wall switch .

you have to define the touchPin and use timer , but I think you can use interrupt too

//setup
timer.setInterval(1000, TTP223); //push 1 sec

//end setup

void TTP223() {
if (digitalRead(touchPin) == HIGH)
  {
   //do something
  }
 }

I hope your mains wiring skills are better than your coding skills, or this may be the last we hear of you :zap::zap::zap:

Pete.

1 Like

:smile:

Hey Pete … I´m realy good in wiring… only got a few shocks but I´m still here :slight_smile:

Thx Alexis - this make sense, but won´t change the state of the Virtual Pins an Blynk.

if (digitalRead(touchPin) == HIGH)
  {
   change color of the led, switch relay an change state of blynk (e.g V6)
  }
 }

so if I´m right, I have to check which state all the components are.

if led is green and relay1 is on and blynk app button (V6) is on - change led to red and relay1 off and blynk app button (V6) off

1 Like

You should really go back to the sketch builder example classes Sync physical switch, as this has all the elements you need as the basis for your project.
Take time to understand what it does, and use it with a physical switch, then go from there.

Pete.

2 Likes

went back to the sketch … but don´t understand what the sketch exactly doning, becaus - Noob.
but thx anyway - got no time at the moment to learn programming from begin on and need this for my new appartment, so I have to take a look if someone can do this for me.

THX Pete and Alex

It’s a pity you didn’t say “I have no time to learn programming - I’m looking for someone to write my code for me” at the very start, so that we would have known not to waste our time on you.

Pete.

1 Like

Sorry Pete, my fault, but what time have you waste for telling me that you hope that my wirings skills better than my programming skills and tell me to look at an example that I´ve done befor.

That was not usefull at all.

If said above that I´m a noob, and the code you see above I worked hard by my self, but now I´m on a point I didn´t came any further and need help and all I get from you was … scorching criticism.

I’ve taken the time to read your posts, both in this thread and your other one, and taken time to examine your code in some detail.
I’ve also considered what might be the best way to explain to you how flags can be used to track the status of your relay; and concluded that going back to the Ready made and well documented Blynk example would be the best starting point for you - and said so in my comments.

And, if you don’t think that a word of warning about the dangers of mains electricity is useful, then please feel free to disregard my comments.

Good luck with finding someone to write your code for you.

Pete.

Thx Pete for your honesty but …
The post with the electricity don´t have anything with the problem to do ( I know about high voltage dangers) but ok… its legitim.

your second post don´t help too because I said a my first post that I´ve read the example and I don´t understand it.
So … tell someone to take a look at an example that the person already have done… hmmmm.

What about the time … I don´t said, that I don´t wanna leran it - I´ve said that I don´t have time at the moment becaus we are moving to a new home, and I´ve plenty of things to do they are more importent. But I want to add my porject to our new home from start on. Finding someone isn´t the problem (got some IT specialists), but I want to learn programing arduino by my own and understanding what I´m doing.

No… I am the one who gives that :stuck_out_tongue_winking_eye: @PeteKnight is much more diplomatic :smiley:

Our Community Policy clearly requests that new users put in some time reading and researching… we try to help you learn, not hold your hand or do it for you. We are all just volunteers here… with our own lives, projects, priorities, etc. Learning something new takes time and effort as you are well aware… but that goes the same for us helping others… our time and effort!

Ever heard of the “Fast, Good, Cheap… pick only two” saying? If your priority is to get it done, then perhaps a for pay programming site like fivver.

Not to mention we don’t always have the same parts as you… so how could we even test it ourselves? You may run into same issue with pay for sites… but at least they have $$ motivation to try and do it for you.

1 Like

@GunnerTechTools … I think your post is much more diplomatic … but anyway.

as I said befor :slight_smile: - I know that I have to learn programming from scratch on, and this is what I wanna do insted of people doing things for me :wink: - but to tell someone "take a look at the same example that you´ve looked before (and don´t understand) realy don´t help.

but … I´ve sitting down the whole night and tried an do something and now it works … I don´t know how, but it do :slight_smile:

the only thing I have to do is to implement the WS2812b strip for the LED to turn red or green if the relays are on or off.

here ist the code without the leds and two relais with two touch sensors.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

const int relayOnePin = 12; //connected to nodeMCU Pin D6 and relay IN1
const int relayTwoPin = 14; //connected to nodeMCU Pin D5 and relay IN2
const int btnOnePin = 13;   //connected to nodeMCU Pin D7 Touch Sensor 1 IN
const int btnTwoPin = 15;   //connected to nodeMCU Pin D8 Touch Sensor 2 IN

// Your Wifi Credentials and Auth Tokens
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
char server[] ="xxx";

BlynkTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relayOneState = LOW;
int btnOneState = HIGH;

int relayTwoState = LOW;
int btnTwoState = HIGH;

BLYNK_CONNECTED() 
{
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  
}

// Button 1
BLYNK_WRITE(V0) {
  relayOneState = param.asInt();
  digitalWrite(relayOnePin, relayOneState);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnOnePin) == LOW) {
    if (btnOneState != LOW) {

      // Toggle Relay state
      relayOneState = !relayOneState;
      digitalWrite(relayOnePin, relayOneState);

      // Update Button Widget
      Blynk.virtualWrite(V0, relayOneState);
    }
    btnOneState = LOW;
  } else {
    btnOneState = HIGH;
  }
}

// Button 2
BLYNK_WRITE(V1) {
  relayTwoState = param.asInt();
  digitalWrite(relayTwoPin, relayTwoState);
}

void checkPhysicalButton2()
{
  if (digitalRead(btnTwoPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnTwoState != LOW) {

      // Toggle Relay 2 state
      relayTwoState = !relayTwoState;
      digitalWrite(relayTwoPin, relayTwoState);

      // Update Button Widget
      Blynk.virtualWrite(V1, relayTwoState);
    }
    btnTwoState = LOW;
  } else {
    btnTwoState = HIGH;
  }
}

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

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


  pinMode(relayOnePin, OUTPUT);
  pinMode(btnOnePin, INPUT_PULLUP);
  digitalWrite(relayOnePin, relayOneState);
  
  pinMode(relayTwoPin, OUTPUT);
  pinMode(btnTwoPin, INPUT_PULLUP);
  digitalWrite(relayTwoPin, relayTwoState);


  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);

}

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

Well, it seems that you did go back to that example and, understand it or not, you incorporated large chunks of it into your code.

Code from the Blynk ‘Sync Physical Button’ example…

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;
  }
}

Your code…

Incidentally, you’ll find that you won’t be able to turn your power sockets on and off, even with the physical buttons, if your internet is down.
There’s a fairly simple solution, but I won’t bother pointing you in that direction, as you probably won’t understand it and wouldn’t want to be accused of being unhelpful again :rofl: :rofl: :rofl:

I’m sure one of the more sympathetic forum members will point you in the right direction if you ask nicely.

Pete.

Dear Pete,
to cover my curiosity, is the bl…run unconditionally or the short timer same time timers?

Not sure I understand the question @mikekgr

The Blynk.begin is blocking, so Blynk.config needs to be used instead if the OP wants control without internet access available.
I guess that if he’s using a local server (could be as he’s specifying the server separately, but seems unlikely with his level of expertise) then provided his Wi-Fi and local server are up then he’ll get full control even without internet access. Coding it better, using config instead of begin and some connection testing, will give a more robust system that will work even if his Wi-Fi and local server died.

Pete.

yes, I understood!
Furthermore I stated again the 100mS two start timers

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);

as well as blynk.run() that is running unconditionally ( without if )

if (Blynk.connected()) { // to ensure that Blynk.run() function is only called if we are still connected to the server conditional run though
    Blynk.run();
     }

have a nice rest of the day!

Oh yes, I hadn’t spotted that the timers weren’t staggered.

Pete.

I use a redundant server with local blynk server on it.
So this project isn´t connected to internet. Also if the nodeMCU or the relay is broken I can bypass with a mechanical switch.

But what do you mean with the chunk @PeteKnight Both codes are identical - I use two touch sensor and two relays … so I have to check both of them or could a put both “checkPhysicalButton” in one void?

Search for “staggered timers” and look through the results until you find one that you’re able to understand.

BTW, tagging the user called @Pete isn’t the same as tagging me with @PeteKnight

Pete.