WiFi UDP Send and Receive String Library Bug (need guidance)

(you haven't provided your code)

My code is to large. Every time I try to post it, I get a script error message that freezes my computer and forces me to re-start the computer to get the script error message of my computer. I spent all morning trying to chop the code up into smaller pieces that still allows it to work, but even that is to large to post.

You probably need to set a flag for your button push such that the client loop is not reenterd until the button is released and then pushed again. Your current code is probably creating a spew of client request upon the button press.

This is the code for the Arduino2. It is much smaller that the Arduino1 code and I hope it will fit. Does anyone have any examples of using flags that I could use as a learning reference to understand how flags work?

/*
  Web client/Server for Arduino2 
 
 This sketch is a evolutionary experiment using the logic behind the code
 Generously provided by Supporters of the arduino Form. 
 Without forum supporters Zoomkat and SufferTim
 This code would not have been possible for me to comprehend
 
 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 Pin 2 connected to button
 Pin 3 and 4 connected to leds
 created based on the sketch
 zoomkat 10-02-14, combined client and server
 modified for client server direct handshaking
 
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,1,174);
EthernetServer server(80);
EthernetClient client;
byte Arduino1Name[] = { 192, 168, 1, 177 };

String readString; //used by server to capture GET request 
boolean RunForTheHills = 0;
boolean OnlyOnce = 0;
boolean Internet = 0;
boolean RepoMan = 0;
boolean ApplePie = 0;

void setup() {

  Serial.begin(9600);      // initialize serial communication
  pinMode(2,INPUT);   // button 1 (led on if pressed)
  pinMode(4,OUTPUT);  // led 2
  pinMode(5, OUTPUT); // led 3
  
  Ethernet.begin(mac, ip);
  delay(1000); // give the Ethernet shield a second to initialize:
  Serial.println(ip);
  Serial.println("connecting...");

  server.begin();                           // start the web server on port 80
  }


void loop() 
  {
  /*========================= Button 1 Pressed =================== 
    =========== If overide from Arduino 1 not valid ============== 
    ====================== Led 2 comes on ================== 
  */
  if (digitalRead(2) == HIGH)
  { RunForTheHills = HIGH; }
 

   /*==================== Button 1 not Pressed =================== 
    ========================== Led2 is not on ==================== 
  */      
   if (digitalRead(2) == LOW)
   {  RunForTheHills = LOW; }
   
   //---------------------------------------------------------------------

  if (RunForTheHills == HIGH) {
      if (ApplePie == LOW)
      {  Serial.println("Arduino 2 button 2 pressed");
         
       if (RepoMan == LOW)
       { digitalWrite(4,HIGH); 
         Serial.println("Arduino 2 led 2 on");
       } else { Serial.println("Arduino2 is still in override mode");
                Serial.println("led 2 will not turn on");
               }
               ApplePie = HIGH;
      }
   }
       
       if (RunForTheHills == LOW)
       { digitalWrite(4,LOW);
         ApplePie = LOW;
       }
  
    if (RepoMan == HIGH)
    { if (OnlyOnce == LOW)
      { digitalWrite(5, HIGH);
        { Serial.println("A1 button activated");
          Serial.println("led 3 (pin4) should come on");
          Serial.println("led 2 (pin3) should not come on if button pressed on A2");
          Serial.println("---------------------------");
          if ( client.connect(Arduino1Name, 80) ) 
          {  OnlyOnce = HIGH;
             Internet = HIGH;
             Serial.println("");
             Serial.println("connected");
          // Make a HTTP request:
             client.println("GET /OR"); // OR is an acronym for (OverRide Received)
          client.println("Host: Arduino2"); 
          client.println("Connection: close");
          client.println();
          // client.stop();
          } else { // if you didn't get a connection to the server:
                   Serial.println("connection failed");
                   Serial.println("retrying");
                 }  
        }
      }
    }
    
     if (RepoMan == LOW)
     { if (OnlyOnce == HIGH)
       { digitalWrite(5, LOW);
         Serial.println("A1 button 2 activated");
         Serial.println("led 3 (pin4) should go off");
         Serial.println("led 2 (pin3) should come on if button pressed on A2");
         Serial.println("---------------------------");
         if ( client.connect(Arduino1Name, 80) ) 
         { Internet = HIGH;
           OnlyOnce = LOW;Serial.println("");
           Serial.println("connected"); // Make a HTTP request:
           client.println("GET /OD"); //OD is an acronym for (OverRide Disabled)
           client.println("Host: Arduino2");
           client.println("Connection: close");
           client.println();
         } else { // if you didn't get a connection to the server:
                  Serial.println("connection failed");
                  Serial.println("Retrying");
                }  
       }
     }
    
    
      while (client.available()) 
      { char c = client.read();
        Serial.write(c);
       // if (c == '\r') 
        //{ client.stop(); }
      }

     if (Internet == HIGH)
     { // if the server's disconnected, stop the client:
       Internet = LOW;
       Serial.println("A2 disconnecting from A1");
       client.stop();
     }
  
   
  EthernetClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            if (RepoMan == HIGH)
            { client.print("Arduino1 has disabled Arduino2 button");
            } else { client.print( "Arduino2 button is active");
                   }
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        if (currentLine.endsWith("GET /OvR")) // GET /OvR (OverRide) turns LED3 on and disabled LED2
        { RepoMan = HIGH;
        }
        if (currentLine.endsWith("GET /OvD")) // GET /OvD (OverRide-Disabled) turns LED3 off and enables use of LED2
        { RepoMan = LOW;
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Click Reply, then Attach the file with "Attachments and other options"

Hi,

Can any body help me out regarding receiving data through WiFi Shield.I tried many sample codes but could not able to receive the data.Below is my sample code Which I tried.

void UdpReceive()
{
int packetSize ;
// udp.begin(localport);
Serial.println(localport);
// packetSize = udp.parsePacket();

packetSize = udp.parsePacket();

Serial.println(packetSize);
// Serial.println(udp.remotePort());
if (!packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(udp.remotePort());

// read the packet into packetBufffer
int len = udp.read(packetBuffer, 10);
if (len > 0) packetBuffer[len] = '\0';
Serial.println("Contents:");
Serial.println(packetBuffer);

}
}

Or

if(udp.available())
{
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);*
    }
    I tried both,but no luck.I am able to send the data could not able to get it.
    Thanks in Advance.

I figured this out a few months ago. I've started saving files that I got working, but when I was just starting out when I ran into this problem, I wasn't doing that.
Tell you what, I'll return the favor that the forum gave to me. Put your code in the code brackets, and make sure you put all of it so I can compile it. (including what comes before void loop() and i'll get it working for you.

Quick question though. Did you upgrade the Wi Fi firmware? That took me four days to figure out how to do it properly. No WiFi code will work without that upgrade.