daily pastebin goal
49%
SHARE
TWEET

ESP01 nacteni z webu

a guest Apr 24th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <WiFiUDP.h>
  4.  
  5. const char* ssid = "SSID";
  6. const char* password = "HESLO";
  7.  
  8. // WOL
  9. int getDelay = 15000;
  10. long getTime = 0;
  11. const byte targetMacAddress[] = { 0x14, 0xDA, 0xE9, 0x29, 0xD3, 0xF4 };
  12. const byte targetIPAddress[] = { 192, 168, 1, 255 };
  13. const int targetWOLPort = 9;
  14. const unsigned int localUdpPort = 12345;
  15. WiFiUDP Udp;
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   delay(10);
  20.  
  21.   // Connect to WiFi network
  22.   //Serial.println();
  23.   //Serial.print(F("Pripojuji se k "));
  24.   //Serial.println(ssid);
  25.  
  26.   WiFi.mode(WIFI_STA);
  27.   WiFi.begin(ssid, password);
  28.  
  29.   while (WiFi.status() != WL_CONNECTED) {
  30.     delay(500);
  31.     //Serial.print(F("."));
  32.     if (millis() > 10000) {
  33.       goto Preskoc;
  34.     }
  35.   }
  36.   //Serial.println("");
  37.   //Serial.println(F("WiFi pripojena"));
  38.  
  39. Preskoc:
  40.   // Print the IP address
  41.   //Serial.println(WiFi.localIP());
  42. }
  43.  
  44. void loop() {
  45.   // Načte WOL příkaz z webu
  46.   if (millis() - getTime > getDelay) {
  47.     if (WiFi.status() == WL_CONNECTED) {
  48.       HTTPClient http;
  49.       http.begin("ADRESA ODKUD NACTE POZADAVEK");
  50.       int httpCode = http.GET();
  51.       //Serial.println(httpCode);
  52.       if (httpCode == 200) {
  53.         String data = http.getString();
  54.         //Serial.println(data);
  55.         if (data == "1") {
  56.           sendWOL();
  57.           delay(200);
  58.         }
  59.       }
  60.       http.end();
  61.     }
  62.     getTime = millis();
  63.   }
  64.  
  65. }
  66.  
  67. void sendWOL() {
  68.   const int magicPacketLength = 102;
  69.   byte magicPacket[magicPacketLength] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  70.  
  71.   for (int ix = 6; ix < magicPacketLength; ix++)
  72.     magicPacket[ix] = targetMacAddress[ix % 6];
  73.  
  74.   Udp.begin(localUdpPort);
  75.   Udp.beginPacket(targetIPAddress, targetWOLPort);
  76.   Udp.write(magicPacket, magicPacketLength);
  77.   Udp.endPacket();
  78.   Udp.stop();
  79. }
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