I'm using Arduino Nano ESP32 and Bme680.
When I run the code,
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Wire.h>
#include "bsec.h"
// Replace with your network credentials
const char* ssid = "XXXX";
const char* password = "XXXX";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "https://example.com/post-data.php";
// Keep this API Key value to be compatible with the post-data.PHP.
String apiKeyValue = "tPmAxxxxF9";
#define SEALEVELPRESSURE_HPA (1013.25)
void checkIaqSensorStatus(void);
void errLeds(void);
// Create an object of the class Bsec
Bsec iaqSensor; //instantiate the BSEC object
String output;
String httpRequestData;
void setup() {
/* Initializes the Serial communication */
Serial.begin(115200);
Wire.begin(); //initialize I2C communication
delay(1000);
/* WiFi Connection*/
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());
/*Sensor Connection*/
pinMode(LED_BUILTIN, OUTPUT);
iaqSensor.begin(BME68X_I2C_ADDR_LOW, Wire); //initialize BME68X sensor with I2C address and Wire object
checkIaqSensorStatus();
bsec_virtual_sensor_t sensorList[7] =
{
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_STATIC_IAQ,
BSEC_OUTPUT_CO2_EQUIVALENT,
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_STABILIZATION_STATUS,
BSEC_OUTPUT_RUN_IN_STATUS
};
iaqSensor.updateSubscription(sensorList, 7, BSEC_SAMPLE_RATE_LP); //update subscription with sensor list, size of list, and sample rate
checkIaqSensorStatus();
}
void loop() {
/*Check WiFi connection status*/
if(WiFi.status()== WL_CONNECTED){
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure(); //don't use SSL certificate
HTTPClient https;
// Your Domain name with URL path or IP address with path
https.begin(*client, serverName);
// Specify content-type header
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (iaqSensor.run())
{
// Prepare your HTTP POST request data
httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(iaqSensor.iaq)
+ "&value2=" + String(iaqSensor.co2Equivalent) + "&value3=" + String(iaqSensor.breathVocEquivalent) + "";
}
else
{
checkIaqSensorStatus();
}
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = https.POST(httpRequestData);
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("HTTP Response: ");
Serial.println(https.getString());
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
https.end();
delete client;
}
else
{
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(1000);
}
// Helper function definitions
void checkIaqSensorStatus(void)
{
if (iaqSensor.bsecStatus != BSEC_OK) {
if (iaqSensor.bsecStatus < BSEC_OK) {
output = "BSEC error code : " + String(iaqSensor.bsecStatus);
Serial.println(output);
for (;;)
errLeds(); /* Halt in case of failure */
} else {
output = "BSEC warning code : " + String(iaqSensor.bsecStatus);
Serial.println(output);
}
}
if (iaqSensor.bme68xStatus != BME68X_OK) {
if (iaqSensor.bme68xStatus < BME68X_OK) {
output = "BME68X error code : " + String(iaqSensor.bme68xStatus);
Serial.println(output);
for (;;)
errLeds(); /* Halt in case of failure */
} else {
output = "BME68X warning code : " + String(iaqSensor.bme68xStatus);
Serial.println(output);
}
}
}
void errLeds(void)
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
The serial output prints:
httpRequestData: api_key=tPmAxxxxxF9&value1=50.00&value2=600.00&value3=0.50
HTTP Response code: 200
HTTP Response:
But nothing is inserted to my database table (I'm using Bluehost).
However if I'm sending
curl -X POST -d "api_key=txxxxx&value1=27.88&value2=9.45&value3=1004.7" "https://iotgassensory.com/post-data.php"
in command prompt, it successfully insert the data, so I think my post-data.php file works.