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 = "";
const char* ssid = "DARUSALAM C8";
const char* password = "asro4520";
const char* SensorID = "ESP001";

WiFiClient client;

void setup() {

  digitalWrite(BUILTIN_LED, HIGH);

  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi Connected");

void loop() {
float n = random(300);

Serial.print("Random Number: ");

  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("Connection: close");


    digitalWrite(BUILTIN_LED, HIGH);


PHP code:


$db_amb = mysqli_connect("", "root", "", "wemos");
if (!$db_amb) die("Gagal terkoneksi ke DB Utama. Error : " . mysqli_connect_error());

$n = $_REQUEST['n'];
if (!empty($n) &amp;&amp;)
  $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

  • What sort of a problem? (Please edit the answer into the question.) – Mark Smith Feb 16 '17 at 20:52

The HOST should not have a newline after it, so you should have

client.print("HOST: ");

In addition your PHP looks dodgy to me. I'm not very familiar with PHP at all, but I think the &amp;s here are left over from something you have removed, and causing syntax errors - just remove them

if (!empty($n) &amp;&amp;)

I believe your n parameter is going to be in _GET (not _REQUEST). Also, you should check whether it's there, not just assume it is, so:

if isset ( $_GET["n"] )
    $n = $_GET["n"];
    // Do your database stuff here

(You should find the PHP log (where depends on what OS your server is running on) and I think it would point you straight to this problem. No point trying to debug it blind.)

  • First of all thank you for cleaning up my mess. Due to your suggestion i have removed the newline after HOST. Yes, i also suspect that the &amp;s is what causing the syntax error because you are also correct in that i only edited someone else's program i found from browsing the internet. but now when i checked the local host i got Notice: Undefined index: n in C:\xampp\htdocs\log.php on line 6 ERR;. I installed xampp in windows 10, can you point out the PHP log location? thank you very much – Naufal Nasiri Feb 16 '17 at 21:17
  • I don't understand what you mean by "checked the local host". I don't know the location of the PHP log in Windows, sorry, but I think I've seen another the mistake in your code - answer ammended. – Mark Smith Feb 17 '17 at 11:40
  • The $_REQUEST super global should work just fine. It contains the contents _GET, _POST, and _COOKIE. php.net/manual/en/reserved.variables.request.php. – Mazaryk Mar 19 '17 at 16:16
  • You will get the index not defined error if you load the page in your browsers with out the "?n=" part. So to test the script in your browser use: localhost/log.php?n=123 – Mazaryk Mar 19 '17 at 16:18

Change this:

client.println("GET /log.php?n=" + String(n));
client.println("HOST: ");

To this:

client.print("GET /log.php?n=" + String(n));
client.println( " HTTP/1.0" );
client.print("Host: ");

This means you're sending the Host: header + value on one line, and you're correctly appending the HTTP/1.0\n after your URL-request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.