How to display localhost server in node red

Hello, I have made the local server using the ESP8266 web browser which is named as "192.168.1.110" and its is display the values of my sensor in the web browser also.
Without using MQTT I want to display the data in debug node in the node-red.
But while using HTTP node I am not able to make it work properly due to something is missing.
I have made the flow for testing which is mentioned in the snap

Any advice on this will be helpful.

Hi @Varul92

the HTTP In node you have used is for defining HTTP endpoints hosted by the Node-RED runtime.

To get data from another HTTP server, you should use the HTTP Request node instead.

1 Like

Hi @knolleary thanks for the reply, will it work with HTTP request node only or do I have to use any other node also actually I have configured my ESP8266 as mentioned below code

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <Wire.h>
#include<ESP8266WebServer.h>

static float humidity,cTemp,fTemp;

//I2C registe for SHT31
#define Addr 0x44

const char* ssid = "ssid";
const char* password = "password";
IPAddress ip(192,168,1,110);
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0); 
ESP8266WebServer server(80);


void reconnect(){
  WiFi.disconnect(true);
  delay(100);
  Serial.print(WiFi.softAP(ssid,password)? "Wifi Connected" : "Not Connected");
  delay(100);
  WiFi.softAPConfig(ip,gateway,subnet);
  Serial.print(WiFi.softAPIP());  
}

void setup() {
  // Initialize serial port
  Serial.begin(115200);
   // Initialise I2C communication as MASTER
  while (!Serial) continue;
  Wire.begin(2,14);
  reconnect();
   // Start to listen
  server.begin();
  server.on("/",jsonRoot);
}

void loop() {
sht31();
server.handleClient(); 
}

void jsonRoot(){

  server.send(200,"application/json",Buff());
  
}
String Buff(){
DynamicJsonBuffer jsonBuffer;
   //StaticJsonBuffer<300> jsonBuffer;
JsonObject& root =jsonBuffer.createObject();
root["device"] = "Huzzah Board";
root["sensorType"] = "SHT31";
JsonArray& temp = root.createNestedArray("temp");
temp.add(String(cTemp,2));
temp.add(String(fTemp,2));
temp.add(String(humidity,2));
String JSON;
root.printTo(Serial);
root.prettyPrintTo(JSON);
return JSON;
  
}

void sht31()
{
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Stop I2C Transmission
  Wire.endTransmission();

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

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
data[4] = Wire.read();
data[5] = Wire.read();
  }
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float ctemp = -45.0 + (175.0 * temp / 65535.0);
  float ftemp = (cTemp * 1.8) + 32.0;
  float humid = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

  // 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");
  Serial.print("Relative Humidity :");
  Serial.print(humid);
  Serial.println(" %RH");
  delay(500);

   cTemp = ctemp;
   fTemp = ftemp ;
  humidity = humid;
  
}

and getting the reply on my web browser like this

but even after using the HTTP request and mentioned URL details and connected with debug node, I am still not able to receive the data

What does your flow look like? If you can load the data in your browser then, assuming Node-RED is running on the same network as your device, then it will be able to get the data with the HTTP Request node.

You also need an inject node to trigger the request node to do the request.

I don´t know how the ESP webserver works. Having a look on your code my guess is that the statement server.on("/",jsonRoot); is where you define an endpoint. My suggestion is to change this to something like server.on("/mything",jsonRoot); . In Node-RED your flow will have only three nodes. The http request should be configured like below:

@knolleary and @Andrei
I have connected the ESP8266 in local wifi and I have hosted a web page on the same network, so my device webpage and node red are on the same network (Local Wifi).
Further, I have created a flow with HTTP request node, Now I want a response from the webpage in the node-red flow.

I am a bit confused about which IP should I use in URL option

You want Node-RED to load the webpage from your ESP8266, you need the IP address of your ESP8266. As you are currently serving it as it's root page you should only need the IP address. So I would start by copying what worked for you in your browser when you connected to the ESP8266.

Just to be as clear as possible...

In the screenshot of your browser showing the data from the ESP, you used the address http://192.168.1.29/root to access it. That is exactly the address you'd put the url field of the HTTP Request node.

With the help of inject node, I am able to read the sensor data.
But is there any other way which automatically get the response from the webpage?

It depends what you mean by "automatically"

If you wish to use a web server on your ESP8266, you could set the inject node to repeat at an interval for example each minute

But it's really what MQTT is very good at, but for whatever reason you have ruled that out.

1 Like