最新消息:

Arduino库教程-Ethernet-Dhcp Chat Server

Arduino 少儿编程 1548浏览 0评论
Arduino库教程

DHCP Chat Server

  • 这个例子将通过以太网shield来连接一个telnet服务器。来自服务器的消息将通过串行端口打印出来。消息同样也可以发送到远程服务器。串口监视器对实现这个目的很有用处。

  • 这个版本试图用DHCP获得一个IP地址。当Ethernet.begin(mac) 被调用时,一个IP地址可以通过DHCP分配。注意,当使用DHCP扩展,程序大小会明显增大。

硬件要求

  • Arduino 或者 Genuino 开发板
  • Arduino Ethernet Shield

软件要求

  • 一个telnet服务器
  • 另外,Processing有一个ChatServer例子可以实现这个目的

电路

  • 以太网shield可以让你通过SPI总线连接一个 Wiznet 以太网控制器到Arduino或者genuino开发板板。它使用SPI总线连接的引脚pin 10,11,12,和13,到Wiznet。以太网shield后来的模块也有一个SD卡在板上。数字引脚 pin 4 用来控制SD卡上的从选择引脚(slave select pin)。

  • shield应该连接到一个有以太网电缆的网络。您将需要更改程序中的网络设置来对应于您的网络。
    Arduino库教程-Ethernet-Dhcp Chat Server
    图由 Fritzing 软件绘制

  • 在上面的图片里,Arduino或genuino开发板会堆叠在以太网shield下面。

原理图
Arduino库教程-Ethernet-Dhcp Chat Server

样例代码

/*
 DHCP Chat  Server

 A simple server that distributes any incoming messages to all
 connected clients.  To use, telnet to your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Wiznet Ethernet shield.

 THis version attempts to get an IP address using DHCP

 Circuit:

 * Ethernet shield attached to pins 10, 11, 12, 13

 created 21 May 2011
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 Based on ChatServer example by David A. Mellis

 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // initialize the Ethernet device not using DHCP:
    Ethernet.begin(mac, ip, myDns, gateway, subnet);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  // start listening for clients
  server.begin();

}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Hello, client!");
      gotAMessage = true;
    }

    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // echo the bytes to the server as well:
    Serial.print(thisChar);
    Ethernet.maintain();
  }
}

[Get Code]
更多

  • Arduino Ethernet Shield – 产品描述。
  • Getting started with the Ethernet Shield – 在几分钟内启动所有东西。
  • Ethernet library – 以太网库的参考手册
  • AdvancedChatServer – 一个服务器,用来发送所有传入的信息到所有连接的客户端(除了那个发送信息的客户端)。
  • ChatServer – 一个简单的服务器,用来发送所有传入的信息到所有连接的客户端。
  • WebClient – 查询网络,并通过串口监视器得到答案
  • WebClientRepeating – 如何用以太网shield重复HTTP请求。
  • WebServer – 一个简单的Web服务器,用来显示模拟输入的值。
  • DhcpAddressPrinter – 获取DHCP地址,并打印出到串口监视器。
  • DhcpChatServer – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。用DHCP。
  • TelnetClient – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。
  • BarometricPressureWebServer – 用SPI从压力传感器读取的Post数据。
  • UDPSendReceiveString – 通过UDP协议(通用数据包)发送和接收文本字符串。
  • UdpNtpClient – 查询一个网络时间协议(NTP)服务器,并通过串口服务器监视器获取这个信息。

您必须 登录 才能发表评论!