如何使用udp指令在arduino以太网上设置RGB LED?

时间:2019-05-30 17:28:10

标签: arduino

我正在使用RGB LED制作交通信号灯。我尝试获取从服务器发送到arduino mega和以太网屏蔽的udp值,然后arduino应该更改LED的颜色。

可悲的是,到目前为止,它不起作用。在串行监视器中,我发现收到了udp数据包,但是LED不工作。我希望你们能帮助我弄清楚为什么我的代码无法正常工作。先感谢您!这是我的代码:

/*
  Web Server
 */

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



    void Color(int R, int G, int B) //set up for the RGB led
    {
          analogWrite(3, R) ;   // Rojo
          analogWrite(5, G) ;   // Green - Verde
          analogWrite(6, B) ;   // Blue - Azul
    }

// 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(10, 90, 111, 150);

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

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

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

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {  //seting up the outputs for the rgb 
       pinMode(3, OUTPUT);
       pinMode(5, OUTPUT);
       pinMode(6, OUTPUT);



  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet


    // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer Example");

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  Udp.begin(localPort);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {


// if there's data available, read a packet
  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 packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();

  }
       char numero = packetBuffer;
         if (numero = "1") Color(250, 0, 0) ;
         if (numero = "2") Color(100, 110, 0);
         if (numero = "3") Color(0, 255, 0);
  delay(10);

}

从现在开始,它只接收udp,并且在编译时得到:

warning: invalid conversion from 'const char*' to 'char' [-fpermissive]

但最终它会编译。

编辑:

我以这种方式更改了代码(仅在最后一部分),现在当我发送udp数据包时,无论我发送什么,我都会亮绿灯,但只有绿色的亮,这是代码

  void loop() {


// if there's data available, read a packet
  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 packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    char numero =  packetBuffer;
         if (numero = 'R') Color(250, 0, 0) ;
         if (numero = 'A') Color(100, 100, 0);
         if (numero = 'V') Color(0, 255, 0);         
    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();

  }

1 个答案:

答案 0 :(得分:0)

我建议为初学者找到一些C ++教程。

无论如何:

char numero =  packetBuffer; // wrong, you are assigning part of address to char, not character at that address... 
if (numero = 'R') Color(250, 0, 0) ; // better but still wrong - numero is set to 'R' and it's also always true
if (numero = 'A') Color(100, 100, 0); // similar to previous
if (numero = 'V') Color(0, 255, 0);   // and again

正确版本:

char numero =  packetBuffer[0]; // get the first character in buffer
if (numero == 'R') Color(250, 0, 0) ; // compare numero with 'R' 
if (numero == 'A') Color(100, 100, 0); // compare numero with 'A' 
if (numero == 'V') Color(0, 255, 0);   // ...

如果没有其他问题,那应该解决