连接Arduino以太网屏蔽并读取数据问题处理

时间:2013-04-24 14:32:24

标签: udp arduino processing ethernet

我是这个论坛的新手,也是Processing的全部内容。 我有一个具体的问题要问你,非常感谢你的时间和想法!

如何将我的Arduino与Ethernet Shield连接起来,从传感器获取温度值,以便将它们看作处理脚本? 在一个直接的Arduino脚本中,一个获取值,从以太网屏蔽连接到服务器并做自己喜欢的事情。我已经完成了。

在我的情况下,我希望Arduino只运行从传感器读取模拟输入值的脚本。 有可能吗?

我已经使串行连接工作并通过usb读取值,但是使用以太网屏蔽?如何获得arduino读取的值没有USB /串行连接?

PS。我正在使用WAMP服务器等,Windows 7

我正在尝试从http://arduino.cc/en/Tutorial/UDPSendReceiveString进行arduino和处理的UDP连接脚本示例,但是

1)我不确定这是否是我需要的,

2)我已从防火墙端口6000,8888中排除了我的测试,并将我的Arduino的IP地址放在Arduino脚本中,将“localhost”放在Processing脚本

为了更好地使用此处而修改的代码

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */


#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


// 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, 177);

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;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

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();
  }
  delay(10);
}


/*
  Processing sketch to run with this example
 =====================================================

 // Processing UDP example to send and receive string data from Arduino
 // press any key to send the "Hello Arduino" message
 */

 import hypermedia.net.*;

 UDP udp;  // define the UDP object


 void setup() {
 udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
 //udp.log( true );         // <-- printout the connection activity
 udp.listen( true );           // and wait for incoming message  
 }

 void draw()
 {
 }

 void keyPressed() {
 String ip       = "192.168.1.177"; // the remote IP address
 int port        = 8888;        // the destination port

 udp.send("Hello World", ip, port );   // the message to send

 }

 void receive( byte[] data ) {          // <-- default handler
 //void receive( byte[] data, String ip, int port ) {   // <-- extended handler

 for(int i=0; i < data.length; i++)
 print(char(data[i]));  
 println();  
 }

2 个答案:

答案 0 :(得分:1)

将这些值读入文件并使用该文件将数据发送到处理中。 http://py.processing.org/reference/createReader.html

答案 1 :(得分:0)

很棒的计划。只有一个问题。它在我的系统上完美运行。我用你的Arduino草图装载了我的Arudino Uno R3并加载了Processing sketch。工作就像一个魅力,第一次尝试。没有更改我的Arduino,Windows系统,处理(2.0.3),网络等

你可能有Arduino板问题(不太可能)或以太网屏蔽问题(遗憾的是,更有可能)。您可能遇到网络问题(甚至更可能)。

尝试Wireshark。你真的只是猜测,直到你看看Wireshark输出。请注意,Wireshark有过滤器。你需要它们。过滤掉所有非UDP流量。