-1

I've been looking around for a solution, but couldn't manage to find one. I'm looking to send a post request from an ESP8266 on a local API. Here is my code :

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

String ssid = "HUAWEI P30 lite";
String password = "testazerty";
String serverName = "http://192.168.56.1:3030/drink/voltron";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
    
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;
      
      http.begin(client, serverName);
      
      http.addHeader("Content-Type", "application/json");
      int httpResponseCode = http.POST("{\"city\":\"toronto\"}");

      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
        
      http.end();
  }
}

Both my computer and ESP8266 are connected to my phone network (HUAWEI).

192.168.56.1 is the IP of my computer.

API is running, and I can successfully make the request from postman (see image below).

enter image description here

It always return -1 as http code. I activated debug level HTTP_CLIENT, here is what I have on the serial monitor :

11:07:41.645 -> SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
11:07:41.645 -> fpm close 1 
11:07:41.645 -> mode : sta(30:83:98:b1:da:62)
11:07:41.645 -> add if0
11:07:41.645 -> Connecting
11:07:42.160 -> .....scandone
11:07:45.348 -> state: 0 -> 2 (b0)
11:07:45.348 -> .state: 2 -> 3 (0)
11:07:45.441 -> state: 3 -> 5 (10)
11:07:45.441 -> add 0
11:07:45.441 -> aid 6
11:07:45.441 -> cnt 
11:07:45.441 -> 
11:07:45.441 -> connected with HUAWEI P30 lite, channel 6
11:07:45.441 -> dhcp client start...
11:07:45.864 -> ....ip:192.168.43.88,mask:255.255.255.0,gw:192.168.43.1
11:07:47.870 -> .
11:07:47.870 -> Connected to WiFi network with IP Address: 192.168.43.88
11:07:47.870 -> [HTTP-Client][begin] url: http://192.168.56.1:3030/drink/voltron
11:07:47.870 -> [HTTP-Client][begin] host: 192.168.56.1 port: 3030 url: /drink/voltron
11:07:47.870 -> [HTTP-Client][sendRequest] type: 'POST' redirCount: 0
11:07:53.021 -> [HTTP-Client] failed connect to 192.168.56.1:3030
11:07:53.021 -> [HTTP-Client][returnError] error(-1): connection failed
11:07:53.021 -> HTTP Response code: -1
11:07:53.021 -> [HTTP-Client][end] tcp is closed
7
  • firewall on PC? Commented Jul 8, 2022 at 9:36
  • I completely turned off Windows Defender, but still have the same output Commented Jul 8, 2022 at 9:46
  • 1
    Can your PC ping the ESP32? Commented Jul 8, 2022 at 10:07
  • I tried to do it connecting to my home router, and it worked perfectly ! The problem comes from sharing internet with my mobile, but at least it can work with router. I looked it up, and seems that it might be because of the protocol used (IPv4/IPv6). Here is what I found. Commented Jul 8, 2022 at 13:05
  • 1
    localhost is the device itself ... synonymous with 127.0.0.1 ... did you mean to say local host instead? Commented Jul 8, 2022 at 21:50

1 Answer 1

0

TLDR:

This is a networking problem, not a programming problem. Try changing your subnet mask to 255.255.0.0 on your router/network configuration, or make sure they're actually connected to the same router. If different routers, try to change one to a "Wireless Access Point" configuration. If you can't get them on the same network, you will have to expose your API to the Internet.

Long version:

You are on different subnets. Your API IP address is 192.168.56.1 and your device IP address is 192.168.43.88, but your subnet mask is 255.255.255.0. That puts these devices on separate subnets within the network.

There are a lot of complicated explanations of subnets and subnet masks on the internet. I linked one of the better ones below. Technically, you are not just on different subnets but on different networks entirely. You have a 192.x.x.x IP address, making it a Class C network. That makes your address pattern one of "network.network.network.node". Depending on your router configuration or other network hardware, you can upgrade it to a Class B network by customizing your subnet mask as 255.255.0.0 ("network.network.node.node"). Class B addresses might require a lot of customization on your end depending on your specific circumstances. By using a subnet mask of 255.255.0.0 on a Class B address, you classify the IP address parts .56.1 and .43.88 as being on the same network AND subnet. If you change it to Class B addressing without changing the subnet, the .56 will be one subnet and .43 will be a different subnet.

Sometimes this addressing is done automatically based on the installed hardware. This is especially problematic in home networks containing multiple wireless routers but connected to the same modem. Many home routers have a "Wireless Access Point" setting that removes a lot of the more complex router configuration. This might convince your router(s) to put you onto the same subnet. If that works, you now have your devices able to connect locally! Additional router configuration might be required to expose ports for your API to incoming requests across subnets.

If your network class is being determined by your ISP and you can't get your devices onto the same network, you have to expose your API to the Wild Wild Web :D There are many services which allow for Dynamic DNS, but I'd suggest reading through https://www.ionos.com/digitalguide/server/tools/free-dynamic-dns-providers-an-overview/ and selecting one that meets your needs. This WILL require advanced router configuration to expose ports to the internet. Please select a Dynamic DNS services that provides DDOS protection. Once it is exposed, your ESP8266 can hit your API as it would any public API. Please put authentication on your API.

References:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.