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:
1#include <WiFi.h>2#include <HTTPClient.h>3#include <LiquidCrystal_I2C.h>456#define SENSOR_INPUT 36//157#define LIGHT_PORT 1289LiquidCrystal_I2C lcd(0x27,16,2);10const String ssid = "REDACTED";11const String password = "REDACTED";12const String serverName = "https://soil-moisture-monitor-phi.vercel.app/api/updateMeasurement/";13int prevHumidity = 0;1415void setup() {16 Serial.begin(9600);17 pinMode(LIGHT_PORT, OUTPUT);18 lcd.init();19 lcd.clear(); 20 lcd.backlight(); 21 WiFi.begin(ssid, password);22 while(WiFi.status() != WL_CONNECTED){23 Serial.print("Connecting...");24 delay(100);25 }26 Serial.println("Connected!");27 // delay(1000);28 pinMode(LIGHT_PORT, OUTPUT);29 if(WiFi.status() == WL_CONNECTED){30 WiFiClient client;31 HTTPClient http;32 33 // Your Domain name with URL path or IP address with path34 http.begin(client, serverName);35 const char* headerNames[] = { "Location", "Last-Modified" };36 http.collectHeaders(headerNames, sizeof(headerNames)/sizeof(headerNames[0]));37 // Data to send with HTTP POST38 String payload = "{\"measurement\": "+String(12345) + "}";39 // Authorization40 // Send HTTP POST request41 http.addHeader("Content-Type", "application/json");42 http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);43 int httpResponseCode = http.POST(payload);44 Serial.print("HTTP Response code: ");45 Serial.println(httpResponseCode);46 if (httpResponseCode == 308) {47 String location = http.header("Location"); // Get redirect target48 http.end();4950 Serial.println("Redirected to: " + location);5152 // Try the new location53 http.begin(client, location);54 http.addHeader("Content-Type", "application/json");55 httpResponseCode = http.POST(payload);56 Serial.print("New code: ");57 Serial.println(httpResponseCode);58 }59 // Free resources60 http.end();61 }62 else {63 Serial.println("WiFi Disconnected");64 }65}6667void loop() {68 // put your main code here, to run repeatedly:69 int sensorOutput = analogRead(SENSOR_INPUT);70 int humidityPercentage = map(sensorOutput, 0, 4095, 100, 0);71 if (humidityPercentage < 30) {72 digitalWrite(LIGHT_PORT, HIGH);73 } else {74 digitalWrite(LIGHT_PORT, LOW);75 }76 prevHumidity = humidityPercentage;77 lcd.setCursor(1,0);78 lcd.print("Soil Humidity:");79 lcd.setCursor(6,1);80 lcd.print(String(humidityPercentage)+"%");81 // When the probe is not in soil it reports moisture as 0 or 1%82 // if (humidityPercentage != prevHumidity && humidityPercentage > 1){ 83 // if(WiFi.status() == WL_CONNECTED){84 // WiFiClient client;85 // HTTPClient http;86 87 // // Your Domain name with URL path or IP address with path88 // http.begin(client, serverName);8990 // // Data to send with HTTP POST91 // String httpRequestData = "";92 // // Authorization93 // // Send HTTP POST request94 // http.addHeader("Content-Type", "application/json");95 // int httpResponseCode = http.POST("{\"measurement\": "+String(humidityPercentage) + "}");96 // Serial.print("HTTP Response code: ");97 // Serial.println(httpResponseCode);98 // // Free resources99 // http.end();100 // }101 // else {102 // Serial.println("WiFi Disconnected");103 // }104 // }105 if (humidityPercentage < 30) {106 digitalWrite(LIGHT_PORT, HIGH);107 } else {108 digitalWrite(LIGHT_PORT, LOW);109 }110 prevHumidity = humidityPercentage;111}112
My webapp code is here: http://github.com/sr5434/soil-moisture-monitor