Hi, I'm really new in programming, but willing to learn. I have a project where I have an Ethernet Shield connected to an Arduino Mega. This ethernet shield is connected to a 4 port router. The arduino Mega controls a ws2812b addressable rgb led strip, wich is powered by an independent 12v source. I ran several tests from the FastLED library, no problems there. The thing is I want to be able to send a "play" message to the arduino through A Lan device, wich sends udp messages using local port 8080. Let me know if anyone can help, and what other info can I supply in order to get help. Thanks in advance to everyone here!
rather than implementing a UDP client/server it may be simpler to run a webserver on the Mega which when a client connects displays a HTML page which enables you to enter information to control the LEDs, e.g. using a HTML form
try a web search for Arduino Enthernet webserver and Arduino HTML form
No, I can't. The device wich sends the message only uses udp. And there is no internet connection either.
This example looks like enough to get you started.
have a look at File>Examples>Ethernet - ther are a couple of example programs using UDP
do you have details of the protocol/data format the target device uses?
I'll try that, thank you.
I've already used the examples without any good results. I only know that the device sends udp messages through local port 8080. I don't need arduino to answer, just to understand the message to turn on the led strip
I tried that, thanks very much. I´ve achieved that arduino prints the message in the serial connection, but couldn´t make that it turns on the led strip. I share the code here:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <FastLED.h>
#define DATA_PIN 38
#define NUM_LEDS 300
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 240
CRGB leds[NUM_LEDS];
// 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, 210);
unsigned int localPort = 8080; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup() {
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER> (leds, NUM_LEDS); // GRB ordering is typical
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
Serial.println("conectado");
}
void loop() {
int packetSize = Udp.parsePacket();
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 packetBuffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
if (packetBuffer == "play")
{
for (int s=0; s<NUM_LEDS; s++) {
leds[s] = CRGB::DarkOliveGreen;
}
for (int s=20; s<BRIGHTNESS; s++) {
FastLED.setBrightness(s);
FastLED.show();
delay(0);
}
delay(1000);
for (int s=BRIGHTNESS; s>=20; s--) {
FastLED.setBrightness(s);
FastLED.show();
delay(0);
}
}
if (packetBuffer == "stop"){
FastLED.clear();
FastLED.show();
}
}
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear out the packetBuffer array
}
you appear to be using == to compare C type char strings
try strcmp()
if (strcmp(packetBuffer , "play") == 0) {
........
I assume the received text does not contain end of line charcaters etc
Ok, first of all, thanks a lot for the help. I did as you said, no answer on the lights so I added a serial print to know if the string comparison works or not. Below is just the part we are working on right now.
// read the packet into packetBuffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
}
if(strcmp(packetBuffer,"play")==0){
Serial.println("Done");
lights();
}
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear out the packetBuffer array
}
void lights (){
for (int s=0; s<NUM_LEDS; s++) {
leds[s] = CRGB::DarkOliveGreen;
}
for (int s=20; s<BRIGHTNESS; s++) {
FastLED.setBrightness(s);
FastLED.show();
delay(0);
}
delay(1000);
for (int s=BRIGHTNESS; s>=20; s--) {
FastLED.setBrightness(s);
FastLED.show();
delay(0);
}
}
I suspect there may be other character in the received string
try strstr() which looks for a string in a string, e.g.
if(strstr(packetBuffer,"play")!=NULL){
what does the serial monitor display?
C strings need a terminating null for functions like strcmp and strstr to work properly. Unless you had the sender add one (unlikely), they aren't what you need. Try memcmp.
It works with the strstr! ohhh, The Gods may bless you, oh Wise Magicians! Thanks a lot! you both @horace and @wildbill have been a light in the dark! Thank you again!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.