So I set up a mqtt broker on localhost and I can receive and send messages (checked on mac terminal as well as on node-red).
But trying in any way to set up the mqtt client on arduino fails ...
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0xB3, 0xF7
};
IPAddress ip(192, 168, 50, 109);
IPAddress myDns(192, 168, 50, 1);
IPAddress gateway(192, 168, 50, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(127, 0, 0, 1);
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void setup()
{
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
if (client.connect("arduinoClient", "XXX", "XXXX")) {
client.publish("Sensors","8 °C");
client.subscribe("ExCom");
}
}
void loop()
{
client.loop();
}
pw and username XXX XXXX are set up correctly !
thank you in advance