Need help, void loop if stantment runs only once

Hello, i'm 15 y old from Croatia, and i am trying to make use esp8266 with mqtt. i have 3 led's and 3 buttons hooked up to esp, and i wrote a code that will send data number when each button is pressed, and that will also read if something was send from another esp. and at the very end of my code i added if stantment that should tell me if when a button on 1.st esp is pressed, the second esp picked it up, and print "working" to serial monitor.

So my problem is that, it does what it has to do, but it does it only once, when i press the button, it prints "working", but when i press button again, it doesnt work, it acts like it reads if statment only once. Plz help

Btw, in this section,
"const char* ssid = "My wifi ssid"; // Enter your WiFi name
const char* password = "my wifi pass"; // Enter WiFi password
const char* mqttServer = "my server ip";
const int mqttPort = my mqtt port;
const char* mqttUser = "my mqtt username";
const char* mqttPassword = "my mqtt pass";"
i deleted my actual info, so someone don't just steal it.

Thanks,
Matej

Code:

"

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
const char* ssid = "My wifi ssid"; // Enter your WiFi name
const char* password =  "my wifi pass"; // Enter WiFi password
const char* mqttServer = "my server ip";
const int mqttPort = my mqtt port;
const char* mqttUser = "my mqtt username";
const char* mqttPassword = "my mqtt pass";

short message_recived;
 
WiFiClient espClient;
PubSubClient client(espClient);

int button_crvena = 5;
int button_zelena = 4;
int button_zuta = 14;

int led_crvena = 12;
int led_zelena = 13;
int led_zuta = 15;

int button_crvena_State = 0;
int button_zelena_State = 0;
int button_zuta_State = 0;
 
void setup() {
 
  Serial.begin(115200);

  pinMode(led_crvena, OUTPUT);
  pinMode(led_zelena, OUTPUT);
  pinMode(led_zuta, OUTPUT);

  pinMode(button_crvena, INPUT_PULLUP);
  pinMode(button_zelena, INPUT_PULLUP);
  pinMode(button_zuta, INPUT_PULLUP);

  digitalWrite(led_crvena, LOW);
  digitalWrite(led_zelena, LOW);
  digitalWrite(led_zuta, LOW);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266_Matej", mqttUser, mqttPassword )) {
 
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
 
  client.publish("esp_for_matej", "esp_2_booted"); //Topic name
  client.subscribe("esp_for_matej");
 
}
 
void callback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");

  for (int i = 0; i < length; i++) {
    message_recived += (char)payload[i];
    Serial.print((char)payload[i]);
  }
 
  Serial.println();
  Serial.println("-----------------------");
 
}
 
void loop() {
  client.loop();

  button_zuta_State = digitalRead(button_zuta);
  button_zelena_State = digitalRead(button_zelena);
  button_crvena_State = digitalRead(button_crvena);

  if (digitalRead(button_crvena_State) == HIGH) {
    client.publish("esp_for_matej", "5");
  }
  
  if (digitalRead(button_zelena_State) == HIGH) {
    client.publish("esp_for_matej", "6");
  }
  
  if (digitalRead(button_zuta_State) == HIGH) {
    client.publish("esp_for_matej", "7");
  }  

  if (message_recived == '1'){
    client.publish("esp_for_matej", "esp_2_2_recived");
  }
  if (message_recived == '2'){
    client.publish("esp_for_matej", "esp_2_2_recived");
  }
  if (message_recived == '3'){
    client.publish("esp_for_matej", "esp_2_3_recived");
  }

  delay(100);

}

"

Welcome @jetamgamer

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Welcome to the forum

One problem that I see is that message_recived can only hold 2 bytes because of

short message_recived;

but then you do this

for (int i = 0; i < length; i++) {
message_recived += (char)payload[i];
Serial.print((char)payload[i]);
}

What? "short int" is a 16-bit integer so it can hold the sum of at least 256 8-bit integers.

I admit that it's quite strange to be adding all of the characters in a message together and then comparing the sum against a single character:

  if (message_recived == '1')
  if (message_recived == '2')
  if (message_recived == '3')

It will work the first time for a single-character message.

To get it to work a second time, set 'message_received' back to 0 before adding the characters to it. It will still only work for single-character messages.

Perhaps the OP intended 'message_received' to be a String. That would accumulate multiple characters. It would still have to be cleared (set to "") before each message arrives.

My mistake, I mistook what was going on here

Hi, no unfortunetly it is not that,
i added this line of code:

  if (message_recived == '1') {
    client.publish("esp_for_matej", "esp_2_2_recived");
    message_recived == 0;
  }

So, it should work, but it doesn't. Also i changed the "short" variable into "short int"

It's actually my second time that i have this problem, i worked on a simmular project in the past, and i also had this problem, and in the end i quit the project because i couldn't figure out what was wrong.

Also, it is supposed to be a number message (single character), bc i'm sharing node-red hosting with my friend, so i made it that way, he cant know what i mean by that numbers. So, it is not a problem that the message is one character.

Thanks again,
Matej

You meant:
message_recived = 0;

So, i tried again, and nothing. when i send number 5, it works for the first time, and than it just spits out random numbers, like 9 and 8, that are not eaven in use.

Idk what to do at this point, im thinking about canceling this project.

Thanks,
Matej

Please post your full revised sketch

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

const char* ssid = ""; // Enter your WiFi name
const char* password =  ""; // Enter WiFi password
const char* mqttServer = "";
const int mqttPort = ;
const char* mqttUser = "";
const char* mqttPassword = "";

WiFiClient espClient;
PubSubClient client(espClient);

int button_crvena = 5;
int button_zelena = 4;
int button_zuta = 14;

int led_crvena = 12;
int led_zelena = 13;
int led_zuta = 15;

int button_crvena_State = 0;
int button_zelena_State = 0;
int button_zuta_State = 0;

short int message_recived;

void setup() {

  Serial.begin(115200);

  pinMode(led_crvena, OUTPUT);
  pinMode(led_zelena, OUTPUT);
  pinMode(led_zuta, OUTPUT);

  pinMode(button_crvena, INPUT_PULLUP);
  pinMode(button_zelena, INPUT_PULLUP);
  pinMode(button_zuta, INPUT_PULLUP);

  digitalWrite(led_crvena, LOW);
  digitalWrite(led_zelena, LOW);
  digitalWrite(led_zuta, LOW);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266_Lucija", mqttUser, mqttPassword )) {

      Serial.println("connected");

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

  client.publish("esp_for_lucija", "esp_1_booted"); //Topic name
  client.subscribe("esp_for_lucija");

}

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived in topic: ");
  Serial.println(topic);

  Serial.print("Message:");

  for (int i = 0; i < length; i++) {
    message_recived += (char)payload[i];
    Serial.print((char)payload[i]);
  }

  Serial.println();
  Serial.println("-----------------------");

}

void loop() {
  client.loop();


  button_zuta_State = digitalRead(button_zuta);
  button_zelena_State = digitalRead(button_zelena);
  button_crvena_State = digitalRead(button_crvena);

  if (digitalRead(button_crvena_State) == HIGH) {
    client.publish("esp_for_lucija", "1");
  }

  if (digitalRead(button_zelena_State) == HIGH) {
    client.publish("esp_for_lucija", "2");
  }

  if (digitalRead(button_zuta_State) == HIGH) {
    client.publish("esp_for_lucija", "3");
  }

  Serial.print(message_recived);
  if (message_recived == '5') {
    Serial.print("Working1");
    message_recived = 0;
  }

  if (message_recived == '6') {
    Serial.print("Working2");
  }

  if (message_recived == '7') {
    Serial.print("Working3");
  }
    delay(100);
  }

Try replacing this whole loop with:
message_recived = payload[0];

Please explain what the above line is supposed to do.

In the context of your program, it does not make sense to sum the numeric values of incoming characters.

Sorry for late response,

It just puts data that it recived itno a variable that i can use.

Matej

Hi, sorry for late response.

So, i tried, and nothing at all. But, i tested, and it works, when i send 1, it works for the first time, but not second.

Hi, so i managed to get it working:

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

const char* ssid = "*; // Enter your WiFi name
const char* password =  "*"; // Enter WiFi password
const char* mqttServer = "*";
const int mqttPort = *;
const char* mqttUser = "*";
const char* mqttPassword = "*";

short message_recived;

WiFiClient espClient;
PubSubClient client(espClient);

int button_crvena = 5;
int button_zelena = 4;
int button_zuta = 14;

int led_crvena = 12;
int led_zelena = 13;
int led_zuta = 15;

int button_crvena_State = 0;
int button_zelena_State = 0;
int button_zuta_State = 0;

void setup() {

  Serial.begin(115200);

  pinMode(led_crvena, OUTPUT);
  pinMode(led_zelena, OUTPUT);
  pinMode(led_zuta, OUTPUT);

  pinMode(button_crvena, INPUT_PULLUP);
  pinMode(button_zelena, INPUT_PULLUP);
  pinMode(button_zuta, INPUT_PULLUP);

  digitalWrite(led_crvena, LOW);
  digitalWrite(led_zelena, LOW);
  digitalWrite(led_zuta, LOW);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266_Matej", mqttUser, mqttPassword )) {

      Serial.println("connected");

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

  client.publish("esp_for_lucija", "esp_2_booted"); //Topic name
  client.subscribe("esp_for_lucija");

}

void callback(char* topic, byte* payload, unsigned int length) {

  message_recived += (char)payload[0];

}

void loop() {

  client.loop();

  if (message_recived == '1') {
    Serial.println("Working");
    message_recived = 0;
  }

  button_zuta_State = digitalRead(button_zuta);
  button_zelena_State = digitalRead(button_zelena);
  button_crvena_State = digitalRead(button_crvena);


  if (digitalRead(button_crvena_State) == HIGH) {
    client.publish("esp_for_lucija", "5");
  }

  if (digitalRead(button_zelena_State) == HIGH) {
    client.publish("esp_for_lucija", "6");
  }

  if (digitalRead(button_zuta_State) == HIGH) {
    client.publish("esp_for_lucija", "7");
  }

  delay(100);

}

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