Advertisement
DrAungWinHtut

ethernet shield.ino

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