Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /*
  2. Web Server
  3.  
  4. */
  5.  
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8. #include <EthernetUdp.h>
  9.  
  10.  
  11.  
  12. void Color(int R, int G, int B) //set up for the RGB led
  13. {
  14. analogWrite(3, R) ; // Rojo
  15. analogWrite(5, G) ; // Green - Verde
  16. analogWrite(6, B) ; // Blue - Azul
  17. }
  18.  
  19. // Enter a MAC address and IP address for your controller below.
  20. // The IP address will be dependent on your local network:
  21. byte mac[] = {
  22. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  23. IPAddress ip(10, 90, 111, 150);
  24.  
  25. unsigned int localPort = 8888; // local port to listen on
  26.  
  27. // buffers for receiving and sending data
  28. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
  29. char ReplyBuffer[] = "acknowledged"; // a string to send back
  30.  
  31. // An EthernetUDP instance to let us send and receive packets over UDP
  32. EthernetUDP Udp;
  33.  
  34. // Initialize the Ethernet server library
  35. // with the IP address and port you want to use
  36. // (port 80 is default for HTTP):
  37. EthernetServer server(80);
  38.  
  39. void setup() { //seting up the outputs for the rgb
  40. pinMode(3, OUTPUT);
  41. pinMode(5, OUTPUT);
  42. pinMode(6, OUTPUT);
  43.  
  44.  
  45.  
  46. // You can use Ethernet.init(pin) to configure the CS pin
  47. //Ethernet.init(10); // Most Arduino shields
  48. //Ethernet.init(5); // MKR ETH shield
  49. //Ethernet.init(0); // Teensy 2.0
  50. //Ethernet.init(20); // Teensy++ 2.0
  51. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  52. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  53.  
  54.  
  55. // start the Ethernet connection and the server:
  56. Ethernet.begin(mac, ip);
  57.  
  58. // Open serial communications and wait for port to open:
  59. Serial.begin(9600);
  60. while (!Serial) {
  61. ; // wait for serial port to connect. Needed for native USB port only
  62. }
  63. Serial.println("Ethernet WebServer Example");
  64.  
  65. // Check for Ethernet hardware present
  66. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  67. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  68. while (true) {
  69. delay(1); // do nothing, no point running without Ethernet hardware
  70. }
  71. }
  72. if (Ethernet.linkStatus() == LinkOFF) {
  73. Serial.println("Ethernet cable is not connected.");
  74. }
  75.  
  76. // start the server
  77. Udp.begin(localPort);
  78. server.begin();
  79. Serial.print("server is at ");
  80. Serial.println(Ethernet.localIP());
  81. }
  82.  
  83.  
  84. void loop() {
  85.  
  86.  
  87. // if there's data available, read a packet
  88. int packetSize = Udp.parsePacket();
  89. if (packetSize) {
  90. Serial.print("Received packet of size ");
  91. Serial.println(packetSize);
  92. Serial.print("From ");
  93. IPAddress remote = Udp.remoteIP();
  94. for (int i=0; i < 4; i++) {
  95. Serial.print(remote[i], DEC);
  96. if (i < 3) {
  97. Serial.print(".");
  98. }
  99. }
  100. Serial.print(", port ");
  101. Serial.println(Udp.remotePort());
  102.  
  103. // read the packet into packetBufffer
  104. Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  105. Serial.println("Contents:");
  106. Serial.println(packetBuffer);
  107.  
  108. // send a reply to the IP address and port that sent us the packet we received
  109. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  110. Udp.write(ReplyBuffer);
  111. Udp.endPacket();
  112.  
  113. }
  114. char numero = packetBuffer;
  115. if (numero = "1") Color(250, 0, 0) ;
  116. if (numero = "2") Color(100, 110, 0);
  117. if (numero = "3") Color(0, 255, 0);
  118. delay(10);
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement