找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

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

查看数: 6231 | 评论数: 34 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2020-3-9 17:25

正文摘要:

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备 ...

回复

ID:890367 发表于 2022-7-12 19:11
eagler8 发表于 2020-3-10 16:24

有没有清楚一点的原理图
ID:513258 发表于 2020-3-11 09:42
Ethernet Library(以太网库)
通过Arduino Ethernet 开发板或者shield,使能网络连接(本地和互联网)。更多的信息参考the Reference for the Ethernet Library page。适用于所有Arduino开发板板。

ID:513258 发表于 2020-3-10 21:51
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. }
复制代码





ID:513258 发表于 2020-3-10 21:10
  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. }
复制代码


小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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