Arduino Ethernet/SD Shield Hanging

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

Grrr.....

First, arduwino, a huge thanks. It's pretty motivating to have someone peering over my shoulder, watching me push forward. That may not be your intent, but it's certainly happening. If you're ever in the DC area, let me know. The first round is on me, at the very least.

OK, my paycheck hits tomorrow, the Processing book is going to be coming home (either on electron or paper).

I'm starting to look further under the hood, and I can't understand something in Udp.h (or any of the other files).
After getting a little bit of sleep, I want to look at UDP again (If this is my ADD acting up again, it's incredibly insidious). UDP is going to be key for another project that I want to work with, and is the pathway I eventually want to reach with this current project.

It looks like commands are duplicated. Further, it looks like the commands in it should let me do what I want to do.

Code: Select all

#ifndef udp_h
#define udp_h

#define UDP_TX_PACKET_MAX_SIZE 24

class UdpClass {
private:
  uint8_t _sock;  // socket ID for Wiz5100
  uint16_t _port; // local port to listen on

public:
  void begin(uint16_t);				// initialize, start listening on specified port
  int available();								// has data been received?

  // C-style buffer-oriented functions
  uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 
  uint16_t sendPacket(const char[], uint8_t *, uint16_t);  //send a string as a packet to specified peer
  int readPacket(uint8_t *, uint16_t);		// read a received packet 
  int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 	
  // readPacket that fills a character string buffer
  int readPacket(char *, uint16_t, uint8_t *, uint16_t &);

};

extern UdpClass Udp;

#endif

Take a look at the "uint16_t sendPacket" lines (there's two of 'em).
It looks like one forces me to send a const char[]. The one above it seems duplicated except for the information it passes to the method(?), BUT it seems like the information it is passing to the method, it passes twice.

First, why the two, almost identical, lines?
Second, can I force the issue somehow?

Finally, I'm poor. Like "Not making the rent" poor.
At what point am I going to brick one of these things? If I comment out one of the lines of code in the program above, do I risk this?
I'm not pushing liability on anyone, or shirking my responsibility. I'm the one hacking away at this thing. I'm just hoping someone can tell me the relative risk here. I know I'm probably more likely to brick my gear screwing up my anti-static discipline when I unplug the Arduino.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

I can't say that the "Processing" book is really helping out.

How in the world can you dynamically create a UDP datagram to send from the ethernet shield?
Ideally I'd like to be able send a UDP message to the Arduino+Shield, and have the Arduino+Shield check a pin status (or change a pin status) and send a UDP message with the results.

It looks like you can only send a fixed message through UDP on the ethernet shield. If I try to change it within the program I get errors (I'm getting all kinds of errors, but most common is const char errors).
Why can't I send the contents of a string variable?

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Ethernet/SD Shield Hanging

Post by adafruit_support_bill »

First, why the two, almost identical, lines?
Second, can I force the issue somehow?
There are two versions of the same function. The first takes a pointer to a byte (usually the first byte of a string or a structure), the second takes a character array. No need to force the issue, just use the one that fits the type of data you have.
At what point am I going to brick one of these things? If I comment out one of the lines of code in the program above, do I risk this?
You are still in safe territory here. Nothing that can't be undone.
It looks like you can only send a fixed message through UDP on the ethernet shield. If I try to change it within the program I get errors (I'm getting all kinds of errors, but most common is const char errors).
If you post your code it would help. But const char errors are probably because you declared your packet data as "const char packetData[]". Drop the const and you should be fine.
Why can't I send the contents of a string variable?
Have you looked at the UDPSendReceive example in the IDE? It does a sendPacket on a string. (Actually, an array of characters).

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

Thanks a bundle.

I've been working on this thing for 6 hours now. Lots of progress, still exposing more gaps in my knowledge, but:
UDP Sending is WORKING!

I'm learning the bizarre intricacies of Arduino string slicing and comparisons.

Thank you so much. I'll keep posting updates here, as I keep making progress.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

OK, I keep coming back to this problem. Here's a code snippet:

Code: Select all

    if (Received.substring(0,3) == "Get") {  //Starts finding the object of the "Get" command from the UDP datagram
    
      char ReplyBuffer[] = "Getting Info";
      Serial.println(initial);
      Serial.println("Get called");
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      if (Received.indexOf("All") != -1)
      {
        for (int i = 0; i < 10; i++)
        {
          val = digitalRead(i);
          valc = char(val);
          PinState = "Digital Pin " + i;
          PinState = PinState + " = ";
          PinState = PinState + valc;
         //char ReplyBuffer[] = PinState; 
          Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
        }
      }
          
      Serial.println(Object);
    }
    else if (Received.substring(0,3) == "Set") {
    
      Serial.println(initial);
      Serial.println("Set called");
      char ReplyBuffer[] = "Setting Condition";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    else
I keep coming back to this same issue.
In the above example, the first Udp.sendPacket is trying to send the contents of the variable "PinState", which is a constructed string. It can't do it. I get an error "initializer fails to determine size of 'ReplyBuffer'"
I've tried using String methods to get the length of "PinState". I'm not sure I'm putting it in the right place (The brackets?). Not working.

The second Udp.SendPacket example works, because it is sending a fixed string.

I was having this earlier, and thought I got around it. I'm still not there.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Ethernet/SD Shield Hanging

Post by adafruit_support_bill »

Post the entire sketch, so we can reproduce the compiler errors.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

My apologies. I wanted to have experts focusing on the area of concern.
Thank you again for taking the time to look at this.

Code: Select all

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: [email protected] 12/30/2008


// 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, 0x00, 0x21, 0xE3};
byte ip[] = {192,168,1,9};
int i, val;
char PinStateLen[] = ("");
char initial[] = {""};
char Obj[] = ("");
//char valc[] = ("");
String Received, Get, Set, Command, Object, Specifier, PinState, valc;




unsigned int localPort = 8888;
// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// 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

//digital pin setup
//pinMode(2, INPUT);
//pinMode(3, INPUT);
//pinMode(4, INPUT);
//pinMode(5, INPUT);
//pinMode(6, INPUT);
//pinMode(7, INPUT);
//pinMode(8, INPUT);
//pinMode(9, INPUT);

void setup()
{
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  Get = String("Get");
  Set = String("Set");
}

void loop() 
{
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;  // subtract the 8 byte header
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
  
    Udp.readPacket(packetBuffer, UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.println("Contents;");
    Serial.println(packetBuffer);
    Received = packetBuffer;
    Serial.println(Received);
    
    if (Received.substring(0,3) == "Get") {  //Starts finding the object of the "Get" command from the UDP datagram
    
      char ReplyBuffer[] = "Getting Info";
      Serial.println(initial);
      Serial.println("Get called");
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      if (Received.indexOf("All") != -1)
      {
        for (int i = 0; i < 10; i++)
        {
          val = digitalRead(i);
          valc = char(val);
          PinState = "Digital Pin " + i;
          PinState = PinState + " = ";
          PinState = PinState + valc;
          char ReplyBuffer[] = PinState;
          Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
        }
      }
          
      Serial.println(Object);
    }
    else if (Received.substring(0,3) == "Set") {
    
      Serial.println(initial);
      Serial.println("Set called");
      char ReplyBuffer[] = "Setting Condition";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    else
    {
      Serial.println("Command Capture Start");
      Serial.println(initial);
      Serial.println("Command Capture Failed");
      Serial.println("Command Capture End");
      char ReplyBuffer[] = "Command Capture Failed";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    
    //Serial.println(initial);
    //char ReplyBuffer[] = initial;
    Udp.sendPacket(initial, remoteIp, remotePort);
    char ReplyBuffer[] = "Complete";
    Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    delay(10);
}

/*
    switch(initial[0]) {
    case 'G':
      char ReplyBuffer[] = "Getting Info";
      Serial.println(initial);
      Serial.println("Get called");
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      break;
    case 'S':
      Serial.println(initial);
      Serial.println("Set called");
      char ReplyBuffer[] = "Setting Condition";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      break;
    default:
      Serial.println("Command Capture Failed");
      char ReplyBuffer[] = "Command Capture Failed");
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    Udp.sendPacket(initial, remoteIp, remotePort);
    char ReplyBuffer[] = "Complete";
    Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    }
    delay(10);
}
*/    
      
  

  
  

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Ethernet/SD Shield Hanging

Post by adafruit_support_bill »

The problem is that the compiler doesn't know how much space to allocate for ReplyBuffer[] because the initializer is not constant.
Try this:

Code: Select all

        for (int i = 0; i < 10; i++)
        {
          val = digitalRead(i);
          valc = char(val);
          PinState = "Digital Pin " + i;
          PinState = PinState + " = ";
          PinState = PinState + valc;
          Udp.sendPacket(&PinState[0], remoteIp, remotePort);
        }
This uses the other version of sendPacket that expects a byte pointer.
"&PinState[0]" means "the address of the first character of PinState" - which is equivalent to a byte pointer to the first byte of what you want to send.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

Your suggestions gave me output that I didn't expect at all (I was getting reducing string readouts, like the string was being sliced by one character every iteration, through the ten iterations. BUT I wasn't getting any pin readouts).
I tried a couple of alternate approaches, and suddenly everything came together. Completely. Beautifully. I don't know if you intended for me to find my own solution with your pointer (no pun intended), but I really appreciate it. I'm really not good at asking for help, and would rather find my own way. Your suggestion pointed me in a direction that I wouldn't have traveled, at all. Hell, looking at the Arduino reference for the "&" sign doesn't yield much information at all, besides saying "It's an incredibly complex topic".
http://pw1.netcom.com/~tjensen/ptr/pointers.htm

I'm going to work on reading this between coding sprees and naps (I've got mono or something. It's horrible. Just takes your energy completely away). I've never seen anything about "pointers" before, so I've got some learning to do.

Arduwino, thanks again. I'll keep updating.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

Wow. What a difference that code snippet made.

Code: Select all

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: [email protected] 12/30/2008


// 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, 0x00, 0x21, 0xE3};
byte ip[] = {192,168,1,9};
int i, val, TypIndex, pin, pwm;
char PinStateLen[] = ("");
char initial[] = {""};
char Obj[] = ("");
//char valc[] = ("");
String Received, Get, Set, Command, Object, Specifier, PinState, valc, PinRead;




unsigned int localPort = 8888;
// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// 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



void setup()
{
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  //digital pin setup
  //pinMode(2, INPUT);
  //pinMode(3, INPUT);
  //pinMode(4, INPUT);
  pinMode(5, OUTPUT);
  //pinMode(6, INPUT);
  //pinMode(7, INPUT);
  //pinMode(8, INPUT);
  //pinMode(9, INPUT);
}

void loop() 
{
  char ReplyBuffer[]= "                                    ";  //Clear the buffers
  char packetBuffer[] = "                                     ";
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;  // subtract the 8 byte header
    //Serial.print("Received packet of size ");
    //Serial.println(packetSize);
  
    Udp.readPacket(packetBuffer, UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.flush();
    Serial.println("Contents;");
    //Serial.println(packetBuffer);
    Received = packetBuffer;
    //Serial.println(Received);
    
    if (Received.substring(0,3) == "Get") {  //Starts finding the object of the "Get" command from the UDP datagram
    
      //char ReplyBuffer[] = "Getting Info";
      //Serial.println("Getting Info");
      Serial.println("Get called");
      //Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      if (Received.indexOf("All") != -1)
      {
        for (int i = 2; i < 10; i++)
        {
          val = digitalRead(i);
          valc = char(val);
          PinRead = int(i);
          PinState = "Digital Pin" + PinRead;
          PinState = PinState + " = ";
          PinState = PinState + val;
          Udp.sendPacket(&PinState[0], remoteIp, remotePort);
          //Serial.flush();
        }
        for (int i = 0; i < 6; i++)
        {
          val = analogRead(i);
          PinRead = int(i);
          PinState = "Analog Pin" + PinRead;
          PinState = PinState + " = ";
          PinState = PinState + val;
          Udp.sendPacket(&PinState[0], remoteIp, remotePort);
          //Serial.flush();
        }
      }
      else if (Received.indexOf("Digital") != -1)
      {
      TypIndex = Received.indexOf("Digital");
      Serial.print("Digital Pin ");
      char PinRead = char(Received[TypIndex + 8]);
      Serial.print(PinRead);
      Serial.println(" requested");
      pin = (PinRead - '0');
      val = digitalRead(pin);
      Serial.print("Value of digital pin ");
      Serial.print(pin);
      Serial.print(" = ");
      Serial.println(val);
      PinState = "Digital Pin" + String(pin);
      PinState = PinState + " = ";
      PinState = PinState + String(val);
      Udp.sendPacket(&PinState[0], remoteIp, remotePort);
      Serial.flush();
      }
      else if (Received.indexOf("Analog") != -1)
      {
        TypIndex = Received.indexOf("Analog");
        Serial.println(Received);
        Serial.println(Received[TypIndex + 7]);
        Serial.print("Analog Pin ");
        char PinRead = char(Received[TypIndex + 7]);
        Serial.print(PinRead);
        Serial.println(" requested");
        pin = (PinRead - '0');
        val = analogRead(pin);
        Serial.print("Value of analog pin ");
        Serial.print(pin);
        Serial.print(" = ");
        Serial.println(val);
        PinState = "Analog Pin" + String(pin);
        PinState = PinState + " = ";
        PinState = PinState + String(val);
        Udp.sendPacket(&PinState[0], remoteIp, remotePort);
        Serial.flush();
      }
          
      //Serial.println(Object);
    }
    else if (Received.substring(0,3) == "Set") {   //The Set command branch of the Arduino
    
      Serial.println(Received);
      Serial.println("Set called");
      char ReplyBuffer[] = "Setting Condition";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      Serial.flush();
      if (Received.indexOf("Digital") != -1)
      {
        TypIndex = Received.indexOf("Digital");
        Serial.print("Digital Pin ");
        char PinRead = char(Received[TypIndex + 8]);
        Serial.print(PinRead);
        Serial.print(" is being set to ");
        pin = (PinRead - '0');
        if (Received.indexOf("High") != -1)
        {
          Serial.println("High");
          digitalWrite(pin, HIGH);
        }
        else if (Received.indexOf("Low") != -1)
        {
          Serial.println("Low");
          digitalWrite(pin, LOW);
        }
        else if (Received.indexOf("Input") != -1)
        {
          Serial.println("Input");
          pinMode(pin, INPUT);
        }
        else if (Received.indexOf("Output") != -1)
        {
          Serial.println("Output");
          pinMode(pin, OUTPUT);
        }
        else
        {
          Serial.println("Command Failure");
          char ReplyBuffer[] = "Command Failed";
          Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
        }
      }
      else if (Received.indexOf("Analog") != -1)
      {
        Serial.print("PWM on Pin ");
        TypIndex = Received.indexOf("Analog");
        char PinRead = char(Received[TypIndex + 7]);
        Serial.print(PinRead);
        Serial.print(" is being set to ");
        pin = int(PinRead - '0');
        //pwm = int(Received[10,14]);
        pwm = 0;
        int pinSet = 0;
        //char PinState = char(Received[TypIndex + 8])
        //pin = (PinRead - '0');
        char pinState = char(Received[TypIndex + 9]);
        pinSet = (pinState - '0');
        pwm = pwm + (1000*(int(pinSet)));
        char PinState = char(Received[TypIndex + 10]);
        pinSet = (PinState - '0');
        pwm = pwm + (100*(int(pinSet)));
        PinState = char(Received[TypIndex + 11]);
        pinSet = (PinState - '0');
        pwm = pwm + (10*(int(pinSet)));
        PinState = char(Received[TypIndex + 12]);
        pinSet = (PinState - '0');
        pwm = pwm + (1*(int(pinSet)));
        Serial.println(pwm);
        Serial.println(pin);
        analogWrite(pin, pwm);
      }
      
        
          
    }
    else
    {
      Serial.println("Command Capture Start");
      Serial.println(initial);
      Serial.println("Command Capture Failed");
      Serial.println("Command Capture End");
      char ReplyBuffer[] = "Command Capture Failed";
      Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
      Serial.flush();
    }
    
    //Serial.println(initial);
    //char ReplyBuffer[] = initial;
    Udp.sendPacket(initial, remoteIp, remotePort);
    Serial.flush();
    char ReplyBuffer[] = "Complete";
    Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    Serial.flush();
    }
    delay(10);
    Received = "                       ";
    char PacketBuffer[] = "                     ";
}



I finished a large chunk of the Arduino code today for the board.
The board will accept a UDP packet, either in the form of a command or an information request.
The board will accept "Set Commands", allowing you to set pins as Digital Input or Output. If the pin is set as a Digital Output pin, then you can set it as a digital high or low.
The board will also accept "Set Commands" allowing you to set an Analog PWM level.
The board will also let you get pin states (Either one at a time, or all of them).

Anything that can send out a UDP datagram should be able to make use of the information. Now I just need to set up the datalogging side of things on my home computer. Then I need to see if I can send UDP messages on my home network, from a remote location.

Huge progress today. I'm exhausted. Thank you so much for the critical help.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

Yesterday destroyed me.
Wow. Don't code when you're sick or something. Maybe it's best if you leave it to the professionals or something....

I moved to a separate segment of the network today, working across a different network device.
Holy BANNED, what a difference that made. A scary difference.
Yesterday, commands were getting from my laptop to the Arduino with no problem. Hundreds of tests, no hiccups.
This morning I was lucky to get two commands across before the Arduino stopped responding (or stopped getting packets).

I started playing with things, wondering how febrile I was last night when I finished coding. A nice long sit with a cup of coffee, and I decided to go back downstairs, and replicate everything exactly.

I plugged the board into the network, and everything worked. I didn't even have to roll the code back.

So, a D-Link DIR-855 seems to get angry about repeatedly routing UDP packets. My Verizon Fios router doesn't seem to have the same issues.
Others who are working with Arduino Shields, keep an eye out for that issue. Some routers play with UDP packets less nicely than others.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Ethernet/SD Shield Hanging

Post by adafruit_support_bill »

Some routers play with UDP packets less nicely than others.
Broadcast packets in particular are not welcome on the internet at-large and are generally not passed through routers. If you do know the destination IP you are better off using unicast.
Also, if your router has a built-in firewall, you may need to configure it to allow UDP on certain ports.

User avatar
nrg will
 
Posts: 11
Joined: Fri Oct 01, 2010 4:59 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by nrg will »

I saw the CODE Snippet you gave the author of this Post. I am using the UDPsendreceivestring in a sketch on my Arduino Mega (from Adafruit!) and the EthershieldSD...

Problem#1
I have UDP tool on my Iphone and succesfully SENT a message to the Arduino set up... but I could not use the Serial Monitor to SEND a message back to the Iphone. (all working on my LAN by WiFi tether)

Problem #2
The next test message I sent FROM the Iphone was shorter than the first message, and it left on the letters from the first message??? TEST was the first message and JOY was the second message, so it displayed on the Serial Monitor as JOYT...

I tested it several more times and it always did the same thing, I could correctly send longer messages, but when I went to a shorter message, it always left on the extra letters from the previous message!

Would your CODE snippet fix those two issues and where do I place it inthe Sketch... I tried a couple places I thought it should go, but it didn't compile- no joy!

STANDARD SKETCH:
[Edit - moderator - use code block]

Code: Select all

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender
 
 A Processing sketch is included at the end of file that can be used to send 
 and received messages for testing with a computer.
 
 created 21 Aug 2010
 by Michael Margolis
 
 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: [email protected] 12/30/2008


// 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, 0x00, 0x1d, 0xb8 };
byte ip[] = { 10, 10 , 10 , 122 };

unsigned int localPort = 8888;      // local port to listen on

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// 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


void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Received packet of size ");
    Serial.println(packetSize);

    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);
  }
  delay(10);
}

Code: Select all

/*
  Processing sketch to run with this example
 =====================================================
 
 // Processing UDP example to send and receive string data from Arduino 
 // press any key to send the "Hello Arduino" message
 
 
 import hypermedia.net.*;
 
 UDP udp;  // define the UDP object
 
 
 void setup() {
 udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
 //udp.log( true ); 		// <-- printout the connection activity
 udp.listen( true );           // and wait for incoming message  
 }
 
 void draw()
 {
 }
 
 void keyPressed() {
 String ip       = "192.168.1.177";	// the remote IP address
 int port        = 8888;		// the destination port
 
 udp.send("Hello World", ip, port );   // the message to send
 
 }
 
 void receive( byte[] data ) { 			// <-- default handler
 //void receive( byte[] data, String ip, int port ) {	// <-- extended handler
 
 for(int i=0; i < data.length; i++) 
 print(char(data[i]));  
 println();   
 }
 */

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Ethernet/SD Shield Hanging

Post by adafruit_support_bill »

@NRG WILL - Please use the 'code' button when submitting lengthy blocks of code.

Problem #1 - I don't see anything in your code that reads from the serial monitor.

Problem #2 sounds like a simple 'dirty buffer' problem. Either clear out the old buffer before re-use, or be sure to append a null character to the end of any strings you send.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Arduino Ethernet/SD Shield Hanging

Post by static »

I had this problem as well.
You need to flush the buffers each time.
I'm at work right now, or I'd give you the sample code from my project. It's there, because I remember having this problem.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”