Advertisement
Guest User

Untitled

a guest
Oct 8th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <EthernetENC.h>
  4. //#include <avr\wdt.h> // watchdog knihovna
  5.  
  6. boolean zacatekCteni = false;
  7. unsigned long cas = millis();
  8. unsigned long reset = millis();
  9. byte citac = 0;
  10.  
  11. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  12. IPAddress ip(192, 168, 1, 11);
  13. EthernetServer mujSvr = EthernetServer(4501);
  14.  
  15. EthernetClient clientzkontroluj;
  16. IPAddress webCamAddr(192,168,1,34); // IP adresa webové kamery
  17.  
  18. String header;
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   Ethernet.begin(mac, ip);
  23.   mujSvr.begin();
  24.   Serial.println(Ethernet.localIP());
  25.  
  26.   digitalWrite(8, HIGH); // pin 8 na 1
  27.   pinMode(8,OUTPUT);     // nastavení pinu  8 na výstup
  28. }
  29. ///////////////////////////////////////////////////////////
  30. void loop() {
  31.   String poslane = "";
  32.   EthernetClient client = mujSvr.available();
  33.   if (client) {
  34.     boolean currentLineIsBlank = true;
  35.  
  36.     while (client.connected()) {
  37.       if (client.available()) {
  38.         char c = client.read();
  39.         header += c;
  40.  
  41.         if(zacatekCteni && c == ' '){zacatekCteni = false;} //ukončí čtení
  42.         if(c == '?'){zacatekCteni = true;} //začatek čtení
  43.         if(zacatekCteni){poslane = poslane + char(c);}
  44.        
  45.         if (c == '\n' && currentLineIsBlank) {
  46.            // // heslo je admin  admin
  47.            if(header.indexOf("YWRtaW46YWRtaW4=") >= 0) {
  48.             // při úspěšném přihlášení
  49.             client.println("HTTP/1.1 200 OK");
  50.             client.println("Content-Type: text/html");
  51.             client.println("Connection: close");  
  52.             client.println();
  53.            
  54.             //-------------------------------------
  55.             if (poslane == "?reboot")
  56.                {
  57.                 citac = 0;
  58.                 digitalWrite(8, LOW);
  59.                 reset = millis();
  60.                 client.println("restartuji zasuvku");
  61.                 Serial.print(poslane);
  62.                 }
  63.             //-------------------------------------
  64.  
  65.              }
  66.  
  67.            else {
  68.             // špatně zadane  user/pass
  69.             client.println("HTTP/1.0 401 Authorization Required");
  70.             client.println("HTTP/1.1 401 Unauthorized");
  71.             client.println("WWW-Authenticate: Basic realm=\"Secure\"");
  72.             client.println("Content-Type: text/html");
  73.             client.println();
  74.             client.println("<html>Text to send if user hits Cancel button</html>"); // really need this for the popup!
  75.           }
  76.  
  77.           header = "";
  78.           break;
  79.         }
  80.         if (c == '\n') {currentLineIsBlank = true;}
  81.         else
  82.         if (c != '\r') {currentLineIsBlank = false;}
  83.       }
  84.     }
  85.     delay(1);  
  86.     client.stop();
  87.      
  88.   }
  89.  
  90.  // ---------------------------------------------------------------------
  91.     if (millis() - reset > 5000)    
  92.     {
  93.         digitalWrite(8, HIGH);          
  94.     }
  95. // ---------------------------------------------------------------------
  96. // Každou minutu zavola proceduru zkontroluj
  97.    if (millis() - cas > 60000)    
  98.     {
  99.         Serial.print("kontrolují kameru ");
  100.         Serial.print(webCamAddr);
  101.         zkontroluj();
  102.         cas = millis();          
  103.     }
  104. // ---------------------------------------------------------------------
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////////
  107. void zkontroluj()
  108. {
  109.   clientzkontroluj.connect(webCamAddr, 8034);  // kamera na adrese 192,168,1,34 port 8034
  110.   delay(1000);  // počka vteřinu
  111.   if (clientzkontroluj.connected())
  112.      {
  113.       citac = 0;  // je-li odpoveď OK, vynulovat čítač
  114.       Serial.println(" - je tam");
  115.      }
  116.      else
  117.      {
  118.     // není odpověd
  119.        citac++; // zvyšovat čítač
  120.        Serial.println(" - není tam");
  121.      };
  122.    
  123.   clientzkontroluj.stop();   // spojení clientzkontroluj automaticky uzavře
  124.  
  125.   if (citac == 5)
  126.           {
  127.            Serial.println("restartuji zasuvku");
  128.            digitalWrite(8, LOW);
  129.            reset = millis();
  130.            citac = 0;
  131.           }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement