Issue
My Goal:
I am trying to communicate with an MQTT server (I'm using mosquitto) hosted on a raspberry pi from a ClearCore device which I have programmed using Arduino.
My Issue:
I have found numerous examples online which achieve what I want using similar devices and techniques. However I am unable to make a connection from my ClearCore device using my Arduino software to the MQTT broker hosted on my raspberry pi.
My Setup:
I am using an Ethernet connection from my clear core device to my raspberry pi. I am using a Teknic CLCR-4-13. I am not using DHCP. I am setting the IP address of the raspberry pi each time it reboots so I always know what it is (see command below). I have created the mosquitto.conf file (port: 1883) and the password_file where I define a "username" and "password".
I run this command whenever I reboot my pi so I don't have to make a static IP.
sudo ifconfig eth0 192.168.1.23 netmask 255.255.255.0
What I have tried:
- From my PC - using an Ethernet connection and a python script I am able to connect, subscribe, and publish to my MQTT server using the IP address of the raspberry pi as the name of the MQTT server.
import paho.mqtt.publish as pub
MQTT_SERVER = "192.168.1.23"
MQTT_PATH = "dev/test"
credentials = {'username':"user",'password':"pass"}
import time
while True:
pub.single(MQTT_PATH, "Hello Pi!", hostname = MQTT_SERVER, auth = credentials)
time.sleep(3)
print(".")
- To verify I can transmit data from the ClearCore device and raspberry pi using an Ethernet cable I have successfully sent UDP packages using an Arduino program. I use the same mac and IP address.
- I have tried changing the MQTT version by defining it in the Arduino program to older versions.
- I have used Wireshark to monitor the Ethernet traffic when I run the program to verify a connection attempt is being made.
My Arduino Program:
Note: everything compiles and program runs successfully, but it is unable to connect to MQTT server
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
//#define MQTT_VERSION MQTT_VERSION_3_1
//#define MQTT_VERSION MQTT_VERSION_3_1_1
//#define MQTT_VERSION MQTT_VERSION_5_0
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Set your MAC address and IP address here
byte mac[] = {0x24, 0x15, 0x10, 0xb0, 0x00, 0x3f};
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void setup() {
// Useful for debugging purposes
Serial.begin(9600);
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "myClientID"
if (mqttClient.connect("myClientID","user","pass"))
{
Serial.println("Connection has been established, well done");
// Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
}
void loop() {
mqttClient.loop();
mqttClient.subscribe("dev/test");
if(mqttClient.publish("dev/test", "Hello World"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
// Dont overload the server!
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
Command from Raspberry Pi
mosquitto_sub -d -u user -P pass -t dev/test
I use this to see messages when they come in from the pi.
where it fails...
mqttClient.setServer(server, 1883);
if (mqttClient.connect("myClientID","user","pass"))
{
//error message
}
Thoughts of potential issues:
Most examples I have seen of similar projects - people are using "test.mosquitto.org" as their server name, but because I have configured my own MQTT server on my raspberry pi I instead use the IP address of the raspberry pi as the server name. This worked when I connected from my PC using a python script, but I don't know if this is an issue in my Arduino program.
I hope I provided enough information. Please let me know if I there is something else you might want to see that might help - I appreciate all feedback.
Solution
It looks like you are setting the IP address of your device to be the same as your raspberry pi server.
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
That's not going to work. Make the device IP something different, like IPAddress ip(192, 168, 1, 24)
.
Answered By - CB_Ron Answer Checked By - Katrina (WPSolving Volunteer)