Hey,
I can't set my arduino as cilent on mosquitto broker.
I am trying to connect arduino to mosquitto on raspberry via ethernet cable.
I set raspberry static ip eth0 address 192.168.4.1 . Arduio pings on raspberry but i can't connect to mqqtt server.
I set in mosquitto configure allow_anonymous true but still does not work. Do you guys have any advice? I will be very grateful.
here is code
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 4, 100);
IPAddress server(192, 168, 4, 1); //my raspberry eth0 ip adress
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
//turn the LED ON if the payload is '1' and publish to the MQTT server a confirmation message
if (payload[0] == '1') {
client.publish("outTopic", "value 1");
}
//turn the LED OFF if the payload is '0' and publish to the MQTT server a confirmation message
if (payload[0] == '0') {
client.publish("outTopic", "value 0");
}
} // void callback
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
Serial.println("connected to mqtt");
client.publish("outTopic", "hello there");
client.subscribe("inTopic");
}
}
void loop()
{
client.loop();
}