I'm trying to connect an ESP8266 to my laptop to a mosquitto broker. I have tried both windows and ubuntu but none worked!.
This is the windows part:
The mosquitto broker is online:
C:\Program Files\Mosquitto>mosquitto -v
1625556778: mosquitto version 2.0.11 starting
1625556778: Using default config.
1625556778: Starting in local only mode. Connections will only be possible from clients running on this machine.
1625556778: Create a configuration file which defines a listener to allow remote access.
1625556778: For more details see https://mosquitto.org/documentation/authentication-methods/
1625556778: Opening ipv4 listen socket on port 1883.
1625556778: Opening ipv6 listen socket on port 1883.
1625556778: mosquitto version 2.0.11 running
This is the arduino code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "Laurium";
const char* password = "pass";
//const char* mqtt_server = "192.168.1.35"; I tried this as well
IPAddress mqtt_server = (192, 168, 1, 35);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
This the output on the terminal:
WiFi connected
IP address:
192.168.137.90
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
My laptop local IP: 192.168.1.35
So it successfully connects to the WIFI but cannot find the broker. I also followed This thread but didn't work. I even added 1883 port as an inbound port in the windows firewall with a new TCP rule. I also entirely disabled the firewall or switched to an older version of mosquitto where remote clients were allowed by default All to no avail.
The ubuntu part:
Here I modified the code slightly. I also added the ping
command so I could check if my esp8266 can ping the google? And can it ping the destination mosquitto server IP or not?
I installed mosquito on my ubuntu vmware and passed the vmware IP obtained by IP config command in my host machine, the windows.
I also tested my mosquitto on ubuntu and it worked fine with test publisher-subscriber on ubuntu.
On ubuntu unlike windows, pining the mosquitto IP was successful but the mosquitto terminal did not show anything.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266Ping.h>
// Update these with values suitable for your network.
const char* ssid = "Laurium";
const char* password = "pass";
//const char* mqtt_server = "192.168.186.1";
IPAddress mqtt_server = (192, 168, 186, 1);
//IPAddress mqtt_server = (192, 168, 15, 1);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
bool ret = Ping.ping(mqtt_server);
Serial.println(ret);
Serial.println(Ping.ping("www.google.com"));
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
//IPAddress ip (192, 168, 0, 1); // The remote ip to ping
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
This time, the output was this:
WiFi connected
IP address:
192.168.137.109
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
1
1
Unlike windows, pinging the IP where the mosquitto server resides is successful, but still not establishing any connection.
If I could work with any of the windows or ubuntu mosquitto servers it is fine.
Thanks.