ESP-01: Using GPIO3 (RX) as an input

In this project, I have a soft button, toggled via my browser. This soft button works perfectly. When activated it sounds an alarm.

There is a button on the alarm (GPIO3). When pushed it is meant to silence the alarm. It works the first time but then whenever I toggle the soft switch, after this has been pushed once, the alarm switches off immediately; just as if the button has been pressed.

What it looks like is that when the programme starts, input1 is high. When the button is pressed, it goes low and then stays low. How can I pull it up again?

Looking at ESP-01 and ESP-01S How program and use the Pins and Leds (if I am reading it correctly) it does not look like I should need a pull_up resistor on GPIO3.

I am using a custom PCB board so for now I am stuck with using GPIO3.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
#define load1 0 //GO0
#define input1 3 //GO3

bool buttonPushed;


ESP8266WiFiMulti WiFiMulti;
static const char ssid[] = "SSID";
static const char password[] = "PWD";

const char* mqtt_server = "IP";

WiFiClient espClient;
PubSubClient client(espClient);

long lastMsg = 0;
char msg[50];
int value = 0;

int last_pub_time = 0;

bool beeping;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  // Serial.println();
  // Serial.print("Connecting to ");
  // Serial.println(ssid);

  WiFiMulti.addAP(ssid, password);

  while (WiFiMulti.run() != WL_CONNECTED) {
    // Serial.print(".");
    delay(100);
  }

  // Serial.println("");
  // Serial.print("Connected to ");
  // Serial.println(ssid);
  // Serial.print("IP address: ");
  // Serial.println(WiFi.localIP());

  randomSeed(micros());

  // Serial.println("");
  // Serial.println("WiFi connected");
  // Serial.println("IP address: ");
  // Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  // Serial.print("Message arrived [");
  // Serial.print(topic);
  // Serial.print("] ");
  for (int i = 0; i < length; i++) {
    // Serial.print((char)payload[i]);
  }
  // Serial.println();
  if ((char)payload[0] == '3') {
    beeping = true;
  }
  else  {
    beeping = false;
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    // Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "lens_5kchta2dWhdRS2AQz0TeTuhplPE";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), "openhabian", "openhabian")) {
      // Serial.println("connected");
      // Once connected, publish an announcement...
      // client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("/home/esp8266/switch/command");
    } else {
      // Serial.print("failed, rc=");
      // Serial.print(client.state());
      // Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {

  pinMode(load1, OUTPUT);
  digitalWrite(load1, HIGH);  //Turn buzzer off on startup
  pinMode(input1,INPUT);

  //Serial.begin(115200,SERIAL_8N1,SERIAL_TX_ONLY);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  //Sound to confirm successful wifi connection
  digitalWrite(load1, LOW);
  delay(500);
  digitalWrite(load1, HIGH);

  //Rest switch on webportal
  if (!client.connected()) {
    reconnect();
  }
  client.publish("/home/esp8266/switch/state", "2");

}

void soundAlarm() {
  digitalWrite(load1, LOW);
  // Serial.print("Beep on : ");
  delay(1000);
  digitalWrite(load1, HIGH);
  // Serial.println("Beep off");
  delay(1000);

}

void loop() {
  if (beeping) soundAlarm();
  if (!client.connected()) {
    reconnect();
  }
  buttonPushed = !digitalRead(input1);
  if (buttonPushed) {
    if (millis() - last_pub_time > 3000) {
      last_pub_time = millis();
      client.publish("/home/esp8266/switch/state", "2");
      beeping = false;
      // Serial.println("Push Switch");
    }
    delay(500);
  }
  client.loop();
}

Have you tried?

pinMode(input1,INPUT_PULLUP);

Thanks for that. I had tried earlier and got mixed results. I then tested the button and it was faulty so a replacement button together with the PULL_UP and all is well.

Many thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.