极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: eagler8

【Arduino】168种传感器模块系列实验(144)---W5100 网络扩展板

[复制链接]
 楼主| 发表于 2020-3-10 18:01:12 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 18:02:51 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:06:40 | 显示全部楼层
【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  实验一百四十四:Ethernet W5100S 网络扩展板 SD卡扩展模块 支持MEGA

  安装 "Ethernet.h"库-工具-管理库-搜索-安装
  项目测试 :通过插入W5100 以太网扩展板,实现Arduino NUO 接入以太网

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:11:00 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十四:Ethernet W5100S 网络扩展板 SD卡扩展模块 支持MEGA

  4.   安装 "Ethernet.h"库-工具-管理库-搜索-安装
  5.   项目测试 :通过插入W5100 以太网扩展板,实现Arduino NUO 接入以太网
  6. */

  7. #include <Ethernet.h>
  8. #include <SPI.h>

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

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

  12. EthernetServer server(80);

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

  19.   }
  20.   delay(1000);
  21.   Serial.println("[INFO] Connection Successsful");
  22.   Serial.print("");
  23.   printConnectionInformation();
  24.   Serial.println("-------------------------");
  25.   Serial.println("");
  26. }
  27. void printConnectionInformation()
  28. {
  29.   Serial.print("[INFO] IP Address: ");
  30.   Serial.println(Ethernet.localIP());
  31.   Serial.print("[INFO] Subnet Mask: ");
  32.   Serial.println(Ethernet.subnetMask());
  33.   Serial.print("[INFO] Gateway: ");
  34.   Serial.println(Ethernet.gatewayIP());
  35.   Serial.print("[INFO] DNS: ");
  36.   Serial.println(Ethernet.dnsServerIP());
  37. }
  38. void setup() {
  39.   // 将设置代码放在此处,运行一次:
  40.   Serial.begin(9600);
  41.   connectToInternet();
  42.   server.begin();

  43. }


  44. void loop()
  45. {
  46.   //当有客户连接服务器时,服务器available函数会返回一个客户端对象用以向客户反馈信息
  47.   EthernetClient client = server.available();
  48.   if (client) {
  49.     // http请求以空行结束
  50.     boolean current_line_is_blank = true;
  51.     while (client.connected()) {
  52.       if (client.available()) {
  53.         char c = client.read();
  54.         // 如果我们排到了队伍的尽头
  55.         // (字符)且该行为空,则http请求已结束,
  56.         // 所以我们可以回复
  57.         if (c == 'n' && current_line_is_blank) {
  58.           // 发送标准http响应头
  59.           client.println("HTTP/1.1 200 OK");
  60.           client.println("Content-Type: text/html");
  61.           client.println();

  62.           // 输出每个模拟输入引脚的值
  63.           client.print("welcome to tinyos electronics");
  64.           client.println("<br />");
  65.           client.print("//*************************************");
  66.           client.println("<br />");
  67.           client.print("");
  68.           client.println("<br />");
  69.           client.print("//*************************************");
  70.           client.println("<br />");
  71.           for (int i = 0; i < 6; i++) {
  72.             client.print("analog input ");
  73.             client.print(i);
  74.             client.print(" is ");
  75.             client.print(analogRead(i));
  76.             client.println("<br />");
  77.           }
  78.           break;
  79.         }
  80.         //有的教程里也有用(c == '\n')和 (c != '\r')的
  81.         //用(c == '\n')和 (c != '\r')的话,客户端连接不上服务器,不能用
  82.         if (c == 'n') {
  83.           // 我们要开始新的生产线
  84.           current_line_is_blank = true;
  85.         } else if (c != 'r') {
  86.           // 我们在当前行中找到了一个角色
  87.           current_line_is_blank = false;
  88.         }
  89.       }
  90.     }
  91.     client.stop();
  92.   }
  93. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:13:50 | 显示全部楼层


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:15:30 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:52:22 | 显示全部楼层
ARDUINO W5100 WebClient 测试
基础工作:W5100扩展板插在ARDUINO上。用网线把W5100和自己家的路由器连接。插上网线能看到侧面网口指示灯变亮。路由器开启DHCP服务(一般都是开启的)。

1.打开官方例程里面的Ethernet->WebClient

2.修改里面的谷歌服务器为百度的。

3.修改IP地址为本地的局域网号码段,比如你的电脑是192.168.1.100。那么设置你的w5100,也在192.168.1.x。后面的x不能与局域网内的其它设备重复。

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十四:Ethernet W5100S 网络扩展板 SD卡扩展模块 支持MEGA

  4.   安装 "Ethernet.h"库-工具-管理库-搜索-安装
  5.   项目测试之二 :ARDUINO W5100 WebClient 测试
  6. */

  7. #include <SPI.h>
  8. #include <Ethernet.h>

  9. // Enter a MAC address for your controller below.
  10. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  11. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  12. // if you don't want to use DNS (and reduce your sketch size)
  13. // use the numeric IP instead of the name for the server:
  14. //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
  15. char server[] = "www.baidu.com";    // name address for Google (using DNS)

  16. // Set the static IP address to use if the DHCP fails to assign
  17. IPAddress ip(192, 168, 1, 177);

  18. // Initialize the Ethernet client library
  19. // with the IP address and port of the server
  20. // that you want to connect to (port 80 is default for HTTP):
  21. EthernetClient client;

  22. void setup() {
  23.   // Open serial communications and wait for port to open:
  24.   Serial.begin(9600);
  25.   while (!Serial) {
  26.     ; // wait for serial port to connect. Needed for Leonardo only
  27.   }

  28.   // start the Ethernet connection:
  29.   if (Ethernet.begin(mac) == 0) {
  30.     Serial.println("Failed to configure Ethernet using DHCP");
  31.     // no point in carrying on, so do nothing forevermore:
  32.     // try to congifure using IP address instead of DHCP:
  33.     Ethernet.begin(mac, ip);
  34.   }
  35.   // give the Ethernet shield a second to initialize:
  36.   delay(1000);
  37.   Serial.println("connecting...");

  38.   // if you get a connection, report back via serial:
  39.   if (client.connect(server, 80)) {
  40.     Serial.println("connected");
  41.     // Make a HTTP request:
  42.     client.println("GET /search?q=arduino HTTP/1.1");
  43.     client.println("Host: www.baidu.com");
  44.     client.println("Connection: close");
  45.     client.println();
  46.   }
  47.   else {
  48.     // kf you didn't get a connection to the server:
  49.     Serial.println("connection failed");
  50.   }
  51. }

  52. void loop()
  53. {
  54.   // if there are incoming bytes available
  55.   // from the server, read them and print them:
  56.   if (client.available()) {
  57.     char c = client.read();
  58.     Serial.print(c);
  59.   }

  60.   // if the server's disconnected, stop the client:
  61.   if (!client.connected()) {
  62.     Serial.println();
  63.     Serial.println("disconnecting.");
  64.     client.stop();

  65.     // do nothing forevermore:
  66.     while (true);
  67.   }
  68. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-10 21:54:53 | 显示全部楼层
能显示服务器返回的数据,证明通讯成功。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-11 09:43:36 | 显示全部楼层
本帖最后由 eagler8 于 2020-3-11 09:53 编辑

Ethernet Library(以太网库)

通过Arduino Ethernet 开发板或者shield,使能网络连接(本地和互联网)。
更多的信息参考https://www.arduino.cc/en/Reference/Ethernet
适用于所有Arduino开发板板。

Advanced Chat Server: 建立一个简单的聊天服务器。
Barometric Pressure Web Server: 输出从气压传感器传来的数值,作为一个网页。
Chat Server: 建立一个简单的聊天服务器。
Dhcp Address Printer: 通过DHCP获取一个IP地址,并打印出来。
Dhcp Chat Server: 一个简单的DHCP聊天服务器
Telnet Client: 一个简单的telnet客户端。
UDP Ntp Client: 通过UDP查询网络时间协议(NTP)服务器。
UDP Send Receive String: 通过UDP发送和接收文本字符串。
Web Client: 做一个HTTP请求。
Web Client Repeating: 重复HTTP请求。
Web Server: 创建一个简单的HTML页面,用来显示模拟传感器的数值。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-11 09:58:44 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-11 10:03:49 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2020-12-24 09:39:42 | 显示全部楼层
arduino+w5100

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-24 09:52:07 | 显示全部楼层

这板子漂亮
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-18 08:47 , Processed in 0.042775 second(s), 16 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表