i'm currently trying to send a randomly generated number from Arduino via ESP8266 wifi module to a local mysql database estabilished with xampp, but having a problem with it. Here is my arduino and php receiving code. I should mention that i'm extremely unfamiliar with mysql, php, and the whole server & database things, so any help would be greatly appreciated.
Arduino Code:
#include <ESP8266WiFi.h>
//#define DHTPIN D2
//#define DHTTYPE DHT11
//DHT dht(DHTPIN, DHTTYPE);
/* Parameters:
* t = temperature
* h = humidity
* i = ID
*/
const char* server = "192.168.1.106"; // ganti dengan alamat servermu
const char* ssid = "DARUSALAM C8"; // ganti dengan SSID modem/router wifimu
const char* password = "asro4520"; // ganti dengan password utk konek ke wifimu
const char* SensorID = "ESP001"; // ini sekedar identitas sensormu
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// dht.begin();
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
}
void loop() {
// float h = dht.readHumidity();
// float t = dht.readTemperature();
float n = random(300);
// if (isnan(h) || isnan(t)) {
// Serial.println("Gagal membaca sensor DHT");
// return;
// }
// Serial.print("Temperature: ");
// Serial.print(t);
// Serial.print(" *C. Humidity: ");
// Serial.println(h);
Serial.print("Random Number: ");
Serial.println(n);
if (client.connect(server,80)) {
Serial.print("Posting data...");
digitalWrite(BUILTIN_LED, LOW);
// Serial.println("Temperature: " + String(t) + ", Humidity: " + String(h));
Serial.println("Random Number: " + String(n));
// client.println("GET /dht.php?t=" + String(t) + "&amp;h=" + String(h) + "&amp;i=" +SensorID+ " HTTP/1.1");
client.println("GET /log.php?n=" + String(n));
client.println("HOST: ");
client.println(server);
client.println("Connection: close");
client.println();
/*
while (client.connected() &amp;&amp; !client.available()) delay(1);
while (client.connected() || client.available()) {
char c = client.read();
Serial.print(c);
}
*/
client.stop();
Serial.println();
digitalWrite(BUILTIN_LED, HIGH);
}
delay(5000); // Beri delay 5 detik sebelum polling berikutnya
}
PHP code:
<?php
$db_amb = mysqli_connect("127.0.0.1", "root", "", "wemos");
if (!$db_amb) die("Gagal terkoneksi ke DB Utama. Error : " . mysqli_connect_error());
$n = $_REQUEST['n'];
if (!empty($n) &&)
{
$s = "insert into data_random set value1='$n'";
$r = mysqli_query($db_amb, $s);
echo "OK";
}
else echo "ERR";
?>;
The database is named 'wemos', and the table where i intend to store the data is named 'data_random' with 4 columns named value1-value4. My intentions is to store the random number to the 'value1' field.