1

I am really new to IoT and MQTT. For my project I want ESP32 to send data to node-red as a part of visualization requirement. I am running a broker (mosquitto) locally on my PC (Windows 11), I downloaded nodejs, node-red and also mosquitto broker on pc. I made a simple flow on node-red dashboard as seen below. MQTT node is configured to localhost:1880 and topic is "esp32/VOC”:

enter image description here

On the esp32 end I have this simple code where it publishes random numbers to the node-red topic. The esp32 is connected to the same PC for programming. This is the code:

#include <WiFi.h>
#include <PubSubClient.h>

const char *ssid = "iPhone";
#its a phone hotspot my router stopped working conveniently !const char *password = "password";
const char *mqtt_server = "192.168.100.218";
#my laptop IP address.I also tried localhost IP address const int mqtt_port = 1883;
const char *mqtt_topic = "esp32/VOC";

WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
    Serial.begin(115200);
    Serial.println(WiFi.localIP());
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
    client.setServer(mqtt_server, mqtt_port);
}

void loop()
{
    if (client.connected())
    {
        Serial.println("Connected to MQTT broker...");
    }
    else
    {
        Serial.println("Connectinggg to MQTT broker...");
    }
    client.loop();
    int random_value = random(0, 501); // generate random number between 0-500
    client.publish(mqtt_topic, String(random_value).c_str());
    delay(1000);
}

On arduino IDE it prints ("conectinggg") forever so it is connecting to the MQTT broker. I searched online and saw couple of threads with solutions that mainly consisted of adding arguments to the conf file as I did below:

allow_anonymous true
listener 1883 

in cmd window i run the following command a

C:\Users\azak2\mosquitto>mosquitto -v -c test.conf

and returns this after uploading the code to the esp32. this commands also starts mosquitto. prior I turn the service on through "Services"

1690378669: mosquitto version 2.0.15 starting
1690378669: Config loaded from test.conf.
1690378669: Opening ipv6 listen socket on port 1883.
1690378669: Opening ipv4 listen socket on port 1883.
1690378669: mosquitto version 2.0.15 running
1690378679: New connection from ::1:60157 on port 1883.
1690378679: New client connected from ::1:60157 as nodered_0b5676548d046883 (p2, c1, k60).
1690378679: No will message specified.
1690378679: Sending CONNACK to nodered_0b5676548d046883 (0, 0)
1690378679: Received SUBSCRIBE from nodered_0b5676548d046883
1690378679:     esp32/VOC (QoS 2)  #this the topic I created on the esp32
1690378679: nodered_0b5676548d046883 2 esp32/VOC
1690378679: Sending SUBACK to nodered_0b5676548d046883
1690378739: Received PINGREQ from nodered_0b5676548d046883
1690378739: Sending PINGRESP to nodered_0b5676548d046883
1690378799: Received PINGREQ from nodered_0b5676548d046883
1690378799: Sending PINGRESP to nodered_0b5676548d046883

What am I missing?

2 Answers 2

0

You can only run one instance of Mosquitto at a time on any given port number, so by default that will be port 1883.

Without any configuration file the mosquitto instance started by the Windows service will bind to port 1883 only on localhost, so will not be accessible from any other machine.

The Windows service by default loads a file called mosquitto.conf in the install directory as it's configuration file.

You will need to rename the test.conf and ensure it is in the right location then restart the service to get it to pick up the changes.

You should also ensure that you have opened port 1883 in the Windows Firewall if applicable.

1
  • I did that, I deleted test.conf and edited mosquitto.conf to include the arguments above. I have also allowed inbound tcp connection through my firewall the problem still persists.
    – Zak
    Commented Jul 26, 2023 at 10:28
0

Sorry, missed this on first read:

const char* ssid = "iPhone"; #its a phone hotspot my router stopped working conveniently! 

Multiple devices connected to an iPhone can not communicate with each other:

https://discussions.apple.com/thread/7895078

This means the ESP32 will not be able to connect to the laptop.

2
  • I see however I since relocated to another setting where I have a router connection. could it be a problem on esp32"s end like programming problem? I am going to try a different esp32.
    – Zak
    Commented Jul 26, 2023 at 10:41
  • Device Isolation is also present on other WiFi Access Points, you should check you can actually communicate between devices.
    – hardillb
    Commented Jul 26, 2023 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.