I am using an Adafruit ESP32-S3 board and trying to send temperature and humidity data to a php file on a website. The sketch compiles and runs, returning a HTTP Response code of 200.
However the data.php does not receive any data.
I added a var_dump($_POST); command to the start of the data.php, which returns array(0) { }.
Despite lot's of searching and different code this has me stumped. So I would appreciate any help from the brains trust!
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
const char* ssid = "myssid";
const char* password = "mypassword";
const char* serverName = "https://mywebsite.com/mypath/data.php";
String apiKeyValue = "apiKeyValue";
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
bool status = bme.begin(0x77);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
while (1);
}
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure();
HTTPClient https;
https.begin(*client, serverName);
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(bme.readTemperature())
+ "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = https.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
https.end();
}
else {
Serial.println("WiFi Disconnected");
}
delay(30000);
}