admin管理员组

文章数量:1122832

Helping my son with his project. I have setup static IP address for RPI3 on old router which runs mqtt broker on 1883 port with user and pass. With laptop on the same network we can send message in terminal but ESP get refused, code 2. It worked before static IP, I didn't change any setting on RPI to make it static. I'm not very familiar with linux and networks.

Anyone experienced similar problem?

Here's my sons code, if somethning need to be changed because of static IP.

#include <WiFi.h>
#include <PubSubClient.h>
#define b1 13 
#define b2 12 

const char* ssid = "iotwifi";
const char* password = "ABC2013#";
const char* mqtt_server = "192.168.1.199";
const char* mqtt_user = "korisnik";
const char* mqtt_password = "12345678";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());
}
void reconnect(){
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");


    
    // Connect to the MQTT broker with username and password
    if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}



void setup(){
  Serial.begin(115200);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  setup_wifi();
  
}

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

  if (digitalRead(b1) == 1){
    Serial.println("1 PRESSED");
    client.publish("testTopic", String("1 presssed").c_str());
  }
  if (digitalRead(b2) == 1){
    Serial.println("2 PRESSED");
    client.publish("testTopic", String("2 presssed").c_str());
  }

}

本文标签: ESP32 can39t connect on RPI mqtt brokerStack Overflow