将Arduino接入以太网

通过插入W5100 以太网盾板,实现Arduino NUO 接入以太网的想法

#include <Ethernet.h>
//#include <SPI.h>

//mac地址可以是随便的48位地址,只要设备间不相互冲突就行
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress staticIP(192,168,31,159);

EthernetServer server(80);

void connectToInternet()
{
  if (Ethernet.begin(mac) == 0)//看看DHCP是否能动态分配ip给Arduino
  {
    Serial.print("[ERROR] Failed to Configure Ethernet using DHCP");
    Ethernet.begin(mac, staticIP);//DHCP不能动态分配,就静态设置ip给Arduino

  }
  delay(1000);
  Serial.println("[INFO] Connection Successsful");
  Serial.print("");
  printConnectionInformation();
  Serial.println("-------------------------");
  Serial.println("");
}
void printConnectionInformation()
{
  Serial.print("[INFO] IP Address: ");
  Serial.println(Ethernet.localIP());
  Serial.print("[INFO] Subnet Mask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("[INFO] Gateway: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("[INFO] DNS: ");
  Serial.println(Ethernet.dnsServerIP());
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  connectToInternet();
  server.begin();
  
}


void loop()
{
  //当有客户连接服务器时,服务器available函数会返回一个客户端对象用以向客户反馈信息
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (c == 'n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          client.print("welcome to tinyos electronics");
          client.println("<br />");
          client.print("//*************************************");
          client.println("<br />");
          client.print("");
          client.println("<br />");
          client.print("//*************************************");
          client.println("<br />");
          for (int i = 0; i < 6; i++) {
            client.print("analog input ");
            client.print(i);
            client.print(" is ");
            client.print(analogRead(i));
            client.println("<br />");
          }
          break;
        }
        //有的教程里也有用(c == '\n')和 (c != '\r')的
        //2018-12-16补充试验,用(c == '\n')和 (c != '\r')的话,客户端连接不上服务器,不能用啊
        if (c == 'n') {
          // we're starting a new line
          current_line_is_blank = true;
        } else if (c != 'r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    client.stop();
  }
}

下载程序运行如下图(路由器动态分配了ip)

连接正常时以太网盾板FULLD,100M,LINK,PWR四个led灯都需要亮,不亮的话重启下试试

  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在Arduino接入以太网,你可以使用以太网模块或以太网盾。以下是一些基本步骤: 1. 首先,确保你的Arduino板支持以太网连接。常见的Arduino以太网模块包括Ethernet Shield、Ethernet Shield 2和Ethernet FeatherWing等。 2. 将以太网模块插入Arduino的扩展插槽上(如Shield接口)或连接到相应的引脚上。根据你使用的模块,可能需要使用跳线连接。 3. 连接以太网模块与你的局域网或路由器。通常,以太网模块会提供一个RJ45网络连接接口,你需要使用网线将其连接到网络中。 4. 在Arduino开发环境中,导入并安装以太网库。常见的库包括Ethernet库和UIPEthernet库。你可以通过“工具”->“管理库”菜单来搜索和安装这些库。 5. 在你的Arduino代码中,初始化以太网模块并设置网络参数,例如IP地址、子网掩码和默认网关。这些参数通常在你的路由器上可以找到。 6. 使用以太网库提供的函数进行网络通信。你可以使用TCP或UDP协议与其他设备进行通信,发送和接收数据。 这是一个简单的示例代码,用于初始化以太网模块并发送HTTP请求: ``` #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 10); EthernetClient client; void setup() { Ethernet.begin(mac, ip); delay(1000); Serial.begin(9600); Serial.println("Connecting to server..."); if (client.connect("www.example.com", 80)) { Serial.println("Connected!"); client.println("GET / HTTP/1.1"); client.println("Host: www.example.com"); client.println("Connection: close"); client.println(); } } void loop() { if (client.available()) { char c = client.read(); Serial.print(c); } if (!client.connected()) { Serial.println(); Serial.println("Disconnected from server."); client.stop(); while(true); // 停止程序运行 } } ``` 这只是一个基本示例,你可以根据需要进行扩展和修改。记得根据你的以太网模块和网络设置进行适当的调整。希望对你有所帮助!
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值