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>
const char* server = "192.168.1.106";
const char* ssid = "DARUSALAM C8";
const char* password = "asro4520";
const char* SensorID = "ESP001";
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
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 n = random(300);
Serial.print("Random Number: ");
Serial.println(n);
if (client.connect(server,80)) {
Serial.print("Posting data...");
digitalWrite(BUILTIN_LED, LOW);
Serial.println("Random Number: " + String(n));
client.println("GET /log.php?n=" + String(n));
client.println("HOST: ");
client.println(server);
client.println("Connection: close");
client.println();
client.stop();
Serial.println();
digitalWrite(BUILTIN_LED, HIGH);
}
delay(5000);
}
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.
EDIT the problem i encountered is whenever i checked http://localhost/log.php
this appears:
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\log.php on line 7
i have tried modifying the syntax in numerous ways but the problem presist. I suspect my code may have some underlying fundamental problem that is not syntax related, but i can't tell due to my lack of experience. Again, any help/clue is greatly appreciated. Thank you very much