所以这就是它如何,我有2个arduino mega,两个都有w5100(wiznet)
盾牌。一个有光传感器,我需要另一个能够从另一个位置获得光传感器的值。我搜索过但却找不到这样的东西。我有client.Println();
值,但我不确定如何获取,而不是存储它。
请帮忙,乔伊
答案 0 :(得分:2)
http://arduino.cc/en/Reference/EthernetServer给出的代码几乎可以告诉您需要什么。精彩复制/改编在这里。
首先是“接收端”:
#include <SPI.h>
#include <Ethernet.h>
// network configuration.
// gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
// telnet defaults to port 23
EthernetServer server = EthernetServer(23);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
char incoming[100];
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
int ii = 0;
while ((c = client.read()) != '\n')
{
incoming[ii++] = c;
}
// the variable incoming[] now contains the most recent value sent
// so you can do something with it
}
}
现在是“发送部分”(在作为数据源的Arduino上运行的草图):
#include <SPI.h>
#include <Ethernet.h>
// inspired by/copied from http://arduino.cc/en/Tutorial/TelnetClient
// 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,176); // this is the data source card IP address
// the IP address of the server you're connecting to:
IPAddress server(192,168,1,177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
EthernetClient client;
int port = 23; // telnet default port
char myVar[100]; // contains string with variable to transmit
void setup() {
// start the Ethernet connection:
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 Leonardo only
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
// the server code above doesn't send anything…
// but if it did, this is where you would echo it
int ii;
if (client.available()) {
char c = client.read();
Serial.print("***Server says:***\n");
Serial.print(c);
}
// assume your variable myVar will have a valid string in it...
strcpy(myVar, "123.456\n");
// tell the serial port what you are sending:
Serial.print("sending variable: ");
Serial.print(myVar);
for(ii = 0; ii < strlen(myVar); ii++) {
if (client.connected()) {
client.print(myVar[ii]);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
// add appropriate delay here before sending next data element
}
我没有两个带有以太网屏蔽的Arduino,所以我不得不从我知道/可以查找的内容中将它拼凑在一起。让我知道你如何继续这个!
答案 1 :(得分:-1)
为尝试此操作的人提供了一些提示:
首先,在我写这篇文章的时候,上面的代码不适用于任何版本的ABOVE 1.0.5(1.0.5r2工作正常)。
发件人还有以下内容:
// the IP address of the server you're connecting to:
IPAddress server(192.168,1,177);
应该是(逗号,不是空格):
// the IP address of the server you're connecting to:
IPAddress server(192,168,1,177);
到目前为止我的发现! :)