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.