PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DhcpChatServer.ino 3.0KB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. DHCP 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. THis version attempts to get an IP address using DHCP
  8. Circuit:
  9. * Ethernet shield attached to pins 10, 11, 12, 13
  10. created 21 May 2011
  11. modified 9 Apr 2012
  12. by Tom Igoe
  13. modified 02 Sept 2015
  14. by Arturo Guadalupi
  15. Based on ChatServer example by David A. Mellis
  16. */
  17. #include <SPI.h>
  18. #include <NativeEthernet.h>
  19. // Enter a MAC address and IP address for your controller below.
  20. // The IP address will be dependent on your local network.
  21. // gateway and subnet are optional:
  22. byte mac[] = {
  23. 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
  24. };
  25. IPAddress ip(192, 168, 1, 177);
  26. IPAddress myDns(192, 168, 1, 1);
  27. IPAddress gateway(192, 168, 1, 1);
  28. IPAddress subnet(255, 255, 0, 0);
  29. // telnet defaults to port 23
  30. EthernetServer server(23);
  31. boolean gotAMessage = false; // whether or not you got a message from the client yet
  32. void setup() {
  33. // You can use Ethernet.init(pin) to configure the CS pin
  34. //Ethernet.init(10); // Most Arduino shields
  35. //Ethernet.init(5); // MKR ETH shield
  36. //Ethernet.init(0); // Teensy 2.0
  37. //Ethernet.init(20); // Teensy++ 2.0
  38. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  39. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  40. // Open serial communications and wait for port to open:
  41. Serial.begin(9600);
  42. while (!Serial) {
  43. ; // wait for serial port to connect. Needed for native USB port only
  44. }
  45. // start the Ethernet connection:
  46. Serial.println("Trying to get an IP address using DHCP");
  47. if (Ethernet.begin(mac) == 0) {
  48. Serial.println("Failed to configure Ethernet using DHCP");
  49. // Check for Ethernet hardware present
  50. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  51. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  52. while (true) {
  53. delay(1); // do nothing, no point running without Ethernet hardware
  54. }
  55. }
  56. if (Ethernet.linkStatus() == LinkOFF) {
  57. Serial.println("Ethernet cable is not connected.");
  58. }
  59. // initialize the Ethernet device not using DHCP:
  60. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  61. }
  62. // print your local IP address:
  63. Serial.print("My IP address: ");
  64. Serial.println(Ethernet.localIP());
  65. // start listening for clients
  66. server.begin();
  67. }
  68. void loop() {
  69. // wait for a new client:
  70. EthernetClient client = server.available();
  71. // when the client sends the first byte, say hello:
  72. if (client) {
  73. if (!gotAMessage) {
  74. Serial.println("We have a new client");
  75. client.println("Hello, client!");
  76. gotAMessage = true;
  77. }
  78. // read the bytes incoming from the client:
  79. char thisChar = client.read();
  80. // echo the bytes back to the client:
  81. server.write(thisChar);
  82. // echo the bytes to the server as well:
  83. Serial.print(thisChar);
  84. Ethernet.maintain();
  85. }
  86. }