Problem: Cannot send data with pushbutton via esp8266 to MQTT

Hey guys! Im working for a little project and I have a problem.

I use:

Arduino Uno
ESP8266
Generic pushbutton
MQTT broker

I can't send data("1" or "0") with my pushbutton to the MQTT broker.
My wifi connection is fine and I can publish messages to the broker but I can't use the ESP8266 to send data with the pushbutton.

Pushbutton connection: see attachment jpg

ESP8266 connection:

GND -------------------------- GND

GP2 -------------------------- Not connected (open)

GP0 -------------------------- GND

RXD -------------------------- RX

TXD -------------------------- TX

CHPD ------------------------ VCC

RST -------------------------- Not connected (open)

VCC -------------------------- 3.3V

Arduino uno's reset is connected to ground.

My code:

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

// Update these with values suitable for your network.

const char* ssid = "wifissid";
const char* password = "wificode";
const char* mqtt_server = "mqttipadres"; // IP address of the Broker
uint8_t my_str[6]; // sting to store the incoming data from the publisher
String str;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
const int buttonPin = 4;    // the pin that the pushbutton is attached to
const int ledPin = 12; 

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
   Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("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++) {
    char receivedChar = (char)payload[i];
    Serial.print(receivedChar);
    if (receivedChar == '0')
      digitalWrite(ledPin, HIGH);
    if (receivedChar == '1')
      digitalWrite(ledPin, LOW);
  }
  Serial.println();
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      } 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() {
    Serial.begin(115200);

   // initialize the button pin as a input:
    pinMode(buttonPin, INPUT);
    // initialize the LED as an output:
    pinMode(ledPin, OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void loop() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

    if (buttonState == HIGH) {
      digitalWrite(ledPin, HIGH);
      client.publish("outTopic", "1"); //
      // if the current state is HIGH then the button
      // went from off to on:
    } else {
          digitalWrite(ledPin, LOW);
      // if the current state is LOW then the button
      // went from on to off:
      client.publish("outTopic", "0"); //
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

  {
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
  }
}


void clearstring() {
  //Serial.flush(); // clears the buffer, you dont need this
  for (int r=0; r<7; r++){
  my_str[r] = '\0'; // deletes each block
  }
//  my_str[0] = '\0';
//  my_str[1] = '\0';
//  my_str[2] = '\0';
//  my_str[3] = '\0';
//  my_str[4] = '\0';
//  my_str[5] = '\0';
//  my_str[6] = '\0';
}

Thank you :slight_smile:

Connection.JPG

Up-1

Up-2

OK.

Up-3!