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

I am trying to create a small project on arduino (WeMos D1 mini). It is to be based on the communication of several devices with a computer using the MQTT protocol.

The protocol itself works great on 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. I need to create a network of devices.

Is it possible to execute the sequence on several devices communicating via MQTT?

Example: Own MQTT Broker on Raspberry Pi. Three Arduino (id: 001; 002; 003) with the same program. 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.

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.

Today I have a problem because when I run all Arduino's at the same time, only one is active on the network. the other two are unresponsive although the Broker receives the information. 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). 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. The rest are dosn't work (are not visible in network).

I need help getting a network of these devices up and running and communicating between them.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation