ESP32 HTTP status code 404 for some reason

Hi there, I'm having a project that can send data from ESP32 to google sheets but I have an issue right here that is the HTTP status code is 404 and I don't know why that happened.

Here's a picture of the error on the Serial Monitor:

and here's the code:

#include <WiFi.h>
#include <HTTPClient.h>

const char * ssid = "Wifi name";
const char * password = "Wifi pass";
String GOOGLE_SCRIPT_ID = "AKfycbw4rA_eBkHPMkgRX4mLH2lscyZLGDVA21HpFngqFEc";



unsigned long Time;
String param = "";

#define RXD2 16
#define TXD2 17


void setup()
{
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Time = millis();
  WiFi.mode(WIFI_STA);
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, password);
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("CONNECTED !!!");
}


// ======================================================
void loop()
{
  if ((unsigned long)millis() - Time > 1000)
  {
    print_speed();
    Time = millis();
  }
}


void print_speed()
{
  if (Serial2.available())
  {
    param  = "forceData=" + Serial2.readString() ;

    Serial.println(param);
    write_to_google_sheet(param);
  }
  else
  {
    Serial.println("No data !!!");
  }

}
/******/
void write_to_google_sheet(String params)
{
  HTTPClient http;
  String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + params;
  //Serial.print(url);
  Serial.println("Data sent to Google Sheets");
  //---------------------------------------------------------------------
  //starts posting data to google sheet
  http.begin(url.c_str());
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  int httpCode = http.GET();
  Serial.print("HTTP Status Code: ");
  Serial.println(httpCode);
  //---------------------------------------------------------------------
  //getting response from google sheet
  String payload;
  if (httpCode > 0) {
    payload = http.getString();
    Serial.println("Payload: " + payload);
  }
  //---------------------------------------------------------------------
  http.end();
}

Could it be that you are not connected to your WiFi when you try to send the data?

well the connection was completely normal

i tested everything but still, it still has the 404 code

but one thing is that I use arduino UART to send data from the arduino to the esp32 and then the esp32 sends that data to the google sheet. The funny thing is that when I disconnect the arduino, the esp32 can send something to the google sheet, and the HTTP status code is 200

Is your program that you posted running on the ESP32?

1 Like

yes it is

and here's the code of the Arduino, it sends the MPU6050 values to the ESP32, then the ESP32 sends this data to the google sheet

#include <MPU6050.h>
MPU6050 mpu;

#define EXPIRED                    true
#define stillTIMING                false

#define LEDon                      HIGH   
#define LEDoff                     LOW

#define PUSHED                     HIGH    
#define RELEASED                   LOW

#define CLOSED                     LOW   
#define OPEN                       HIGH

#define ENABLED                    true
#define DISABLED                   false

#define YES                        true
#define NO                         false

const float thresholdForce = 5.0;
const unsigned long resetInterval = 500;
static float peakForce = 0, lastPeakForce = 0;
static unsigned long lastPeakMs = 0;



//const int arraySize = 200; //array size
//float dataArray[arraySize]; // array for storing values
//byte currentIndex = 0; // current index of the array

struct makeTIMER
{
  unsigned long Time;           //when the TIMER started
  unsigned long Interval;       //delay time in ms which we are looking for
  bool          timerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool          Restart;        //restart this TIMER ? YES/NO

  //****************************************
  //fuction to check if the TIMER is enabled and check if the TIMER has expired
  bool checkTIMER()
  {
    //*********************
    //is this TIMER enabled and has this TIMER expired ?
    if (timerFlag == ENABLED && millis() - Time >= Interval)
    {
      //*********************
      //should this TIMER restart again?
      if (Restart == YES)
      {
        //restart this TIMER
        Time = millis();
      }

      //this TIMER is enabled and has expired
      return EXPIRED;
    }

    //this TIMER is disabled and/or has not expired
    return stillTIMING;
  } //END of   checkTime()
}; //END of   struct makeTIMER

makeTIMER forceChecker =
{
  0, 500ul, ENABLED, NO      //Time, Interval, timerFlag, Restart
};

makeTIMER delayValue =
{
  0, 25ul, ENABLED, NO      //Time, Interval, timerFlag, Restart
};

void setup() {
  //Serial.begin(115200);
  Serial.begin(9600);
  
  Wire.begin();
  mpu.initialize();
  
  //measure range of mpu6050(±16g)
  mpu.setFullScaleAccelRange(3);

  delay(1000);
}

void loop(){

  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  static float smoothForce = 0;

  float gForceX = (float(ax) / 32768.0) * 16.0;
  float gForceY = (float(ay) / 32768.0) * 16.0;
  float gForceZ = (float(az) / 32768.0) * 16.0;

  float accelX = gForceX;
  float accelY = gForceY;
  float accelZ = gForceZ;
  float accelTotal = (sqrt(pow(accelX, 2) + pow(accelY, 2) + pow(accelZ, 2))*9.81) - 9.81;

  smoothForce += 0.01 * (accelTotal - smoothForce);

  float myForce = smoothForce;  // smoothed accelTotal;

  if (myForce > thresholdForce && myForce > peakForce){
    peakForce = myForce;
    lastPeakForce = peakForce;
    lastPeakMs = millis(); 
    forceChecker.Time = millis(); 
  }

  if(forceChecker.checkTIMER() && peakForce > 0){
    peakForce = 0;
    Serial.println(lastPeakForce); //sends this to the ESP32
  }
}

I see. Could you print the URL the ESP32 tries to connect to, too, when it does gets 404?

1 Like

Just the URL?

String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + params;
This one right?

oh and one more thing is that every time I want to upload the code to the esp32, I have to hold the boot button on the esp32, don't know if that will affect the code


here it is, and when I copied this and pasted it into my search bar, things were still normal
here's a pic of it

Now that's odd. 404 comes from the server, so you have a connection and the URL looks sane. Can you fire up wireshark and look at the traffic? Maybe the URL is not that sane as it looks...

1 Like

I'm going to do it right now

Is this a program that I have to download?

well I downloaded it, so what should I do now


well I don't know what to say about this but the traffic looks kinda... unstable

I tried another code but it still not working

I'm having a feeling about the boot mode, like every time I try to upload the code, I have to hold the boot button