arduino + esp8266 - how to read button press on arduino and send it via esp8266

Hi guys.

I thought I solved that problem but while being at 80% of my project i discovered that core of it has terrible malfunction. I hope you guys are going to help me.

So i have arduino with buttons. Pressing those buttons sends 1 for first button, 2 for second button, 4 for third and so on - powers of number 2. If i press first and second button it gives me return value 3, if there is no button pressed i get 0. Arduino has 8 buttons so pressing all buttons in one time returns 511. The number is being sent only on button change. So it doesnt send a number continously, but only “on change”. All that works perfectly fine.
I also have esp8266 module connected to arduinos TX and RX pins (pin 0 and 1). I can send data via Serial1.print to esp module. Esp module sends data that comes from arduino to the local network and it is get by windows application on specified port. All that works.

So if i press button 3 it goes like this:
button press → analize which button is pressed → send digit 4 to esp → esp gets data from arduino → esp sends digit 4 to the port on the web server

The problem comes when I start to press buttons like crazy as fast as i can (i expect such behaviour from users). I expect from side of my web server to get:

4
0
4
0
4
0
4
0
4
0

and so on because i press and release a button.

unfortunatelly i sometimes get something like this:

4
0
4
0
4
0
4
4
0
4

It happens 2-3 times on 100 presses. As you can seen one of button releases was lost. I checked it on many ways and i am almost sure that esp didnt send that missing 0, but surely it has been sent to esp. Probably it didnt get it from arduino and problem lays within readStringUntil function.

Here is code from arduino:

String serialResponse = "";

void setup()
{
    Serial.begin(9600);
    Serial1.begin(256000);
      
    Serial.setTimeout(5);
    Serial1.setTimeout(5);
    
    for (int i = 0; i < numberOfButtons; i++) button[i] = i + startPin;
    for (int i = 0; i < numberOfButtons; i++) buttonStatus[i] = false;
    for (int i = 0; i < numberOfButtons; i++) pinMode(button[i], INPUT_PULLUP);
}

void loop()
{

    if ( Serial.available()) {
        serialResponse = Serial.readStringUntil('\r\n');
        Serial1.println(serialResponse);
        Serial.println(serialResponse);
    }

 
    int currentButtonValue = 0;
    for ( int i=0; i < numberOfButtons; i++) {
      currentButtonValue = currentButtonValue + int(buttonStatus[i] * (0.5 + pow(2, i)));
    }

    if ( currentButtonValue != previousButtonStatus ) {
      previousButtonStatus = currentButtonValue;
      Serial1.println(currentButtonValue);
      Serial.println(currentButtonValue);
    }
  }

And here my esp code:

#include <ESP8266WiFi.h>

const char* ssid = "xxx";
const char* pass = "xxx";
const char* host = "192.168.0.10";
int port = 7777;
    
WiFiClient client;

void setup() {

    Serial.begin(256000);
    Serial.setTimeout(5);

    
    WiFi.begin(ssid, pass);
    

    while(WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
    }
        
    
    Serial.println("\nStarting connection to server...");
    if (client.connect(host, port)) {
        Serial.println("Connected to server.");
        client.print("WIFI Module connected:" + WiFi.localIP());
    }
}


void loop() {

  if ( client.available()) {
    String WiFiResponse = client.readStringUntil('\n');
    Serial.println(WiFiResponse);
  }

  if ( Serial.available()) {
    String SerialResponse = Serial.readStringUntil('\r\n');
    client.print(SerialResponse);
    
    
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnecting from server.");
    client.stop();

    while(true);
  }

What i checked and didnt help:

  • changing baud in both codes: 9600, 57600, 115200, 256000
  • changing settimeout value up to 500 hoping the longer time it will wait it will grab that missing 0.

I decided to use readStringUntil because there is different ammount of bytes being sent to esp. I sometimes also get different signs from ESP instead of digits for example “¦Hø”

Is there something i am doing wrong?
Thanks in advance!

    for (int i = 0; i < numberOfButtons; i++) button[i] = i + startPin;
    for (int i = 0; i < numberOfButtons; i++) buttonStatus[i] = false;
    for (int i = 0; i < numberOfButtons; i++) pinMode(button[i], INPUT_PULLUP)

You only need one for loop. You can do all the stuff in one for loop.
You only need one for loop. You can do all the stuff in one for loop.
You only need one for loop. You can do all the stuff in one for loop.

You will need to point out where you actually read the state of the pins.