PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ChatServer.ino 2.7KB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Chat Server
  3. A simple server that distributes any incoming messages to all
  4. connected clients. To use, telnet to your device's IP address and type.
  5. You can see the client's input in the serial monitor as well.
  6. Using an Arduino Wiznet Ethernet shield.
  7. Circuit:
  8. * Ethernet shield attached to pins 10, 11, 12, 13
  9. created 18 Dec 2009
  10. by David A. Mellis
  11. modified 9 Apr 2012
  12. by Tom Igoe
  13. */
  14. #include <SPI.h>
  15. #include <NativeEthernet.h>
  16. // Enter a MAC address and IP address for your controller below.
  17. // The IP address will be dependent on your local network.
  18. // gateway and subnet are optional:
  19. byte mac[] = {
  20. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  21. IPAddress ip(192, 168, 1, 177);
  22. IPAddress myDns(192, 168, 1, 1);
  23. IPAddress gateway(192, 168, 1, 1);
  24. IPAddress subnet(255, 255, 0, 0);
  25. // telnet defaults to port 23
  26. EthernetServer server(23);
  27. boolean alreadyConnected = false; // whether or not the client was connected previously
  28. void setup() {
  29. // You can use Ethernet.init(pin) to configure the CS pin
  30. //Ethernet.init(10); // Most Arduino shields
  31. //Ethernet.init(5); // MKR ETH shield
  32. //Ethernet.init(0); // Teensy 2.0
  33. //Ethernet.init(20); // Teensy++ 2.0
  34. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  35. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  36. // initialize the ethernet device
  37. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  38. // Open serial communications and wait for port to open:
  39. Serial.begin(9600);
  40. while (!Serial) {
  41. ; // wait for serial port to connect. Needed for native USB port only
  42. }
  43. // Check for Ethernet hardware present
  44. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  45. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  46. while (true) {
  47. delay(1); // do nothing, no point running without Ethernet hardware
  48. }
  49. }
  50. if (Ethernet.linkStatus() == LinkOFF) {
  51. Serial.println("Ethernet cable is not connected.");
  52. }
  53. // start listening for clients
  54. server.begin();
  55. Serial.print("Chat server address:");
  56. Serial.println(Ethernet.localIP());
  57. }
  58. void loop() {
  59. // wait for a new client:
  60. EthernetClient client = server.available();
  61. // when the client sends the first byte, say hello:
  62. if (client) {
  63. if (!alreadyConnected) {
  64. // clear out the input buffer:
  65. client.flush();
  66. Serial.println("We have a new client");
  67. client.println("Hello, client!");
  68. alreadyConnected = true;
  69. }
  70. if (client.available() > 0) {
  71. // read the bytes incoming from the client:
  72. char thisChar = client.read();
  73. // echo the bytes back to the client:
  74. server.write(thisChar);
  75. // echo the bytes to the server as well:
  76. Serial.write(thisChar);
  77. }
  78. }
  79. }