0

当我使用 Ethernet.begin(mac,ip) 时,led 没有打开和关闭。但是当我不使用那条线时它会起作用。但我需要通过使用以太网和 UPP 模块来开启和关闭。我怎样才能 ?

板卡型号:Ethernet 08 Twinker 套件、Twinker 继电器

在此处输入图像描述

#include <TinkerKit.h>
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

TKLed led(O5); //O0 does not work
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "ackv1";       // a string to send back
EthernetUDP Udp;

void setup() {
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    // check the switch state  
    delay(1000);
    led.off();                        
    delay(3000);
    led.on(); 
    // 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();         
  }
  delay(10);
}

编辑:

$ echo "hello" | nc -4u 192.168.1.177 8888
ackv1
4

1 回答 1

0

From your description it sounds like the Ethernet I/O is interfering with the LED I/O.

Notice that:

TKLed led(O0);

This probably defines the led to be at port 0. You need to determine what ports are used by Ethernet. (I would tell you but this is a good lesson.)

IF (you need to read documentation) there is a conflict between the led port number and the ethernet port numbers then you will need to move the LED, because the ethernet ports require serial I/O, while the LED just requires a +5V or 0V.

于 2013-09-06T00:18:00.290 回答