Hello,
Need help, iam using arduino uno, esp8266 01 wifi module and using ph sensor to send data to my hosting web, but the data always show 0 in my web
arduino code:
#include <SoftwareSerial.h>
SoftwareSerial wifi (8, 9); // Rx Tx
#define nama_wifi "wifi"
#define pass_wifi "pass"
String ServerIP = "103.253.212.154";
String host = "monitoring.zasckyweb.my.id";
//====================untuk pH====================
float calibration_value = 21.34 + 0.5;
int phval = 0;
unsigned long int avgval;
int buffer_arr[10], temp;
float ph;
boolean connected = false;
String data2server;
String cipsend;
void setup() {
// put your setup code here, to run once:
wifi.begin(9600);
Serial.begin(9600);
wifi.setTimeout(5000);
Serial.println("ESP8266 cek cek");
delay (1000);
wifi.println("AT+RST");
delay(1000);
if (wifi.find("WIFI GOT IP")) // jika error trus, di at command bisa, coba ganti "OK" atau "Ready"
{
Serial.println(" ESP8266 SIAP ");
}
else {
Serial.println(" Tidak Ada Response dari ESP8266 ");
while (1);
}
delay(1000);
for (int i = 0; i < 5; i++) {
connect_to_wifi();
if (connected) {
break;
}
}
if (!connected) {
while (1);
}
delay(5000);
wifi.println("AT+CIPMUX=0");
delay(1000);
}
void loop() {
//====================pH====================
for (int i = 0; i < 10; i++)
{
buffer_arr[i] = analogRead(A1);
delay(30);
}
for (int i = 0; i < 9; i++)
{
for (int j = i + 1; j < 10; j++)
{
if (buffer_arr[i] > buffer_arr[j])
{
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1024 / 6;
ph = -5.70 * volt + calibration_value;
Serial.print("pH Val: ");
Serial.println(ph);
delay(1000);
String url_post_data_air = "POST /write-data.php"; //29
String url_value_data_air = "data=";
url_value_data_air += ph;
// Membuat TCP connection, UDP transmission or SSL connection
wifi.print("AT+CIPSTART=\"TCP\",\"");
wifi.print(ServerIP);
wifi.println("\",80");
ShowResponse(500);
data2server = url_post_data_air;
data2server += " HTTP/1.0\r\nHost: "; //17
data2server += host;
data2server += "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "; //67
data2server += url_value_data_air.length();
data2server += "\r\n\r\n"; //4
data2server += url_value_data_air;
data2server += "\r\n\r\n"; //4
int i_jumlah_karakter = url_post_data_air.length() +17+ ServerIP.length() + 67 + 2 + 4 + url_value_data_air.length() +4;
String jumlah_karakter = String(i_jumlah_karakter);
cipsend = "AT+CIPSEND=";
cipsend += jumlah_karakter;
cipsend += "\r\n";
//send data
wifi.print(cipsend);
delay(100);
// http request
wifi.println(data2server);
// menutup TCP connection, UDP transmission atau SSL connection
wifi.println("AT+CIPCLOSE=4");
ShowResponse(500);
delay(2000);
}
void connect_to_wifi()
{
wifi.println("AT+CWMODE=1");
String cmd = "AT+CWJAP=\"";
cmd += nama_wifi;
cmd += "\",\"";
cmd += pass_wifi;
cmd += "\"";
wifi.println(cmd);
Serial.println(cmd);
if (wifi.find("OK")) {
Serial.println("Berhasil Terkoneksi ke internet");
connected = true;
} else {
Serial.println("Gagal Terkoneksi");
connected = false;
}
}
void ShowResponse(int wait) {
Serial.print("response ESP : ");
long timeNOW = millis();
String hasilread;
while (millis() - timeNOW < wait) {
while (wifi.available()) {
hasilread = wifi.readString();
Serial.println(hasilread);
delay(10);
return;
}
}
}
write-data.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Variabel database
$servername = "localhost";
$username = "***************";
$password = "*************";
$dbname = "***************";
$conn = mysqli_connect("$servername", "$username", "$password","$dbname");
$data = $_POST['data'];
// Prepare the SQL statement
$result = mysqli_query ($conn,"INSERT INTO datasensor (data) VALUES ('$data')");
if ($result) {
// successfully inserted
$response["success"] = 1;
$response["message"] = "Data successfully created.";
// Show JSON response
echo json_encode($response);
} else {
// Failed to insert data in database
$response["success"] = 0;
$response["message"] = "Something has been wrong";
// Show JSON response
echo json_encode($response);
}
?>
and the messeage on serial monitor:
ESP8266 cek cek
ESP8266 SIAP
AT+CWJAP="ssid","pass"
Berhasil Terkoneksi ke internet
pH Val: 13.57
response ESP :
AT+CIPMUX=0
OK
AT+CIPSTART="TCP","103.253.212.154",80
CONNECT
OK
response ESP : AT+CIPSEND=139
OK
> a=13.57
AT+CIPCLOSE=4
busy s...
Recv 139 bytes
SEND OK
pH Val: 13.72
response ESP : AT+CIPSTART="TCP","103.253.212.154",80
ALREADY CONNECTED
ERROR
response ESP : AT+CIPSEND=139
OK
> a=13.72
AT+CIPCLOSE=4
busy s...
Recv 139 bytes
SEND OK
+IPD,361:HTTP/1.1 200 OK
Date: Sat, 20 Feb 2021 22:26:05 GMT
Server: Apache
X-Powered-By: PHP/5.6.40
Access-Control-Allow-Origin: *
Content-Security-Policy: upgrade-insecure-requests;
Upgrade: h2,h2c
Connection: Upgrade, close
Vary: Accept-Encoding
Content-Type: application/json; charset=UTF-8
{"success":1,"message":"Data successfully created."}
CLOSED
but the data only show 0
please help
thanks