Advertisement
Guest User

Untitled

a guest
May 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <ESPmDNS.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5.  
  6. #define LED 2
  7. #define INTERVAL 1000
  8.  
  9. unsigned long int timer;
  10.  
  11. const char *ssid = "Your SSID here";
  12. const char *password = "Your password here";
  13.  
  14. void setup()
  15. {
  16. Serial.begin(115200);
  17. Serial.println("Booting");
  18. pinMode(LED,OUTPUT);
  19.  
  20. WiFi.mode(WIFI_STA);
  21. WiFi.begin(ssid, password);
  22. while (WiFi.waitForConnectResult() != WL_CONNECTED)
  23. {
  24. Serial.println("Connection Failed! Rebooting...");
  25. delay(5000);
  26. ESP.restart();
  27. }
  28.  
  29. // Port defaults to 3232
  30. // ArduinoOTA.setPort(3232);
  31.  
  32. // Hostname defaults to esp3232-[MAC]
  33. // ArduinoOTA.setHostname("myesp32");
  34.  
  35. // No authentication by default
  36. // ArduinoOTA.setPassword("admin");
  37.  
  38. // Password can be set with it's md5 value as well
  39. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  40. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  41.  
  42. ArduinoOTA
  43. .onStart([]() {
  44. String type;
  45. if (ArduinoOTA.getCommand() == U_FLASH)
  46. type = "sketch";
  47. else // U_SPIFFS
  48. type = "filesystem";
  49.  
  50. // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  51. Serial.println("Start updating " + type);
  52. })
  53. .onEnd([]() {
  54. Serial.println("\nEnd");
  55. })
  56. .onProgress([](unsigned int progress, unsigned int total) {
  57. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  58. })
  59. .onError([](ota_error_t error) {
  60. Serial.printf("Error[%u]: ", error);
  61. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  62. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  63. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  64. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  65. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  66. });
  67.  
  68. ArduinoOTA.begin();
  69.  
  70. Serial.println("Ready");
  71. Serial.print("IP address: ");
  72. Serial.println(WiFi.localIP());
  73. }
  74.  
  75. void loop()
  76. {
  77. ArduinoOTA.handle();
  78.  
  79. if(millis()-timer>=INTERVAL)
  80. {
  81. timer=millis();
  82. digitalWrite(LED,!digitalRead(LED));
  83. if(digitalRead(LED)==LOW) Serial.println("LED OFF");
  84. else Serial.println("LED ON");
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement