I HAVE A PROBLEM CONNECTING TO MY RASPBERRY'S BROKER
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "CELERITY_MONTALVAN";
const char* password = "1722510060B.";
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "192.168.1.11";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//uncomment the following lines if you're using SPI
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
float temperature = 0;
float humidity = 0;
// LED Pin
const int ledPin1 = 15;
const int ledPin2 = 2;
const int ledPin3 = 4;
const int ledPin4 = 16;
const int ledPin5 = 17;
void setup() {
Serial.begin(115200);
// default settings
// (you can also pass in a Wire library object like &Wire2)
//status = bme.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Conectando a_ _ ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Mensaje recivido en topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output1") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin1, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin1, LOW);
}
}
if (String(topic) == "esp32/output2") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin2, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin2, LOW);
}
}
if (String(topic) == "esp32/output3") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin3, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin3, LOW);
}
}
if (String(topic) == "esp32/output4") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin4, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin4, LOW);
}
}
if (String(topic) == "esp32/output5") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin5, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin5, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("esp32/output1");
client.subscribe("esp32/output2");
client.subscribe("esp32/output3");
client.subscribe("esp32/output4");
client.subscribe("esp32/output5");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
AND IT IS TRYING TO CONNECT TO THE MQTT PROTOCOL BUT IT DOES NOT CONNECT AS SHOWN IN THE SERIAL PORT
NEED HELP
BEFORE THE SAME CODE IF CONNECTED