Output control via UDP

Hello all

I have a problem writing a received udp packet to an output. Here is a part of my sketch:

void udpSerialPrint(word port, byte ip[], const char *data, word len) {
IPAddress src(ip[0], ip[1], ip[2], ip[3]);

Serial.println(src);
Serial.println(port);
Serial.println(data);

}

this is part of an example that came with the enc28j60 library. My problem now is, I want to write the data to control the state of an output.
The data I send via udp is "HIGH" and in the serial monitor it writes it as it´s supposed to. But if I want to use digitalWtite(outputPin, data) I get an error message that "data" was not declared in this scope.
Somebody knows what I am doing wrong? Do I need to write the data in a Buffer? If yes how can I do that?
I´ve attached the sketch I wrote so far as well for better understanding.

Thank´s in advance already and greetings

Stefan

UDP_outputcontrol.ino (1.29 KB)

not experienced with this but here is my guess...

http://www.cplusplus.com/doc/tutorial/pointers/

The problem might be that const char *data is a pointer to a character type variable,

according to that link, you might need to do something like this:

//declare global variable

char pinState;


// in setup()

void udpSerialPrint(word port, byte ip[], const char *data, word len) {
  IPAddress src(ip[0], ip[1], ip[2], ip[3]);
  data = &pinState;
  Serial.println(src);
  Serial.println(port);
  Serial.println(data);  
}


// in loop
digitalWrite(outputPin, pinState);

but, I'm only basing my advice on that link....

All right I give it a try thank´s a lot

Yes it is compiling now I thank you again for the quick reply and also for the link it may help me in the future with some other problems.

Greetings Stefan

Hi guys

I am trying to control outputs from the Arduino via udp and only udp. My problem is probably quite trivial but I spent the whole night already finding a solution and just don´t get there.
I started out with a example sketch from the ethernet Master library. Now what I would like to do is if I receive a message for example on port 7050 I would like to change the actual state of an output (off >> on >> off>>on and so on). And if I send a message on port 7051 change the state of another output.
Does anybody have an idea how I can realize that? I´ve done it already with an ethernetshield W5100, but I just can´t get it running with the enc28j60 chip.
I am happy for any ideas. As I said the sketch is attached.

Thanks in advance and greetings

Stefan

ENC28J60_UDP_Outputs.ino (1.53 KB)

Out of curiousity, did the advice I gave you yesterday about the pointers work?

No it just compiled but if I wanted to read out the udp message I don´t get anything so I need to find a different solution. But the link you gave me is great I already studied half of the tutorial.

Greetings Stefan

Yes it is compiling now

That doesn't mean that it works. Reassigning the pointer data, to point to somewhere else makes no sense.

You need to show where you tried to use data, and what your serial output looks like.

Hi Paul

You are right it is not working. The problem is that it doesn´t write the message i sent. Do you know how I can get the data for using it in the loop fuction? Or better could you tell me cause you know it for sure.

Or better could you tell me cause you know it for sure.

Without seeing all your code, all anyone can do is guess.

Sorry I forgot? But I spent the whole night reading web pages and trying stuff I am not at 100% anymore. I attached the sketch but it´s not finished yet I wanted too first fix the communication.
And please don´t blame me if my code is sh... but as I said I am trying too llearn. :.

Thank´s and greetings

Stefan

ENC28J60_UDP_Outputs.ino (1.53 KB)

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Sorry that was a mistake! Won´t happen again.

int port = port;

WTF?

//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[], const char *data, word len) {
  IPAddress src(ip[0], ip[1], ip[2], ip[3]);
 
  
  Serial.println(src);
  Serial.println(port);
  Serial.println(data);
  
}

The callback can do more than print the data received.

Get rid

of all the stupid white space

in your code.

Describe what you are sending, from where, and what the Arduino claims to be receiving.

Yeah I know that port stuff is a left over from trying things out. And it seemed too me that it would be easier if I split it up a little bit, but I got the point in your message this is something I am going to change in future codes.
Actually I was planning to send something like "Licht1", "Licht2" ........ but I can send any textmessage I want or need. And I send from a static IP Address with the UDP ports 7050 ........,
I send the same message for on and off and it is sent only once for every action.
Thank´s for spending your time for my problem and

Greetings Stefan

I send the same message for on and off and it is sent only once for every action.

So? That wasn't the question. The question is what is RECEIVED?

New question. Does what is received match what is sent?

Hi Paul
that is what the Arduino is going too receive. I will not send with the Arduino only receiving. I am sorry it was not good communicated from me that´s because o my english is getting worse as I don´t speek it that often anymore,
I describe it a little more detailed.
The Arduio is only some kind of a output modul for a PLC in my home automation system. The PLC acts as server and has a static IP Address. I don´t even need acknowledgement since this is done also by the PLC via hardware response. So all the Arduino needs to do is if it receives a UDP message on port 7050 change the state of output1, incoming message on port 7051 change the state of output2 and so on

Greetings Stefan

Where does your library come from?

Why particularly do you want to use port numbers to tell you which output to use? Usually, arduino ethernet libraries have few sockets available (four?) so this method isn't very scalable - it would seem more sensible to use the message content. Are you restricted by the capabilities of the PLC?

The problem is the PLC. If you use a UDP port in one function you can not use it in another one anymore. The only thing I can tell the PLC is the starting address, the destination and the message being send in case of being triggered.
The library for my enc28j60 I have from the manufactors homepage.

The frustrating thing is that I have a sketch that does exactly what I want but this is for the W5100. I would rather use the enc28j60 cause of its integrated POE modul.