Arduino with XBee WiFi

Hi!

I need help here. I am doing a project which Android send command to Arduino to light up an LED on pin 13. This is a wireless communication. The problem is the LED on pin13 keep on flashing automatically after I upload the code. It suppose to receive command, then only flash.

Here's my code for arduino:

#include <SPI.h> // needed for Arduino versions later than 0018
#include <WiFi.h>
#include <WiFiUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <SoftwareSerial.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90,0xA2,0xDA,0x0D,0x8B,0x8F};
IPAddress ip(192, 168, 1, 105);
IPAddress ip1(192, 168, 1, 1);
WiFiServer server(8032);
unsigned int localPort = 8032;
boolean incoming = 0;
// local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup() {
// start the Ethernet and UDP:
Udp.begin(localPort);
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop() {

WiFiClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply

//reads URL string from $ to first blank space

if(incoming && c == ' '){
incoming = 0;
}
if(c == '$'){
incoming = 1;
}

//Checks for the URL string $1 or $2
if(incoming == 1){
Serial.println(c);

if(c == 'a'){
Serial.println("ON");
digitalWrite(13, HIGH);
int rd=digitalRead(13);
delay(10);
server.write("ON");
delay(10);
Serial.println(rd);
}
if(c == 'b'){
Serial.println("OFF");
digitalWrite(13, LOW);
int rd=digitalRead(13);
delay(10);
server.write("OFF");
Serial.println(rd);
delay(10);
}

}

if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
client.stop();
}

// if there's data available, read a packet
int packetSize = Udp.parsePacket();

Serial.println(packetSize);
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote*, DEC);*

  • if (i < 3)*

  • {*

  • Serial.print(".");*

  • }*

  • }*

  • Serial.print(", port ");*

  • Serial.println(Udp.remotePort());*

  • // read the packet into packetBufffer*

  • Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);*

  • Serial.println("Contents:");*

  • Serial.println(packetBuffer);*

  • Serial.println(packetBuffer[0]);*

  • if(packetBuffer[0]=='1'){*

  • digitalWrite(13,HIGH);*

  • }else if(packetBuffer[0]=='13'){*

  • digitalWrite(13,LOW);*

  • }*

  • // send a reply, to the IP address and port that sent us the packet we received*

  • Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());*

  • Udp.write("WAHh BHurA");*

  • Udp.endPacket();*

  • }*

  • Udp.beginPacket(ip1,8032);*

  • Udp.write(ReplyBuffer);*

  • Udp.endPacket();*

  • delay(400);*

  • client.stop();*
    }

How to use this forum

#7

I don't have errors in my code. I just can't find why the LED on pin13 automatically flashing without receiving commmand from android phone.

rkosharil:
I don't have errors in my code. I just can't find why the LED on pin13 automatically flashing without receiving commmand from android phone.

If you're not going to follow simple directions, providing help seems like it wouldn't be effective.

Here's my code:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <WiFi.h>
#include <WiFiUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <SoftwareSerial.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90,0xA2,0xDA,0x0D,0x8B,0x8F};
IPAddress ip(192, 168, 1, 105);
IPAddress ip1(192, 168, 1, 1);
WiFiServer server(8032);
unsigned int localPort = 8032; 
boolean incoming = 0;
// local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Udp.begin(localPort);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {

WiFiClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
      
        //reads URL string from $ to first blank space
      
        if(incoming && c == ' '){
          incoming = 0;
        }
        if(c == '

){
          incoming = 1;
        }
     
        //Checks for the URL string $1 or $2
        if(incoming == 1){
          Serial.println(c);
       
          if(c == 'a'){
            Serial.println("ON");
            digitalWrite(13, HIGH);
            int rd=digitalRead(13);
            delay(10);
            server.write("ON");
            delay(10);
            Serial.println(rd);
          }
          if(c == 'b'){
            Serial.println("OFF");
            digitalWrite(13, LOW);
            int rd=digitalRead(13);
            delay(10);
            server.write("OFF");
            Serial.println(rd);
            delay(10);
          }
     
        }

if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);
    client.stop();
  }

// if there's data available, read a packet
  int packetSize = Udp.parsePacket();

Serial.println(packetSize);
if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

// read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    Serial.println(packetBuffer[0]);
 
    if(packetBuffer[0]=='1'){
      digitalWrite(13,HIGH);
    }else if(packetBuffer[0]=='13'){
      digitalWrite(13,LOW);
    }

// send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
    Udp.write("WAHh BHurA");
    Udp.endPacket();
  }
    Udp.beginPacket(ip1,8032);
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  delay(400);
  client.stop();
}