简体   繁体   English

arduino mqtt - 如何创建多设备序列? MQTT 客户端重命名

[英]arduino mqtt - How to create multi device sequence? MQTT client rename

I am trying to create a small project on arduino (WeMos D1 mini).我正在尝试在 arduino(WeMos D1 mini)上创建一个小项目。 It is to be based on the communication of several devices with a computer using the MQTT protocol.它将基于使用 MQTT 协议的多个设备与计算机的通信。

The protocol itself works great on arduino.该协议本身在 arduino 上运行良好。 I wrote a program that works as expected, at least at this stage of the project.我编写了一个按预期工作的程序,至少在项目的这个阶段是这样。

The problem is that it works fine on a single arduino.问题是它在单个 arduino 上工作正常。 I need to create a network of devices.我需要创建一个设备网络。

Is it possible to execute the sequence on several devices communicating via MQTT?是否可以在通过 MQTT 通信的多个设备上执行序列?

Example: Own MQTT Broker on Raspberry Pi.示例:在 Raspberry Pi 上拥有自己的 MQTT 代理。 Three Arduino (id: 001; 002; 003) with the same program.三个 Arduino (id: 001; 002; 003) 具有相同的程序。 Is the sequence possible: I am sending a startup message from the computer to Arduino 001. The device does some work, then sends a log to the computer and a message to Arduino 002. Device 002 does the job, then sends a log to the computer and a message to Arduino 003. Device 003 does the job, then sends the log to the computer and completes the sequence.顺序是否可行:我正在从计算机向 Arduino 001 发送启动消息。设备完成一些工作,然后向计算机发送日志,并向 Arduino 002 发送消息。设备 002 完成工作,然后将日志发送到计算机并向 Arduino 003 发送消息。设备 003 完成工作,然后将日志发送到计算机并完成序列。

Message from computer to Arduino 001 is sent to Topic device / 001, to Arduino 002 it is sent to Topic device / 002 and similarly to Arduino 003 it is sent to Topic device / 003.从计算机到 Arduino 001 的消息被发送到主题设备/001,到 Arduino 002 它被发送到主题设备/002,类似于 Arduino 003 它被发送到主题设备/003。

Today I have a problem because when I run all Arduino's at the same time, only one is active on the network.今天我遇到了一个问题,因为当我同时运行所有 Arduino 时,只有一个在网络上处于活动状态。 the other two are unresponsive although the Broker receives the information.尽管 Broker 收到了信息,但其他两个没有响应。 Why?为什么?

So, after lond descriptions time for code:因此,经过长时间的代码描述时间:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <PubSubClientTools.h>
#include <Thread.h> 
#include <ThreadController.h>

#define WIFI_SSID           "MyNetwork"
#define WIFI_PASS           "123456789"
#define serial              9600
#define MQTT_SERVER         "192.168.8.107"
#define MQTT_PORT           1883

#define id_dev              "001"


WiFiClient espClient;
PubSubClient client(MQTT_SERVER, MQTT_PORT, espClient);
PubSubClientTools mqtt(client);

ThreadController threadControl = ThreadController();
Thread thread = Thread();

int value = 0;
const String s = "";
String service                  = "service"; 
String main_name                = "test"; 
String sub_main_name            = "test";
String info_name                = "test"; 
String sub_info_name            = "test";

void setup() {
  Serial.begin(serial);
  Serial.println("-| WiFi Connection |--------------------------------");
  setup_wifi();
  Serial.println("-| MQTT Connection |--------------------------------");

  Serial.print(s+"Connecting to MQTT: "+MQTT_SERVER+" ... ");
  if (client.connect("ESP8266Client")) {
    Serial.println("connected");

    mqtt.subscribe(service+"/"+id_dev,          topic_service); 
    mqtt.subscribe(main_name,                   topic_main_name);
    mqtt.subscribe(info_name,                   topic_info_name);
  } else {
    Serial.println(s+"failed, rc="+client.state());
  }

  // Enable Thread
  // thread.onRun(publisher);
  thread.setInterval(2000);
  threadControl.add(&thread);

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

void loop() {
  client.loop();
  threadControl.run();
}

void setup_wifi() {
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void publisher() {
  ++value;
  mqtt.publish("test/001", s+"Hello World! - No. "+value);
}

void topic_service(String topic, String message) {
  Serial.println(s+"Message arrived in function 1 ["+topic+"] "+message);
  sub_main_name = message;
  subscription(sub_main_name);
}

void topic_main_name(String topic, String message) {
  Serial.println(s+"Message arrived in function 2 ["+topic+"] "+message);
  if (message == "111") {
    Serial.println("Is OK!");
    String message_sent = "111";
    mqtt.publish(sub_main_name+"/002", message_sent);
  } else {
    Serial.println("Something wrong!");
  }
}
void topic_info_name(String topic, String message) {
  Serial.println(s+"Message arrived in function 3 ["+topic+"] "+message);
  sub_info_name = message;
}

void subscription(String wiadomosc){
  if(main_name != wiadomosc){
    Serial.println("main_name: " + main_name + " | sub_main_name: " + sub_main_name +" <- for change");
    main_name = sub_main_name+"/"+id_dev;
    info_name = sub_main_name+"/"+id_dev+"/i";
    mqtt.subscribe(main_name, topic_main_name);
    mqtt.subscribe(info_name, topic_info_name);
  }
}

//----------------------------------------------------

The same code will be uploaded to all devices, diferent will be only ID (id_dev).相同的代码将上传到所有设备,不同的只有 ID (id_dev)。 This is a simple version of my program but it still not working well.这是我的程序的一个简单版本,但它仍然不能正常工作。 On one device is ok but if I run two or more... only lastone is active.在一台设备上是可以的,但如果我运行两个或更多......只有 lastone 处于活动状态。 The rest are dosn't work (are not visible in network). rest 不工作(在网络中不可见)。

I need help getting a network of these devices up and running and communicating between them.我需要帮助让这些设备的网络启动并运行并在它们之间进行通信。

I checked on 2 devices logged in at the same time.我检查了同时登录的 2 台设备。

ID 001 (Serial Monitor Tool): ID 001(串行监视器工具):

-| WiFi Connection |--------------------------------
Connecting to LinkHome
.......
WiFi connected
IP address: 192.168.8.132
Mac address: 84:F3:EB:0C:42:5E
-| MQTT Connection |--------------------------------
Connecting to MQTT: 192.168.8.107 ... connected
----------------------------------------------------

ID 002 (Serial Monitor Tool): ID 002(串行监视器工具):

-| WiFi Connection |--------------------------------
Connecting to LinkHome
.......
WiFi connected
IP address: 192.168.8.123
Mac address: 84:F3:EB:0C:42:55
-| MQTT Connection |--------------------------------
Connecting to MQTT: 192.168.8.107 ... connected
----------------------------------------------------

(log from broker): (来自经纪人的日志):

1660763173: New connection from 192.168.8.123:63287 on port 1883.
1660763173: New client connected from 192.168.8.123:63287 as ESP8266Client (p2, c1, k15).
1660763213: New connection from 192.168.8.132:51163 on port 1883.
1660763213: Client ESP8266Client already connected, closing old connection.
1660763213: New client connected from 192.168.8.132:51163 as ESP8266Client (p2, c1, k15).

There seems to be a problem in the log file.日志文件中似乎有问题。 Two devices named ESP8266Client.两个名为 ESP8266Client 的设备。

How can I change the device name?如何更改设备名称?

I checked on 2 devices logged in at the same time.我检查了同时登录的 2 台设备。

ID 001 (Serial Monitor Tool): ID 001(串行监视器工具):

-| WiFi Connection |--------------------------------
Connecting to LinkHome
.......
WiFi connected
IP address: 192.168.8.132
Mac address: 84:F3:EB:0C:42:5E
-| MQTT Connection |--------------------------------
Connecting to MQTT: 192.168.8.107 ... connected
----------------------------------------------------

ID 002 (Serial Monitor Tool): ID 002(串行监视器工具):

-| WiFi Connection |--------------------------------
Connecting to LinkHome
.......
WiFi connected
IP address: 192.168.8.123
Mac address: 84:F3:EB:0C:42:55
-| MQTT Connection |--------------------------------
Connecting to MQTT: 192.168.8.107 ... connected
----------------------------------------------------

(log from broker): (来自经纪人的日志):

1660763173: New connection from 192.168.8.123:63287 on port 1883.
1660763173: New client connected from 192.168.8.123:63287 as ESP8266Client (p2, c1, k15).
1660763213: New connection from 192.168.8.132:51163 on port 1883.
1660763213: Client ESP8266Client already connected, closing old connection.
1660763213: New client connected from 192.168.8.132:51163 as ESP8266Client (p2, c1, k15).

There seems to be a problem in the log file.日志文件中似乎有问题。 Two devices named ESP8266Client.两个名为 ESP8266Client 的设备。

How can I change the device name?如何更改设备名称?


Ok, I found a solution.好的,我找到了解决方案。 It was easier than I thought.这比我想象的要容易。 By the way, I will also be able to change hostnames.顺便说一句,我还可以更改主机名。

To change the hostname I added three lines:要更改主机名,我添加了三行:

#define id_dev              "002"
String newHostname = "client_"+String(id_dev); // for change the hostname
...
void setup_wifi() {
  WiFi.mode(WIFI_STA); // for change the hostname
  WiFi.hostname(newHostname); // for change the hostname
  
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
...

To change mosquitto client name: I change the name in line:要更改 mosquitto 客户端名称:我在行中更改名称:

...
Serial.print(s+"Connecting to MQTT: "+MQTT_SERVER+" ... ");
  if (client.connect("dev_001")) {
    Serial.println("connected");
...

Thanks for the hint with the log file, I admit I didn't think about it.感谢日志文件的提示,我承认我没有考虑过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM