Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Web Server
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- void Color(int R, int G, int B) //set up for the RGB led
- {
- analogWrite(3, R) ; // Rojo
- analogWrite(5, G) ; // Green - Verde
- analogWrite(6, B) ; // Blue - Azul
- }
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- IPAddress ip(10, 90, 111, 150);
- unsigned int localPort = 8888; // local port to listen on
- // buffers for receiving and sending data
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
- char ReplyBuffer[] = "acknowledged"; // a string to send back
- // An EthernetUDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- // Initialize the Ethernet server library
- // with the IP address and port you want to use
- // (port 80 is default for HTTP):
- EthernetServer server(80);
- void setup() { //seting up the outputs for the rgb
- pinMode(3, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- // You can use Ethernet.init(pin) to configure the CS pin
- //Ethernet.init(10); // Most Arduino shields
- //Ethernet.init(5); // MKR ETH shield
- //Ethernet.init(0); // Teensy 2.0
- //Ethernet.init(20); // Teensy++ 2.0
- //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
- //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip);
- // 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.println("Ethernet WebServer Example");
- // Check for Ethernet hardware present
- if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- while (true) {
- delay(1); // do nothing, no point running without Ethernet hardware
- }
- }
- if (Ethernet.linkStatus() == LinkOFF) {
- Serial.println("Ethernet cable is not connected.");
- }
- // start the server
- Udp.begin(localPort);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // if there's data available, read a packet
- int packetSize = Udp.parsePacket();
- if (packetSize) {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remote = Udp.remoteIP();
- for (int i=0; i < 4; i++) {
- Serial.print(remote[i], DEC);
- if (i < 3) {
- Serial.print(".");
- }
- }
- Serial.print(", port ");
- Serial.println(Udp.remotePort());
- // read the packet into packetBufffer
- Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
- Serial.println("Contents:");
- Serial.println(packetBuffer);
- // send a reply to the IP address and port that sent us the packet we received
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write(ReplyBuffer);
- Udp.endPacket();
- }
- char numero = packetBuffer;
- if (numero = "1") Color(250, 0, 0) ;
- if (numero = "2") Color(100, 110, 0);
- if (numero = "3") Color(0, 255, 0);
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement