MQTT: il Broker mi rimanda -2

Salve a tutti,
Sto cercando di far comunicare 2 arduino UNO + shield eth W5100 in MQTT via un Broker installato su una RasPI 3 mod B.

Sto usando gli esempi forniti dal grande Paolo Aliverti con dei MKR1010 ma dovendoli adattare alle mie W5100 penso di essermi perso.

Quando testo in locale sulla RasPI il subscriber riceve i messaggi dal publisher, se pingo gli arduino dalla RasPi ritornano le risposte.

Questo é il codice che ho messo sul publisher:

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA };
IPAddress ip(192, 168, 0, 101);
IPAddress myDns(8, 8, 8, 8);
IPAddress gateway(192, 168, 0, 254);
IPAddress subnet(255, 255, 255, 0);


EthernetClient ethClient;

// Make sure to leave out the http and slashes!
IPAddress server(198, 168, 0, 74);  // RASPBERRY BROCKER MQTT

PubSubClient client(ethClient);

unsigned long t1, dt;
int stato = 0;

void setup() {

  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  Serial.begin(9600);
  Serial.println("OK");
 // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
    }
    delay(1000);
  Serial.println("Connected to Network\n");

  client.setServer(server, 1883);

  if (client.connect("arduinopub")) {
    Serial.println("mqtt connected");
    client.subscribe("/hello");
  } else {
    Serial.println("mqtt not connected");
    Serial.print("failed, rc=");
    Serial.println(client.state());
  }
  t1 = millis();
}

void loop() {
  dt = millis() - t1;
  if (dt > 2000) {
    if ( (stato%2) == 0) {
      client.publish("/hello", "on");  
      Serial.println("on");
    } else {
      client.publish("/hello", "off");  
      Serial.println("off");
    }
    stato++;
    t1 = millis();
  }
}

Quando apro la serial sul publisher ho questo:

OK
Connected to Network

mqtt not connected
failed, rc=-2
on
off
on

E sulla shell Subcriber della RasPI non vedo niente... gli on e off non vengono editati.

Questo fantomatico -2 cosa significa?

Grazie in anticipo,
Simone

ciao

il -2 sta per:

#define MQTT_CONNECT_FAILED         -2

e a vedere la libreria vuol dire che non ti sei connesso...sei sicuro delle impostazioni del client?

Grazie Orso2001,
Dagli esempi ho visto che una volta dichiarato su quale mezzo con EthernetClient ethClient; e PubSubClient client(ethClient); non vedo che cosa fare di più...

Mi sfugge qualcosa...

ciao...prova a spostare come prima istruzione nel setup() questa riga di codice:

client.setServer(server, 1883);

ovviamente rimuovendola da dove l'hai messa ora

Risolto!
Ho ripreso dagli esempi della libreria la configurazione della scheda rete e adesso funziona.

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xAA };
IPAddress ip(192, 168, 0, 100);
IPAddress server(192, 168, 0, 74);

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{

  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("/hello");
  }
  pinMode(6, OUTPUT);
  while (!Serial){
    ;  
  }
  Serial.begin(9600);
  Serial.println("OK");
}

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

void callback(char* topic, byte* payload, unsigned int length){
  
  String msg;
  for (int i = 0; i < length; i++) {
    msg += (char)payload[i];  
  }
  
  if (strcmp(topic, "/hello") == 0) {
    if (msg == "on") {
      digitalWrite(6, HIGH);
      Serial.println("on");
      } else if (msg == "off") {
      digitalWrite(6, LOW);
      Serial.println("off");
    }
  }
}

Cercando si impara...

Mi resta una domanda, se in un Topic voglio passare più dati come formattarli?
O conviene fare più Topic differenti, uno per ogni sonda? Uscita?

Consigli prego...

Simone

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.