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);
}
"