Posting date to DB via php code
Posted: Sun Aug 09, 2020 5:26 am
Hi,
Do to ISP's security, I cannot connect directly to the DB Server so I need to send data using API through a PHP file which in turn stores the data into the DB Server.
The PHP file works fine via html form or via python codes so, I'm pretty sure the issue I'm having is with my code.
The error received when the code is executed is "unable to connect". I can only assume that it's an SSL connection issue as the ISP web server needs a secure connection.
How do I do that?
TIA
Do to ISP's security, I cannot connect directly to the DB Server so I need to send data using API through a PHP file which in turn stores the data into the DB Server.
The PHP file works fine via html form or via python codes so, I'm pretty sure the issue I'm having is with my code.
The error received when the code is executed is "unable to connect". I can only assume that it's an SSL connection issue as the ISP web server needs a secure connection.
How do I do that?
TIA
Code: Select all
const char* dbServerName = "https://www.mydomain.com/post_data.php";
void db_send() {
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(dbServerName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//concat data to be sent
String httpRequestData = "api_key=" + apiKeyValue +
"&temp=" + String(t) +
"&humidity=" + String(h) +
"&board=" + board +
"";
//let's see what is being sent
Serial.print("httpRequestData: "); Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: "); Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode); // if -1 connection failed!!
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}