Blynk, Touch Sensor and Relay

: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

@Gunner … 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.

sorry PeteKnight :slight_smile: and also sorry vor insulting you :wink:

so you mean

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(130L, checkPhysicalButton2);

that the update not run at the same time?

one question if I may ask anymore - Led 0 and Led 11 on the neopixel strip lights green - if I push the touchsensor it turns to red, but if I release it turns back to green, but… if relay on = led green / if relay off = led red

Any tip for me?

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

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

      // Update Button Widget
      Blynk.virtualWrite(V1, relayTwoState);
    }
    btnTwoState = LOW;
    strip.setPixelColor(11, 0, 127, 0);
    strip.show();
  } else {
    btnTwoState = HIGH;
    strip.setPixelColor(11, 127, 0, 0);
    strip.show();
  }
}

Ok found it out by myself :slight_smile:

And here is the complete working code

I check the state of the of the “relayOneState” - this gives a number of 0 or 1, and if the “relayOneState == 1” than turn Pixel 1 to green - if not turn to red. This have to go inside of the “checkPhysikalButton”

maybe someone got a cleaner and smother idea for my sketch

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.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

const int neoPin = 5; // WS2812b connectet to nodeMCU Pin D1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, neoPin, NEO_GRB + NEO_KHZ800); //Set the Neopixel Strip to 12 Pixels on pin 5

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

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) {
        relayOneState = !relayOneState;
        digitalWrite(relayOnePin, relayOneState);
        Blynk.virtualWrite(V0, relayOneState);
      }
       
    btnOneState = LOW;
    } 
  
    else {
      btnOneState = HIGH;
    }
    
    //Led 1 Color Change
    if (relayOneState == 1) {
      strip.setPixelColor(0, 64, 224, 208);
      strip.show();
    }
    
    else {
      strip.setPixelColor(0, 127, 0, 0);
      strip.show();
    }
}

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

void checkPhysicalButton2()
{
  if (digitalRead(btnTwoPin) == LOW) {
    
    if (btnTwoState != LOW) {
      relayTwoState = !relayTwoState;
      digitalWrite(relayTwoPin, relayTwoState);
      Blynk.virtualWrite(V1, relayTwoState);
    }
    
    btnTwoState = LOW;   
  }
  
    else {
      btnTwoState = HIGH;
    }

    //LED 2 Color Change
    if (relayTwoState == 1) {
      strip.setPixelColor(11, 64, 224, 208);
      strip.show();
    }
    
    else {
      strip.setPixelColor(11, 127, 0, 0);
      strip.show();
    }
}

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

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

  strip.begin();
  strip.show();
  strip.setBrightness(64);


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

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(130L, checkPhysicalButton2);

}


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