【发布时间】:2013-04-24 14:32:24
【问题描述】:
我是这个论坛的新手,也是 Processing 的新手。 我有一个具体问题要问,非常感谢您的时间和想法!
如何将我的 Arduino 与 Ethernet Shield 连接,从传感器获取温度值,以便处理脚本可以看到它们? 在直接的 Arduino 脚本中,获取值,从以太网屏蔽连接到服务器并做自己喜欢的事情。我已经做到了。
在我的情况下,我希望 Arduino 只运行从传感器读取模拟输入值的脚本。 有可能吗?
我已经使串行连接工作并通过 USB 读取值正常,但是使用以太网屏蔽?如何获得 arduino 读取的值WITHOUT USB/Serial connection?
ps。我正在使用 WAMP 服务器等,Windows 7
我正在为 arduino 和 http://arduino.cc/en/Tutorial/UDPSendReceiveString 的处理尝试 UDP 连接脚本示例,但是
1)我不确定这是否是我需要的,
2)我已从防火墙端口 6000、8888 中排除我的测试,并将我的 Arduino 的 IP 地址放在 Arduino 脚本中,并将“localhost”放在处理脚本中
为了更好地使用这里复制的代码
/*
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();
}
【问题讨论】:
-
这不是直接回答您的问题,但您能否先通过 USB/串行连接对通信进行原型设计,以确保您可以先在处理中发送和接收/组织信息?有一个内置的 Arduino 库可以帮助您...
-
感谢 jesses 的回复,但正如我已经说过的,串行连接很好,一切都按预期工作。附言。你能具体说明一下你所说的原型是什么意思吗?
标签: udp arduino processing ethernet