Problem with UDP packets

Hello people

I have a problem with programming UDP packets.

I still don’t understand how you actually program more than one UDP packet. Could you please explain to me step by step how to do this?

Basically, I want to achieve this:
Step 1: When digital pins 22 and 40 are high, then the Arduino should send a UDP packet which contains G8-1.
Step 2: When digital pins 23 and 42 are high, then the Arduin should send a UDP packet G8-2

These two steps are the steps that I should repeat 96 times but with different UDP packets.
Could you tell me how to program this?

Many thanks in advance!

Leon

Are you able to send a UDP packet? I assume you are working from examples which show how to do that. Is it working?

void loop()
    {
    if (digitalRead(22) && digitalRead(40))
        sendPacket("G8-1");

    if (digitalRead(23) && digitalRead(42))
        sendPacket("G8-2");
    }

void sendPacket(char *contents)
    {
    Udp.beginPacket(destinationIP, destinationPort);
    Udp.write(contents);
    Udp.endPacket();
    }

Hello John & Peter

Thank you for your comments!
I'm already a step further.

But I have still a problem with the programming code.

This was the testing code

void loop()
{
   if (digitalRead(22) && digitalRead(23))
        sendPacket("g1");

   if (digitalRead(24) && digitalRead(25))
        sendPacket("g2");
}

void sendPacket(char *contents)
    {
    Udp.beginPacket(192.168.1.1, 8080);
    Udp.write(contents);
    Udp.endPacket();
    }

The software give the errors you can see below:
"As of Arduino 1.0, the Udp class in the Ethernet library has been renamed to EthernetClient."

"sketch_aug30a.cpp:15:21: error: too many decimal points in number"
"sketch_aug30a.cpp: In function 'void sendPacket(char*)':"
"sketch_aug30a:11: error: 'Udp' was not declared in this scope"

I don't know what the problem is, maybe you can help me.

Thanks

Leon

There is no such thing as an IP Address Constant. You have to use a function to format the four parts of the address as a single 32-bit number before you can pass it to a function. See any of the Ethernet examples for examples.

How do you do this?
I don't know how to do it.

Can you show it how to do it with a code?

Lustige_Leon:
Can you show it how to do it with a code?

Have you looked at any of the examples? I'm not going to simply write your sketch for you, and you don't seem to be making any effort to figure this out for yourself. Surely, you have seen an example which sends a UDP packet?

Hello Peter

I know what you mean but for me it's very hard to find out how it work.
It's the first time I'm doing such thing with the arduino.
I know nothing about programming language(c++) and network on the arduino.

I've worked with it a few years ago at school but it was just a few simple steps.

And now I'm working on a project with it and I have not so much time create it.
And you guys know what the problem is and It's very easy for you because you have a lot of experience with it.

For you guys it take just a few seconds to show it to me, and for me it will take a lot of tile to find out.

That's the reason why I'm asking these (simple) things to yours.

I hope you want to help me further.

Thanks a lot

Leon

What hardware are you using. Xbee, Wifi, Ethernet?

Mark

See any of the Ethernet examples for examples.

Can you show it how to do it with a code?

Have you looked at any of the examples?

For you guys it take just a few seconds to show it to me, and for me it will take a lot of tile to find out.

Code is in the examples. There is an example in there called 'UDPSendReceiveString' probably will be a big help.

If you can't understand the examples, read something like this: http://www.cplusplus.com/doc/tutorial/

An Arduino Ethernet shield rev.3

I've tried this but it still gives errors. The code i've used you can see below

The errors:

Test_Chamsys_Ethernet.cpp:23:54: error: too many decimal points in number
Test_Chamsys_Ethernet.cpp:43:34: error: too many decimal points in number
Test_Chamsys_Ethernet.cpp: In function 'void sendPacket(char*)':
Test_Chamsys_Ethernet:38: error: no matching function for call to 'EthernetUDP::remotePort(int)'
/Applications/Arduino.app/Contents/Resources/Java/libraries/Ethernet/EthernetUdp.h:96: note: candidates are: virtual uint16_t EthernetUDP::remotePort()

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 1);

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

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

void setup()
{
  
  Ethernet.begin(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED, 192.168.1.1);                        //Start the Ethernet and UDP
  Udp.begin(8080);
  
  pinMode(22, INPUT);                            // Sets the digital pin as input
  pinMode(23, INPUT);                            // Sets the digital pin as input
  pinMode(24, INPUT);                            // Sets the digital pin as input
  pinMode(25, INPUT);                            // Sets the digital pin as input
}

void loop()
{
   if (digitalRead(22) && digitalRead(23))
        sendPacket("g1");

   if (digitalRead(24) && digitalRead(25))
        sendPacket("g2");
}

void sendPacket(char *contents)
{
    Udp.beginPacket(Udp.remoteIP(192.168.1.10), Udp.remotePort(8080));
    Udp.write(contents);
    Udp.endPacket();
}

Thanks

Leon

    Udp.beginPacket(Udp.remoteIP(192.168.1.10), Udp.remotePort(8080));

UDP.remoteIP() returns the current remote address, it doesn't take a parameter. The same is true of UDP.remotePort(). See the example at Ethernet - Arduino Reference

Thank you!

But now he gives only this error

Test.cpp:23:54: error: too many decimal points in number

Whats the problem now?

In 'C', literal values can be characters, or strings, or decimal numbers, or floating point numbers.

192.168.1.10 is not any of those.

An IPv4 address is a 32-bit number which is often expressed as a string in dotted decimal format (192.168.1.1). But this is not a format that your compiler will understand.

You need to provide the address in the format needed by the API of the library you're using. It looks to me as if the library is expecting to get a long int, but you would need to look at the API to know for sure. Anyway, you don't need to figure it out and you don't need me to figure it out. Just look at the examples for the UDP library and they will show you how to use it.

And no, I'm not writing the code for you, no matter how hard it is for you and how easy for me. If I write it for you, it achieves nothing. Look at the examples; they show you what to do. If you can't follow the examples, put the problem down and go do something easier. Nobody expects you to tackle the hardest problems right away, and if you aren't ready yet then get some more practice in and then come back to the problem.

Lustige_Leon:
Thank you!

But now he gives only this error

Test.cpp:23:54: error: too many decimal points in number

Whats the problem now?

Post the new version of your code, so we can see.