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