eagler8 发表于 2020-3-10 18:01:12

eagler8 发表于 2020-3-10 18:02:51

eagler8 发表于 2020-3-10 21:06:40

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

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

eagler8 发表于 2020-3-10 21:11:00

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

安装 "Ethernet.h"库-工具-管理库-搜索-安装
项目测试 :通过插入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(" Failed to Configure Ethernet using DHCP");
    Ethernet.begin(mac, staticIP);//DHCP不能动态分配,就静态设置ip给Arduino

}
delay(1000);
Serial.println(" Connection Successsful");
Serial.print("");
printConnectionInformation();
Serial.println("-------------------------");
Serial.println("");
}
void printConnectionInformation()
{
Serial.print(" IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print(" Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print(" Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print(" DNS: ");
Serial.println(Ethernet.dnsServerIP());
}
void setup() {
// 将设置代码放在此处,运行一次:
Serial.begin(9600);
connectToInternet();
server.begin();

}


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

          // 输出每个模拟输入引脚的值
          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')的
      //用(c == '\n')和 (c != '\r')的话,客户端连接不上服务器,不能用
      if (c == 'n') {
          // 我们要开始新的生产线
          current_line_is_blank = true;
      } else if (c != 'r') {
          // 我们在当前行中找到了一个角色
          current_line_is_blank = false;
      }
      }
    }
    client.stop();
}
}

eagler8 发表于 2020-3-10 21:13:50



eagler8 发表于 2020-3-10 21:15:30

eagler8 发表于 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不能与局域网内的其它设备重复。

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

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

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

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

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(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 80 is default for HTTP):
EthernetClient client;

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

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
}
// 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, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.baidu.com");
    client.println("Connection: close");
    client.println();
}
else {
    // kf 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:
if (client.available()) {
    char c = client.read();
    Serial.print(c);
}

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

    // do nothing forevermore:
    while (true);
}
}

eagler8 发表于 2020-3-10 21:54:53

能显示服务器返回的数据,证明通讯成功。

eagler8 发表于 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页面,用来显示模拟传感器的数值。

eagler8 发表于 2020-3-11 09:58:44


eagler8 发表于 2020-3-11 10:03:49


子杰子杰子杰 发表于 2020-12-24 09:39:42

arduino+w5100

eagler8 发表于 2020-12-24 09:52:07

子杰子杰子杰 发表于 2020-12-24 09:39
arduino+w5100

:victory: 这板子漂亮
页: 1 [2]
查看完整版本: 【Arduino】168种传感器模块系列实验(144)---W5100 网络扩展板