daily pastebin goal
16%
SHARE
TWEET

Arduino simple webserver2

copko Jan 17th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #define LED1 3
  4. #define LED2 5
  5.  
  6. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC adresa
  7. IPAddress ip(192, 168, 1, 177); //nahradna IP
  8.  
  9. EthernetServer server(80); //vytvorenie objektu pre HTTP(80) server
  10.  
  11. void setup() {
  12.   Serial.begin(9600); //seriova linka pre debugovacie ucely
  13.   pinMode(LED1, OUTPUT);
  14.   pinMode(LED2, OUTPUT);
  15.  
  16.   if(!Ethernet.begin(mac)) //ak sa nepodari nacitat IP cez DHCP
  17.     Ethernet.begin(mac, ip); //nastavime ju na pevno
  18.   server.begin(); //spustime pocuvanie servera na poziadavky klientov
  19.   Serial.print("Server ma IP adresu ");
  20.   Serial.println(Ethernet.localIP());
  21. }
  22.  
  23. void loop(){
  24.   String buffer=""; //buffer pre prijimanie znakov
  25.   char c; //premenna pre nacitanie znaku
  26.   EthernetClient client = server.available(); //ak server detegoval nejake prichadzajuce spojenie, vytvori klienta pre odosielanie dat
  27.  
  28.   while (client.connected()) { //pokial je klient pripojeny
  29.     if(client.available()){ //ak este existuju data poslane od klienta
  30.        c=client.read(); //nacitaj jeden znak z tychto dat
  31.        if(c!='\n') buffer+=c; //ak to nie je znak konca riadku, pridaj ho do buffra
  32.        else { //ak to je znak konca riadku
  33.          if(buffer.indexOf('\r')==0) { //zisti, ci pred tym prijaty znak \r v buffri je na prvom mieste (index 0)
  34.           client.println("HTTP/1.1 200 OK"); //posli HTTP response
  35.           client.println("Content-Type: text/html");
  36.           client.println("Connection: close");
  37.           client.println();
  38.          
  39.           client.println("<!DOCTYPE HTML>");
  40.           client.println("<html>");
  41.           client.println("<head>");
  42.           client.println("<meta charset=\"UTF-8\">");
  43.           client.println("<title>Ovl&aacute;danie LED</title>");
  44.           client.println("</head>");
  45.          
  46.           client.println("<body>");
  47.           client.println(" <form method=\"GET\">");
  48.           client.println("  LED1:");
  49.           client.println("   <input type=\"radio\" name=\"led1\" value=\"1\" id=\"led1on\">");
  50.           client.println("   <label for=\"led1on\">zapn&uacute;&tcaron;</label>");
  51.           client.println("   <input type=\"radio\" name=\"led1\" value=\"0\" id=\"led1off\">");
  52.           client.println("   <label for=\"led1off\">vypn&uacute;&tcaron;</label><br>");
  53.              
  54.           client.println("  LED2:");
  55.           client.println("   <input type=\"radio\" name=\"led2\" value=\"1\" id=\"led2on\">");
  56.           client.println("   <label for=\"led2on\">zapn&uacute;&tcaron;</label>");
  57.           client.println("   <input type=\"radio\" name=\"led2\" value=\"0\" id=\"led2off\">");
  58.           client.println("   <label for=\"led2off\">vypn&uacute;&tcaron;</label><br>");
  59.  
  60.           client.println("  <input type=\"Submit\">");
  61.           client.println(" </form>");
  62.  
  63.           client.println("</body>");
  64.           client.println("</html>");
  65.  
  66.           client.stop();
  67.           }
  68.          else {
  69.           if(buffer.startsWith("GET")){
  70.             if(buffer.indexOf("led1=1")>=0) digitalWrite(LED1,HIGH);
  71.             if(buffer.indexOf("led1=0")>=0) digitalWrite(LED1,LOW);
  72.             if(buffer.indexOf("led2=1")>=0) digitalWrite(LED2,HIGH);
  73.             if(buffer.indexOf("led2=0")>=0) digitalWrite(LED2,LOW);
  74.             }
  75.          }
  76.          buffer="";
  77.        }
  78.     }
  79.   }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top