send sensor data from arduino to android via wifi

Hi guys, i want to create an android app that can receive data of a sensor that i've already connected with Arduino Yun. Can you help me to write a code that can send data to android via wifi because i tried to do this but it doesnt work .... :frowning: :frowning: :frowning: :frowning:
I wrote something but it's not correct.

#include <SPI.h>
#include <WiFi.h>

WiFiServer server (80);
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  server.begin();
}

// the loop routine runs over and over again forever:
void loop() {
  WiFiClient client = server.available();
  while (client.connected()) {
      if (client.available()) {
        char c = client.read();
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  client.print(sensorValue);
  delay(1000);  }      // delay in between reads for stability
}
}

Maybe the android app has the error? How is it receiving the data? As a tcp socket or else?

DirtySound:

#include <WiFi.h>

Nope, not going to work. That library is for a WiFi Shield. The WiFi Shield uses a networking module that is controlled directly by the Arduino processor. Any examples you see that include WiFi.h or Ethernet.h will not work on a Yun.

Networking on the Yun is completely different, and none of the plain Arduino networking libraries will work. On the Yun, the networking is controlled by the Linux processor, which gives it much more powerful capabilities. The true power is unleashed when you program the networking logic on the Linux side. But you can still do the same sort of control that the WiFi library provides and control things from the sketch, but you must use the Bridge Library to do it.

The closest official example to what you want is the Bridge Example. but this may be doing more than what you want. As the example is currently written, it implements a RESTful API which is accessed using web calls. It sounds like might just want to set up a socket server that waits for an incoming connection? You can do that by including a port number when setting up the YunServer, and then calling server.noListenOnLocalHost() rather than server.listenOnLocalHost(). I have an example, but I can't find it at the moment (forum search is giving problems.) I'll be back later with the example, when I get back to my computer (a tablet is handy, but not nearly as powerful as a real computer.)

Some examples of using a TCP socket on a Yun: