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”:
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?