所以事情是这样的,我有 2 个 arduino mega,两个都有w5100(wiznet)
盾牌。一个有光传感器,我需要另一个能够从另一个位置获取光传感器的值。我已经搜索过,但找不到这样的东西。我有它client.Println();
的价值,但我不确定如何获取,而不是存储它。
请帮忙,乔伊
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.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);
到目前为止只是我的发现!:)