Your script works for me on a Wemos D1 Mini publishing to my MQTT broker (192.168.1.156:1883) on a Raspberry Pi. I removed the code for the DS18B20, as I hadn't got one to hand, and just put in a constant (21.6) to simulate the temperature. Looks like the issue could be with your network.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// #include <OneWire.h>
// #include <DallasTemperature.h>
// GPIO where the DS18B20 is connected to
//const int oneWireBus = 4;
//OneWire oneWire(oneWireBus);
//DallasTemperature sensors(&oneWire);
const char* ssid = "Teamwork";
const char* password = "***********";
const char* mqtt_server = "192.168.1.156"; ///MQTT Broker
int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Start the Serial Monitor
Serial.begin(19200);
//sensors.begin();
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
Serial.println("In reconnect...");
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
// char msg[10];
//char msgtext[25];
//String themsg;
//sensors.requestTemperatures();
//float celsius = sensors.getTempCByIndex(0);
//Serial.println(sensors.getTempCByIndex(0));
//char temperaturenow [15];
//dtostrf(celsius,7,3,temperaturenow); // convert floar to char
client.publish("motor/temperature", "21.6");
if (!client.connected()) {
reconnect();
}
client.loop();
delay(1000);
}
exactly my point .. if i use just use a serial monitor the code works fine.. but if i want to use it with mqtt broker and want to see the reading on nodered .. nodemcu shows reconnecting.. i new with nodered but work with arduino ide before so that's why i am confused.. i follow all the steps
yes 192.168.43.160 is the address of windows on which nodered and mqtt working..
and i know that address by cmd command ipconfig..
and i not tried mqtt-out node .. can you you explain me how ? i am new to nodered..
i have seen your previous replies to other problems thats why i came to know that i have to use the ip of the machine , before i was using the 127.0.... haha..
I thought the same that it could be network issue .. but how i find it .. i tried methods available on internet .. i have even done the handshake of mqtt on two cmd windows and work fine
As suggested earlier try using the full ip address in an mqtt In node in node-red to see if it connects (use the full ip address not localhost so it will go through the network stack).
If that doesn't work, does it work with localhost specified?
Also are you able to ping the nodemcu from the windows machine?
You need an inject, mqtt-out, mqtt-in and a debug node (set to display the complete msg object`
connect the inject to the mqtt-out node.
edit the mqtt-out and set the topic to 'my/test'
enter the server IP address (the 192.168.... not tht 127.0.... address) and port 1883
connect the mqtt-in node to the debug node
edit the mqtt-in node and set the topic to 'my/#' (more about the # later)
select the server (the same one used in the mqtt-out node
Now do a deploy. You should see both mqtt nodes with a green dot and the word 'connected' under them. If you don't then you ca't get to the MQTT broker.
If you see the green dot and test it you should see the debug node output the time in msg payload and msg.topic