Unable to connect to server 127.0.0.1:1880 through port 1880

Hello

lately, I was trying to deploy Temperature and Humidity measurement on Node-Red Using an ESP32 DevKit module.

Through Arduino IDE I Checked that module successfully reads values and connects to my WiFi.
In Node-Red side, it appears to be all ok, But I have my Doubts.

The Flow that I implemented its this:

and the Code:

#include "WiFi.h"
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
const char* ssid = ////////////////////////wifi Name
const char* password = //////////////////////////WiFi key
int port = 1880;
const char* URL = "SensorTemperatura"; //
const char* host = "http://127.0.0.1:1880";
unsigned long lastSend;
float humidity = 0.0;
float temperature = 0.0;
String payload = "";
DHT dht(DHTPIN, DHTTYPE);

void setup(){
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
Serial.print(".");
} Serial.println(" connected");
dht.begin();
}

void loop(){ if ( millis() - lastSend > 1000 ) {
leerSensor();
delay(10);
json();
servicioPost();
lastSend = millis();
}
}void leerSensor() { humidity = dht.readHumidity();// Lee la humedad
temperature = dht.readTemperature(); // Lee la temperatura
if (isnan(humidity) || isnan(temperature)) { // Verifica si hay errores de lectura en el sensor.
Serial.println("Failed to read from DHT sensor!"); // Mensaje de falla en el sensor
return; //Si los hay, vuelve a intentar la comunicaciĂłn }}
}
}

void servicioPost() {
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, port)) {
Serial.println("connected]");
Serial.println("[Sending a request]");
client.print(String("POST /") + URL + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Accept: /" + "*\r\n" + "Content-Length: " + payload.length() + "\r\n" + "Content-Type: application/json" + "\r\n" + "\r\n" + payload );
Serial.println("[Response:]");
while (client.connected() || client.available())
{ if (client.available())
{ String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else { Serial.println("connection failed!]");
client.stop();
}
}

 void json() {
  String humedad = String(humidity, 2);
  String temperatura = String(temperature, 2);
  
  payload = "{"; payload += "\"temperatura\":"; payload += temperatura;
  payload += ",";
  payload += "\"humedad\":"; payload += humedad;
  payload += "}";
  Serial.println(temperatura);
  }

and the Error that I Received its this:

will have any idea what I'm doing wrong????

Regards!!

I'm a newbie here. Since you are specifying the port (1880) in the port variable, are you sure you need it in the host parameter too?

Perhaps try removing ":1880" from your host string and try again?

1 Like

This code will only work if node-red runs on that same ESP32. You see, the reason it works when running from the debugger is that 127.0.0.1 means the local machine, itself. And when the debugger and node-red run on the same machine that’s good. And when you connect to node-red in the browser on that same machine 127.0.0.1 will work too. But from the ESP32’s perspective, 127.0.0.1 points to itself, aka the ESP32. So it needs an address for the machine running node-red instead, in such a format that it can understand it. For example a local ip address in the 192.168.x.x range when located in that same network, or perhaps an external ip address that is forwarded to that machine if it is located outside of that network.

3 Likes

hello

you both right in some way

I get my ip adress PC and replace that 127.0.0.1 also remove the port 1880 from host IP
now it seems to connect whit IP 192.168... but is not able to comunicate to node-red just got this.
maybe I need to change the server running IP from cmd to my local IP too??

Connecting to 192.168///// ... connected]
[Sending a request]
[Response:]
HTTP/1.1 404 Not Found

X-Powered-By: Express

Access-Control-Allow-Origin: *

Content-Security-Policy: default-src 'none'

X-Content-Type-Options: nosniff

Content-Type: text/html; charset=utf-8

Content-Length: 165

Vary: Accept-Encoding

Date: Sun, 09 Feb 2020 22:31:29 GMT

Connection: close

Error
Cannot POST /SensorTemperaturaHTTP/1.1

[Disconnected]

Can you export your NR flow and attach it to a reply.

Also, have you thought about using MQTT to send the data? Once you get the hang of it, it is easy to setup and use to communicate back and forth between devices.

[
    {
        "id": "5763bf65.3cfac8",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": ""
    },
    {
        "id": "9361adcc.7fa1f",
        "type": "debug",
        "z": "5763bf65.3cfac8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 840,
        "y": 340,
        "wires": []
    },
    {
        "id": "2ec217e7.c3b658",
        "type": "http response",
        "z": "5763bf65.3cfac8",
        "name": "confirmaciĂłn mensaje",
        "statusCode": "",
        "headers": {},
        "x": 810,
        "y": 100,
        "wires": []
    },
    {
        "id": "740c83f7.76b10c",
        "type": "http in",
        "z": "5763bf65.3cfac8",
        "name": "SensorTemperatura",
        "url": "test.mosquitto.org",
        "method": "post",
        "upload": true,
        "swaggerDoc": "",
        "x": 380,
        "y": 160,
        "wires": [
            [
                "9361adcc.7fa1f",
                "2ec217e7.c3b658"
            ]
        ]
    }
]

there is

ok, lets backup for a moment. Can you please describe your environment.

  1. what platform is NR installed on?
  2. what version of NR and node.js are running? (see startup log to get version #'s)
  3. did you just add the url for 'test.mosquitto.org' before sending the flow?
  4. can you describe how you expect the data to get from the esp32 to NR?

When I suggested using MQTT, I should have explained that you need to install a broker somewhere (on the device running NR is a good place), alter your sketch to use the pub/sub library and then use the mqtt-in node in your flow.

how to install mosquitto will depend on the platform NR is running on. For a Pi, there are many tutorials such as https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/

For the sketch again you can find tutorials on the web such as https://www.digikey.com/en/maker/blogs/2018/how-to-use-basic-mqtt-on-arduino

While I originally used a sketch to send the readings from my dht22, I'm now flashing my WeMos's with ESPeasy which makes adding sensors a snap once it is all setup.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.