Arduino库教程-Ethernet-Advanced Chat Server
AdvancedChat Server
- 这是一个更精致的服务器,可以将任何传入的消息分发给所有连接的客户端(除了发送消息的那个客户端)。使用时,打开一个终端窗口,远程登录到您的设备的IP地址,并打字输入。任何传入的文本将被发送到所有连接的客户端(包括正在打字的这个客户端)。此外,您将同样能够看到在您的串行监视器上客户端的输入。
硬件要求
- Arduino 或者 Genuino 开发板
- Arduino Ethernet Shield
电路
以太网shield允许你通过SPI总线连接一个WizNet Ethernet 处理器到Arduono或者Genuino开发板。它使用SPI总线连接引脚pin 10,11,12,和13,到Wiznet。以太网shield后来的模块也有一个SD卡在板上。数字引脚 pin 4 用来控制SD卡上的从选择引脚(slave select pin)。
shield应该连接到一个有以太网电缆的网络。您将需要更改程序中的网络设置来对应于您的网络。
图由 Fritzing 软件绘制在上面的图片里,Arduino或genuino开发板会堆叠在以太网shield下面。
原理图
样例代码
/*
Advanced Chat Server
A more advanced server that distributes any incoming messages
to all connected clients but the client the message comes from.
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
redesigned to make use of operator== 25 Nov 2013
by Norbert Truchsess
*/
#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);
EthernetClient clients[4];
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) {
boolean newClient = true;
for (byte i = 0; i < 4; i++) {
//check whether this client refers to the same socket as one of the existing instances:
if (clients[i] == client) {
newClient = false;
break;
}
}
if (newClient) {
//check which of the existing clients can be overridden:
for (byte i = 0; i < 4; i++) {
if (!clients[i] && clients[i] != client) {
clients[i] = client;
// clear out the input buffer:
client.flush();
Serial.println("We have a new client");
client.print("Hello, client number: ");
client.print(i);
client.println();
break;
}
}
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to all other connected clients:
for (byte i = 0; i < 4; i++) {
if (clients[i] && (clients[i] != client)) {
clients[i].write(thisChar);
}
}
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
for (byte i = 0; i < 4; i++) {
if (!(clients[i].connected())) {
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
clients[i].stop();
}
}
}
[Get Code]
更多
- Arduino Ethernet Shield – 产品描述。
- Getting started with the Ethernet Shield – 在几分钟内启动所有东西。
- Ethernet library – 以太网库的参考手册
- ChatServer - 一个简单的服务器,用来发送所有传入的信息到所有连接的客户端。
- WebClient – 查询网络,并通过串口监视器得到答案
- WebClientRepeating - 如何用以太网shield重复HTTP请求。
- WebServer - 一个简单的Web服务器,用来显示模拟输入的值。
- DhcpAddressPrinter – 获取DHCP地址,并打印出到串口监视器。
- DhcpChatServer – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。用DHCP。
- TelnetClient - 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。
- BarometricPressureWebServer – 用SPI从压力传感器读取的Post数据。
- UDPSendReceiveString - 通过UDP协议(通用数据包)发送和接收文本字符串。
- UdpNtpClient - 查询一个网络时间协议(NTP)服务器,并通过串口服务器监视器获取这个信息。
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号