Reputation: 49
I have written a code for my Arduino to read data from sensor and then post
to a PHP file called post-data.php
, which then the PHP will insert the data into a database.
However, my Arduino does not seemed to be able to post
the data or it is not post
ing it correctly.
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#endif
#define signalPin 12
#define sensorPin A0
const char *ssid = "****";
const char *password = "*****";
const char* serverName = "http://smartswitchfyp.rf.gd/post-data.php";
String apiKeyValue = "*******";
ESP8266WebServer server(80);
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
Serial.print("Configuring access point...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFI connected");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
void loop() {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
sensor();
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(current) + "&value2=" + String(power) + "";
//String httpRequestData = "api_key=******9&value1=24.75&value2=49.54";
//Serial.print("httpRequestData: ");
//Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(response);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
delay(1000);
}
I have checked that my serverName
is correct.
I have tested the post-data.php
, and it works fine as there is an update at my database. Below is the test code, test.php
I used to test post-data.php
<html>
<body>
<form action="post-data.php" method="post">
api: <input type="text" name="api_key">
Name: <input type="text" name="value1">
Email: <input type="text" name="value2">
<input type="submit">
</form>
</body>
</html>
And below is my post-data.php
file
<?php
$servername = "sql101.epizy.com";
$dbname = "epiz_28338452_smartswitch";
$username = "epiz_28338452";
$password = "********";
// Keep this API Key value to be compatible with the ESP32 code provided in the project page. If you change this value, the ESP32 sketch needs to match
$api_key_value = "*******";
$api_key = $value1 = $value2 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$api_key = test_input($_POST["api_key"]);
if($api_key == $api_key_value) {
$value1 = test_input($_POST["value1"]);
$value2 = test_input($_POST["value2"]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Sensor (value1, value2) VALUES ('" . $value1 . "', '" . $value2 . "')";
$result = $conn->query($sql);
if ($result === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
else {
echo "Wrong API Key provided.";
}
}
else {
echo "No data posted with HTTP POST.";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
I'm 90% sure that is not the post-data.php
file problem but my Arduino not able to post
the data to the php file.
At my Arduino code, I used the line
http.begin(serverName);
to connect to the post-data.php
and then prepare the header:
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
and prepare the content:
String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(current) + "&value2=" + String(power) + "";
where current and power is process in other function/method. I have tested the current and power output, and they are float variables. Finally I used the line
int httpResponseCode = http.POST(httpRequestData);
to post
the 3 data to the php file.
When I startup the Arduino, the output shows HTTP Response code: 200
, which I believe the php file was successfully called (correct me if I am wrong). However, my database does not have any data inserted. Again the test.php
prove that the database can be inserted with data.
Below is image of the database value inserted by the test.php
file
Can anyone help me as I'm not sure what cause the Arduino to not able to post the data. Thanks!
Upvotes: 0
Views: 1330
Reputation: 139
you can't access your infinity free site with your arduino because infinity free have a security system see https://support.infinityfree.net/websites/what-is-the-i-1-suffix/ for more info
Upvotes: 1