When I run a CuRL POST request to my Vercel app, I get a 200 response code. When I do the same thing with HTTPClient on my ESP32, I get a 308 Permanent Redirect, with the redirect URL being the same URL I requested, causing an infinite loop.
Curl request: curl -X POST https://soil-moisture-monitor-phi.vercel.app/api/updateMeasurement -H "Content-Type: application/json" -d '{"measurement": 123.45}' ESP32 code:
#include <WiFi.h>#include <HTTPClient.h>#include <LiquidCrystal_I2C.h>
#define SENSOR_INPUT 36//15#define LIGHT_PORT 12
LiquidCrystal_I2C lcd(0x27,16,2);const String ssid = "REDACTED";const String password = "REDACTED";const String serverName = "https://soil-moisture-monitor-phi.vercel.app/api/updateMeasurement/";int prevHumidity = 0;
void setup() { Serial.begin(9600); pinMode(LIGHT_PORT, OUTPUT); lcd.init(); lcd.clear(); lcd.backlight(); WiFi.begin(ssid, password); while(WiFi.status() != WL_CONNECTED){ Serial.print("Connecting..."); delay(100); } Serial.println("Connected!"); // delay(1000); pinMode(LIGHT_PORT, OUTPUT); if(WiFi.status() == WL_CONNECTED){ WiFiClient client; HTTPClient http; // Your Domain name with URL path or IP address with path http.begin(client, serverName); const char* headerNames[] = { "Location", "Last-Modified" }; http.collectHeaders(headerNames, sizeof(headerNames)/sizeof(headerNames[0])); // Data to send with HTTP POST String payload = "{\"measurement\": "+String(12345) + "}"; // Authorization // Send HTTP POST request http.addHeader("Content-Type", "application/json"); http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); int httpResponseCode = http.POST(payload); Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); if (httpResponseCode == 308) { String location = http.header("Location"); // Get redirect target http.end();
Serial.println("Redirected to: " + location);
// Try the new location http.begin(client, location); http.addHeader("Content-Type", "application/json"); httpResponseCode = http.POST(payload); Serial.print("New code: "); Serial.println(httpResponseCode); } // Free resources http.end(); } else { Serial.println("WiFi Disconnected"); }}
void loop() { // put your main code here, to run repeatedly: int sensorOutput = analogRead(SENSOR_INPUT); int humidityPercentage = map(sensorOutput, 0, 4095, 100, 0); if (humidityPercentage < 30) { digitalWrite(LIGHT_PORT, HIGH); } else { digitalWrite(LIGHT_PORT, LOW); } prevHumidity = humidityPercentage; lcd.setCursor(1,0); lcd.print("Soil Humidity:"); lcd.setCursor(6,1); lcd.print(String(humidityPercentage)+"%"); // When the probe is not in soil it reports moisture as 0 or 1% // if (humidityPercentage != prevHumidity && humidityPercentage > 1){ // if(WiFi.status() == WL_CONNECTED){ // WiFiClient client; // HTTPClient http; // // Your Domain name with URL path or IP address with path // http.begin(client, serverName);
// // Data to send with HTTP POST // String httpRequestData = ""; // // Authorization // // Send HTTP POST request // http.addHeader("Content-Type", "application/json"); // int httpResponseCode = http.POST("{\"measurement\": "+String(humidityPercentage) + "}"); // Serial.print("HTTP Response code: "); // Serial.println(httpResponseCode); // // Free resources // http.end(); // } // else { // Serial.println("WiFi Disconnected"); // } // } if (humidityPercentage < 30) { digitalWrite(LIGHT_PORT, HIGH); } else { digitalWrite(LIGHT_PORT, LOW); } prevHumidity = humidityPercentage;}
My webapp code is here: http://github.com/sr5434/soil-moisture-monitor