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

User avatar
By alisdisco
#85560 Hi, I hope someone would be able to show me the way. I have an ESP-01 with two inputs. Basically, I want to monitor a intruder alarm for set and unset. The connection to the esp is with a spdt relay contact. The esp would look at the state of this input and then forward to Blynk on my phone to the status of the alarm. I have this code working but its not consistent. It has two inputs GPIO0 and GPIO2 to give me the status. As I said, I only want one GPIO to do this but its just not reliable. Could anyone help me before I pull out the last remaining hair that I have!

#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[] = "Auth Token : ***********************************";
// 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's alarm is now set and he has gone to bed");
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 now unset");
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(16000L, notifyOnButtonPress1);
timer.setInterval(16000L, notifyOnButtonPress2);

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

Alistair
User avatar
By StanJ
#85662 The problem with the ESP-01 module is that both GPIO0 and GPIO2 are used to put the chip in different boot modes, so you can't drive them as LOW inputs until after boot is cleared (about 120ms after reset) or you'll have it in some strange mode that's not running your code. Both pins need to be HIGH during reset or else it goes off into LaLaLand.

If you're usingGPIO0 and GPIO2 as outputs, no problems.

Optionally, if you don't need the serial port during normal execution of your widget, the two serial pins are free for any use you need. One of the two TxD pins must be free, but the one that's not brought out to the 8-pin header works for that initial debug message, so you should be able to drive the TxD pin if you wish.