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
版权声明:本文标题:ESP32 can't connect on RPI mqtt broker - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280993a1926176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论