First of all, this is my first time posting here, so i do apologise if this topic has already been solved,
I have a Mosquito MQTT broker, publishers and subscribers, the Publishers and Subscribers are Arduinos with Ethernet Shield for IP Communication and the Broker is running on an Ubuntu box.
I have everything running well except for the subscribers.
I will leave the sketches for both the Publishers and subscribers at the end of the email.
DESCRIPTION OF PROBLEM
When the subscribers are connected to the same physical Ethernet switch as the Broker, then they are able to connect to it (able to subscribe to the various Topics). From the IDE serial monitor Iām able to confirm this. However, they are unable to connect to the broker from a Point-To-Point wireless link (Ubiquiti NanoStation).
Please note: There is no issue with the Publishers ā They work fine with same Ethernet Library and also over the Point-To-Point link.
REQUEST:
Could anyone please have a look at the sketch and see what I have done wrong. I have been battling this for the past few weeks with no success.
I will really appreciate you help.
Kind regards,
.
SKETCH FOR THE SUBSCRIBERS ā THIS IS THE ONE WITH THE IP COMMUNICATION ISSUES OVER THE PtP LINK.
#include <PubSubClient.h>
#include <Ethernet.h>
const byte LIGHT_PIN = 3; // When activated goes high 5V output when remote key for Alarm panel is pressed
const byte POLICE_PANIC_PIN = 5; // When activated goes high 5V output when remote key for Alarm panel is pressed
const char ID = "Arduino9"; // Unique Name of our client device
const char TOPIC_B = "Outdoor/SecurityLight_Siren_Topic_B"; // Topic to Subscribe to
// Callback Function Header
void callback(char topic, byte payload, unsigned int length);
// Ethernet Shield Networking details (CHANGE THE DETAILS BELOW TO SUIT NETWORK ROUTER HUB CONFIGURATION)
byte mac[] = { 0xED, 0xED, 0xBE, 0xAD, 0xCE, 0xED }; // Unique Device Ethernet shield (W5100) MAC address
IPAddress ip(192, 168, 0, 224); // For Arduino #9 - Topic B Subscriber NEW Ethernet shield (W5100) IP address
IPAddress broker(192, 168, 0, 200); // NEW MTTQ server IP address
// PubSubClient client(wclient); // Setup MQTT client for WiFi Option
EthernetClient ethClient;
PubSubClient client(ethClient);
// Handle incomming messages from the broker
void callback(char* topic, byte* payload, unsigned int length) {
String response;
for (int i = 0; i < length; i++) {
response += (char)payload*;*
-
}*
-
Serial.print("Message has arrived for ");*
-
Serial.print(TOPIC_B);*
-
Serial.print(" ");*
-
Serial.println(response);*
-
Serial.println("Remote Control Security Light/Alarm response change of state");*
-
/// For Topic_B // Police State Panic Activation*
-
if(response == "help") // Turn the light on*
-
{*
-
digitalWrite(POLICE_PANIC_PIN, HIGH); // When the Remote Control through Alarm Panel is activated via message "help" Output 5V to SSR terminals*
-
digitalWrite(LIGHT_PIN, LOW); // onboard indicator LED turn on*
-
}*
-
else if(response == "block") // Turn the Police Strobe and Floodlights off*
-
{*
-
digitalWrite(POLICE_PANIC_PIN, LOW);// OV output - Turn output to the SSR terminals off.*
-
digitalWrite(LIGHT_PIN, HIGH); // onboard indicator LED turn off*
-
} *
}
// Reconnect to client
void reconnect() { -
// Loop until we're reconnected*
-
while (!client.connected()) {*
-
Serial.print("Attempting MQTT connection...");*
-
// Attempt to connect*
-
if(client.connect(ID)) {*
-
client.subscribe(TOPIC_B);*
-
Serial.println("connected");*
-
Serial.print("Subscribed to: ");*
-
Serial.println(TOPIC_B);*
-
Serial.println('\n');*
-
} else {*
-
Serial.println(" try again in 5 seconds");*
-
// Wait 5 seconds before retrying*
-
delay(5000);*
-
}*
-
}*
}
void setup() { -
Serial.begin(115200); // Start serial communication at 115200 baud*
-
pinMode(POLICE_PANIC_PIN, OUTPUT); // Configure POLICE_PANIC_PIN as an output*
-
pinMode(LIGHT_PIN, OUTPUT); // Configure LIGHT_PIN as an output*
-
delay(100);*
-
client.setServer(broker, 1883);*
-
client.setCallback(callback);// Initialize the callback routine*
-
// Ethernet shield configuration*
-
Ethernet.begin(mac, ip);*
-
delay(1500); // Allow hardware to stabilize*
}
void loop() {
- if (!client.connected()) // Reconnect if connection is lost*
- {*
- reconnect();*
- }*
- client.loop();*
}