最新消息:

Arduino库教程-Ethernet-Chat Server

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

Chat Server

    • 这是一个简单的服务器,可以将任何传入的消息分发给所有连接的客户端(除了发送消息的那个客户端)。使用时,打开一个终端窗口,远程登录到您的设备的IP地址,并打字输入。任何传入的文本将被发送到所有连接的客户端(包括正在打字的这个客户端)。此外,您将同样能够看到在您的串行监视器上客户端的输入。

硬件要求

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

电路

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

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

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

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

样例代码

/*
 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.

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

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 */

#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[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
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 alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  // start listening for clients
  server.begin();
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

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

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

    if (client.available() > 0) {
      // 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.write(thisChar);
    }
  }
}

[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)服务器,并通过串口服务器监视器获取这个信息。

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