Connect SHT25 I2C module with Node Red using ESP8266

Hi, I am using SHT25 I2C module with esp8266, I am coding on Arduino IDE to send the sensor data to node red and display the reading in Node-Red dashboard,

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#define Addr 0x40
#define wifi_ssid "SSID"
#define wifi_password "Password"
#define mqtt_server "" // Desktop client IP for test
#define humidity_topic "humid"
#define temperature_C "Ctemp"
#define temperature_F "Ftemp"
WiFiClient espClient;
PubSubClient client;
// ***Software Interrupts*** create interval for 5 seconds and execute it in main code
unsigned long Timer = 0;
unsigned long Interval = 5000;
volatile float Ctemp,Ftemp,humid;
void setup() {
  client.setServer(mqtt_server, 1883);
void setup_wifi() {
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
      if (client.connect("ESP8266Client")) {
    else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void loop()
  Timer = millis();
  while(millis()- Timer<=Interval)// use intervels as per mentioned earlier
  if (!client.connected()) {

  //Mentioned below directly executed in String url
//  String tempC =  String(Ctemp, 1); 
//  String tempF =  String(Ftemp, 1); 
//  String humiD =  String(humid, 1); 

  Serial.print("Degree C temperature:");
  client.publish(temperature_C, String(Ctemp).c_str(), true);

  Serial.print("Degree F temperature:");
  client.publish(temperature_F, String(Ftemp).c_str(), true);
  Serial.print("New humidity:");
  client.publish(humidity_topic, String(humid).c_str(), true);

void tempTask()
unsigned int data[2];
  // Start I2C transmission
  // Send humidity measurement command, NO HOLD master
  // Stop I2C transmission

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // humidity msb, humidity lsb
  if(Wire.available() == 2)
    data[0] =;
    data[1] =;
    // Convert the data
    float humidity = (((data[0] * 256.0 + data[1]) * 125.0) / 65536.0) - 6; 
    // Output data to Serial Monitor
    Serial.print("Relative Humidity :");
    Serial.println(" %RH");
    humid = humidity;
  // Start I2C transmission
  // Send temperature measurement command, NO HOLD master
  // Stop I2C transmission

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data 1 byte = 8 bits total 16 bits 
  // temp msb, temp lsb
  if(Wire.available() == 2)
    data[0] =;
    data[1] =;  

    // Convert the data and we will shift MSB value 8 bits (by multiplying with 256) to left and add it with LSB values
    float cTemp = (((data[0] * 256.0 + data[1]) * 175.72) / 65536.0) - 46.85;
    float fTemp = (cTemp * 1.8) + 32;

    // Output data to Serial Monitor
    Serial.print("Temperature in Celsius :");
    Serial.println(" C");
    Serial.print("Temperature in Fahrenheit :");
    Serial.println(" F");
    Ctemp = cTemp;
    Ftemp = fTemp;

but while working on my code I am facing the error of disconnection due to which I am not able to connect with Node red desktop client. I am using the flow as mentioned in the snapshot. Also the topics has been mentioned properly in flow.

Any advice on modification I should use will be a great help.

Your image isn't displaying.
But step one would be to make sure your arduino IDE is closed down. Only one application can use the serial connection and if the IDE has it the connection to Node-RED will fail.

The IP address should be the address of the machine running the MQTT broker. This address is the local (ESP8266) address.

Hi, Please check I guess Now you are able to see the snap shot, And I am not using Arduino IDE while working on node red

Sorry, the MQTT Server IP "" is localhost IP of node red, As you can see in the snapshot also.

Local ESP address is ""

If the MQTT broker is running on the node-red machine:

You need to put the IP of the machine running node red in your ESP8266 code:
#define mqtt_server "IP of node-red machine"

I am initializing Server IP which is mentioned in "NODE- RED Machine" -> Server = "" and port -> 1883 which is mentioned in URL is a loopback IP address for a device, you need the network IP. For example, it the device running NR is a Pi, open a terminal window and enter ifconfig there you will see an IP address like or something like that. That is the IP address you have to give the ESP2866 so it can connect on your local network to the correct device running the MQTT broker.

As others have said, is the local loopback address and not the external ip address you would use to connect to that machine from another.

But another observation; your MQTT nodes in Node-RED are failing to connect. Are you aware that Node-RED does not include an MQTT broker. You need to install one yourself - such as mosquitto.

1 Like

Hi @knolleary Sir,

Thanks a lot, I have just started as a beginner with node red
With the help of the last post, I have installed Mosqitto MQTT broker in node red and even all flows are also connected now

but still not able to connect ESP8266 with node-red


Should I have to install MQTT Message box for Windows ???

As you know MQTT works via a broker
So your data will go

Esp8266 > MQTT Broker
MQTT Broker > Node-RED

So you need to ensure you have configured your Esp8266 correctly with your MQTT broker details

@fredy76 - in your arduino code you have the statement
#define mqtt_server "" // Desktop client IP for test
So your code is saying "connect to the mqqt server at and because the IP means the device this code is running on, connect to the mqtt broker that is running on myself"

...oh but wait there is no MQTT broker running on the WeMos is there...

I apologize for the miscommunication here,
The ESP8266 Device IP is and MQTT server ip is and I am using Pubsubclient library for creating the connection between NodeRed and ESP8266

No it isn't. We have been trying to tell you lots of times that is not the right ip address. is the loopback address - it is how a computer refers to itself. It is not an external address that one computer can use to connect to another.

You need to find out the right ip address of the device running your mqtt broker. It will probably be 192.168.1.something