coasty
April 6, 2024, 4:04pm
1
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?
coasty
April 7, 2024, 1:44pm
4
well the connection was completely normal
coasty
April 7, 2024, 1:45pm
5
i tested everything but still, it still has the 404 code
coasty
April 7, 2024, 1:50pm
6
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
coasty
April 8, 2024, 3:59am
9
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
coasty
April 8, 2024, 10:01am
12
String url = "https://script.google.com/macros/s/ " + GOOGLE_SCRIPT_ID + "/exec?" + params;
This one right?
coasty
April 8, 2024, 2:05pm
13
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
coasty
April 8, 2024, 2:09pm
14
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
coasty
April 8, 2024, 2:57pm
16
I'm going to do it right now
coasty
April 8, 2024, 2:58pm
17
Is this a program that I have to download?
coasty
April 8, 2024, 3:11pm
18
well I downloaded it, so what should I do now
coasty
April 8, 2024, 3:19pm
19
well I don't know what to say about this but the traffic looks kinda... unstable
coasty
April 8, 2024, 3:43pm
20
I tried another code but it still not working
coasty
April 8, 2024, 3:47pm
21
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