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 "127.0.0.1" // 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() {
  Wire.begin(2,14);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setClient(espClient);
  }
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);
  WiFi.begin(wifi_ssid, wifi_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
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
      if (client.connect("ESP8266Client")) {
      Serial.println("connected");
    } 
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


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

  //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:");
  Serial.println(String(Ctemp).c_str());
  client.publish(temperature_C, String(Ctemp).c_str(), true);

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

void tempTask()
{
unsigned int data[2];
  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send humidity measurement command, NO HOLD master
  Wire.write(0xF5);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

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

  // Read 2 bytes of data
  // humidity msb, humidity lsb
  if(Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }
    // 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.print(humidity);
    Serial.println(" %RH");
    humid = humidity;
    
  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send temperature measurement command, NO HOLD master
  Wire.write(0xF3);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

  // 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] = Wire.read();
    data[1] = Wire.read();  
  }

    // 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.print(cTemp);
    Serial.println(" C");
    Serial.print("Temperature in Fahrenheit :");
    Serial.print(fTemp);
    Serial.println(" F");
    Ctemp = cTemp;
    Ftemp = fTemp;
    delay(300);  
}

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 "127.0.0.1" is localhost IP of node red, As you can see in the snapshot also.

Local ESP address is "192.168.4.15"

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 = "127.0.0.1" and port -> 1883 which is mentioned in URL

127.0.0.1 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 192.168.1.45 or 10.0.0.45 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, 127.0.0.1 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

arduino

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 "127.0.0.1" // Desktop client IP for test
So your code is saying "connect to the mqqt server at 127.0.0.1 and because the IP 127.0.0.1 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 192.168.1.6 and MQTT server ip is 127.0.0.1 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 127.0.0.1 is not the right ip address.

127.0.0.1 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