0

I've been trying to send data from my Arduino Yun to a local server using XAMPP. My code is shown below. In the serial monitor of the Arduino Yún, it shows connected and prints the data string, but in the server nothing is showing up. I don't know what's wrong. Maybe this doesn't work on a localhost? Any help would be much appreciated.

I've updated the Arduino code from something I found here.

send_data_ws.ino

#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>

IPAddress server(192,168,1,200); 
YunClient client;

String parametri="";
int x1,x2,x3;

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  String parametri ="";
  delay(2500);
  Serial.println("connecting...");
  x1 = analogRead(A0);
  x2 = analogRead(A1);
  x3 = analogRead(A2);
  if (client.connect(server, 80)) {
    Serial.println("connected");
    delay(2500);
    parametri="x1="+String(x1)+"&x2="+String(x2)+"&x3="+String(x3);
    client.println("POST /arduino/index.php HTTP/1.1");
    client.print("Content-length:");
    client.println(parametri.length());
    Serial.println(parametri.length());
    Serial.println(parametri);
    client.println("Connection: Close");
    client.println("Host:192.168.1.200");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println();
    client.println(parametri);     
 } else {
   Serial.println("connection failed");
 }
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);  
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

index.php

<?php
echo 'running....';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $x1 = $_POST['x1'];
    $y1 = $_POST['x2'];
    $z1 = $_POST['x3'];
    echo 'x1: ' . $x1 . '<br>x2: ' . $x2 . '<br>x3:' . $x3 . '<br>';
}
?>

Serial Monitor

The Serial Monitor shows the PHP code using client.read() and as you can see it displays the data I sent to the PHP file, but in my server nothing shows up, only the running.... line shows up.

5
  • 2
    You are not connecting to the localhost. You are connecting to another device on the same network. From the perspective of the Arduino Yun, localhost would be the Arduino Yun again. – gre_gor Nov 5 '16 at 15:20
  • 1
    Your Arduino code works and your HTTP request is processed on the server. I don't know where and what are you expecting to show up on the server, but this now looks like it's not in the scope of Arduino and this SE site. – gre_gor Nov 6 '16 at 11:25
  • oh yeah my bad, i appreciate your help! thanks for replying – Ralph Nov 7 '16 at 15:03
  • 4
    I'm voting to close this abandoned question as off topic because the issue appears to be with misconceptions about networking rather than Arduino, and the precise issue to be resolved has never been stated accurately enough to achieve an answerable question. – Chris Stratton Jul 6 '17 at 3:45
  • 2
    I'm voting to close this abandoned question as off topic because the issue appears to be with misconceptions about networking rather than Arduino, and the precise issue to be resolved has never been stated accurately enough to achieve an answerable question. – Chris Stratton Jun 3 '18 at 12:13
1

There needs to be an empty line between HTTP headers and data.

You need to end the Content-Length header line with

client.println(data.length());

so that the client.println(); inserts an empty line.

1
  • It still did not solve my problem, I've updated my post please check it if you have time – Ralph Nov 6 '16 at 8:45

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.