So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By alisdisco
#81161 Hi all, I'm Alistair and very new to this great little board - be even better if I could get it to work how I want it to tho :o)

Any way, I digress. I have an ESP-01 and at the moment I have it setup when GPIO0 goes high, it sends a push notification to my Blynk app, and also on GPIO2 going high it sends another message to my Blynk app. Basically, I wish to monitor when a set of remote contacts open and close. What I would love is if I could have a notification if on one GPIO only goes high and when it goes low another message - is this possible please?

the code I have is this:

Code: Select all#define BLYNK_PRINT Serial# include < ESP8266WiFi.h > #include < BlynkSimpleEsp8266.h >
  BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*****************";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***********";
char pass[] = "***********";
int flag1 = 0;
int flag2 = 0;

void notifyOnButtonPress1() {
  int isButtonPressed1 = digitalRead(0);
  if (isButtonPressed1 == 1 && flag1 == 0) {

    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Dad is up and alarm is now unset");
    flag1 = 1;
  } else if (isButtonPressed1 == 0) {
    flag1 = 0;
  }

}

void notifyOnButtonPress2() {
  int isButtonPressed2 = digitalRead(2);
  if (isButtonPressed2 == 1 && flag2 == 0) {

    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Dad's alarm is part set and has gone to bed");
    flag2 = 1;
  } else if (isButtonPressed2 == 0) {
    flag2 = 0;
  }

}
void setup() {
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // Setup notification button on pin 0
  pinMode(0, INPUT_PULLUP);
  // Setup notification button on pin 2
  pinMode(2, INPUT_PULLUP);
  timer.setInterval(16000 L, notifyOnButtonPress1);
  timer.setInterval(16000 L, notifyOnButtonPress2);

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


any help would be so greatly appreciated.

Alistair
User avatar
By RichardS
#81163 Does this help? Will only printout when the button changes state.

Code: Select allbool buttonIs = digitalRead(2);
bool buttonWas  = buttonIs;

while(1) {
  buttonIs = digitalRead(2);
  if(buttonWas && !buttonIs) {
    Blynk.notify("just went low");
    buttonWas = buttonIs;
  }
  if(!buttonWas && buttonIs) {
    Blynk.notify("just went high");
    buttonWas = buttonIs;
  }
}
User avatar
By alisdisco
#81214 Hi Richard, many thanks for your reply, its just what I've been looking for! I've been trying to find this very thing for over 3 weeks. Its working fine just as I need it :D I love the project section of this forum too - I shall spend many a happy moment looking.

Cheers once again

Alistair
User avatar
By RichardS
#81218 Many years of programming and it makes it look simple :-)

All your doing is keeping the last state of the button and when that state is not the same as the last state with know it has either gone high or low....

Are you familiar with the ! operation, it is the logic NOT, basically any variable that is 0 is "FALSE/LOW" and any other number +100 +1000000 or -1 is a "TRUE/HIGH" so if you !100 its 0 (false) if you !-1 its 0 (false), and !0 is 1 (true), and of course you can use it will Boolean variables of course!

RichardS