-->
Page 1 of 1

ESP8266 will talk to REST API’s except for the one I’ve made

PostPosted: Thu May 19, 2022 6:22 am
by Jacob O'Neill
Hi this is my first post so if I do anything wrong I'm sorry!
I currently have a Express REST API running on my mac, my mac has a static IP address of 192.168.1.9 on the LAN and the server is running on port 3000

I created a GET route to get data from a database on my machine, if I use postman on it from the server machine or another machine then each machine receives a response hence why I don't think the server is the problem

When I try to do the same GET request on my DFROBOT Fire-beetle ESP8266 board then it comes up with a WiFi 3 status code (connected) but a http response code of -1 (no response from server) which would indicate that the board isn't working, however when I changed the address to "http://numbersapi.com/random/math" then it worked absolutely fine. I have no idea what the problem with the code is and I swear it was working just fine yesterday (although intermittently it would disconnect"

Please help

Heres the Arduino code:
Code: Select all// Dependencies
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>


// Variables
const String ssid = "[INSERT NETWORK SSID]";
const String pass = "[INSERT NETWORK PASSWORD]";
const String serverIP = "192.168.1.9:3000"; //static ip of my mac with api running

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200);

  delay(1000); Serial.print("\n");

  WiFi.begin(ssid, pass);
  Serial.print("Connecting to " + ssid);
  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.print(".");
  }
  Serial.print("\nConnection Established with IP: "); Serial.println(WiFi.localIP());
}


void loop() {
  // Send an HTTP GET request depending on timerDelay
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;

      String serverPath = serverIP + "/api/machines";
     
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
     
      // Send HTTP GET request
      int httpResponseCode = http.GET();
     
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
        Serial.println(WiFi.status());
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}


OUTPUT:
Connecting to SSID.........
Connection Established with IP: 192.168.1.107
Error code: -1
3