如何让2个Arduinos通过以太网进行变量通信?

如何让2个Arduinos通过以太网进行变量通信?,arduino,ethernet,Arduino,Ethernet,我有两个arduino mega,都有一个w5100(wiznet)屏蔽。一个有光传感器,我需要另一个能够从另一个位置获取光传感器的值。我已经找过了,但是找不到像这样的东西。我有它client.Println()值,但我不确定如何获取并存储它 乔伊,请帮忙。上给出的代码几乎准确地告诉了你需要什么。此处复制/改编的亮点 首先是“接收端”: #包括 #包括 //网络配置。 //网关和子网是可选的。 //屏蔽的介质访问控制(以太网硬件)地址: 字节mac[]={0xDE,0xAD,0xBE,0xEF,

我有两个arduino mega,都有一个
w5100(wiznet)
屏蔽。一个有光传感器,我需要另一个能够从另一个位置获取光传感器的值。我已经找过了,但是找不到像这样的东西。我有它
client.Println()值,但我不确定如何获取并存储它

乔伊,请帮忙。

上给出的代码几乎准确地告诉了你需要什么。此处复制/改编的亮点

首先是“接收端”:

#包括
#包括
//网络配置。
//网关和子网是可选的。
//屏蔽的介质访问控制(以太网硬件)地址:
字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
//屏蔽的IP地址:
字节ip[]={10,0,0,177};
//路由器的网关地址:
字节网关[]={10,0,0,1};
//子网:
字节子网[]={255,255,0,0};
//telnet默认为端口23
EthernetServer服务器=EthernetServer(23);
无效设置()
{
//初始化以太网设备
以太网开始(mac、ip、网关、子网);
//开始监听客户机
server.begin();
}
void循环()
{
//如果传入客户端连接,将有字节可读取:
字符输入[100];
EthernetClient=server.available();
if(client==true){
//从传入的客户端读取字节并将其写回
//连接到服务器的任何客户端:
int ii=0;
而((c=client.read())!='\n')
{
传入[ii++]=c;
}
//传入变量[]现在包含最近发送的值
//所以你可以用它做点什么
}
}
现在是“发送部件”(在作为数据源的Arduino上运行的草图):

#包括
#包括
//灵感来自/抄袭自http://arduino.cc/en/Tutorial/TelnetClient
//在下面输入控制器的MAC地址和IP地址:
//IP地址将取决于您的本地网络:
字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
ip地址ip(192168,1176);//这是数据源卡的IP地址
//要连接到的服务器的IP地址:
IP地址服务器(192168,1177);
//初始化以太网客户端库
//具有服务器的IP地址和端口
//要连接到的端口(telnet的默认端口为23;
以太网络客户端;
int port=23;//telnet默认端口
char myVar[100];//包含带有要传输的变量的字符串
无效设置(){
//启动以太网连接:
以太网开始(mac,ip);
//打开串行通信并等待端口打开:
Serial.begin(9600);
而(!串行){
;//等待串行端口连接。仅Leonardo需要
}
//给以太网屏蔽一秒钟进行初始化:
延迟(1000);
Serial.println(“连接…”);
//如果您获得连接,请通过串行方式报告:
if(客户端连接(服务器、端口)){
Serial.println(“已连接”);
} 
否则{
//如果您没有连接到服务器:
Serial.println(“连接失败”);
}
}
void循环()
{
//如果有可用的传入字节
//从服务器读取并打印它们:
//上面的服务器代码没有发送任何内容…
//但如果是的话,你会在这里回应它
int ii;
if(client.available()){
char c=client.read();
Serial.print(“***服务器显示:***\n”);
连续打印(c);
}
//假设变量myVar中有一个有效字符串。。。
strcpy(myVar,“123.456\n”);
//告诉串行端口您正在发送什么:
Serial.print(“发送变量:”);
连续打印(myVar);
对于(ii=0;ii

我没有两个带以太网屏蔽的Arduino,因此我必须根据我所知道/可以查找的内容将其拼接在一起。让我知道你是如何完成这项工作的!

尝试此功能的人的一些提示:

首先,在我写这篇文章的时候,上面的代码在1.0.5以上的任何版本上都不起作用(1.0.5r2可以正常工作)

发送方还具有以下功能:

// the IP address of the server you're connecting to:
IPAddress server(192.168,1,177);
应该是(逗号,而不是空格):


到目前为止,这只是我的发现!:)

使用以太网可能不是传递变量的最有效方式。有关一些基本想法和概念,请参见。您需要让一个Arduino“监听”端口,另一个进行连接。有关如何创建服务器的信息,请参见。谢谢?这只是镜像发送的字节。。。我不知道如何从第二个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
}
// 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);