{"id":3848,"date":"2021-04-22T15:49:03","date_gmt":"2021-04-22T13:49:03","guid":{"rendered":"https:\/\/arduino.net.pl\/?p=3848"},"modified":"2021-05-01T15:05:41","modified_gmt":"2021-05-01T13:05:41","slug":"usypianie-esp32","status":"publish","type":"post","link":"https:\/\/arduino.net.pl\/index.php\/usypianie-esp32\/","title":{"rendered":"Usypianie ESP32"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Usypianie i budzenie za pomoc\u0105 funkcji touchAttachInterrupt(). <\/h3>\n\n\n\n<p>\u0179r\u00f3d\u0142o: <a href=\"https:\/\/www.youtube.com\/watch?v=oGdKPxXJtL8\">Touch Pins as Wake-up Source (ESP32 + Arduino series)<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Argumenty funkcji<strong> touchAttachInterrupt<\/strong>():<ol><li>uint8_t pin. W moim przypadku T0 to pin GPIO0. Nazwy pin\u00f3w dotykowych mo\u017cemy odczyta\u0107 z diagram\u00f3w p\u0142ytek ESP32.<\/li><li>void (*)() userFunc. U mnie <em>Callback<\/em> to funkcja wykonywana po wybudzeniu<\/li><li>uint16_t threshold. TOUCH_THRESHOLD zdefiniowana czu\u0142o\u015b\u0107 na dotyk. Czym wy\u017csza warto\u015b\u0107, tym czulszy na dotyk.<\/li><\/ol><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">KOD<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/\/#include &lt;Arduino.h&gt;\n#define TOUCH_THRESHOLD 40\n\nvoid callback(){}\n\nvoid setup() {\n  Serial.begin(115200);\n  delay(1000); \/\/Take some time to open up the Serial Monitor\n  Serial.println(&quot;ESP32 has started&quot;);\n\n  touchAttachInterrupt(T0, callback, TOUCH_THRESHOLD);\n  esp_sleep_enable_touchpad_wakeup();\n\n  esp_deep_sleep_start();\n}\n\nvoid loop() {\n  \/\/ put your main code here, to run repeatedly:\n\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Usypianie i budzenie z (jednego) zewn\u0119trznego \u017ar\u00f3d\u0142a:<\/h3>\n\n\n\n<p>\u0179r\u00f3d\u0142o: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-external-wake-up-deep-sleep\/\">ESP32 External Wake Up from Deep Sleep<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2021\/04\/Zrzut-ekranu-2021-04-30-o-20.42.01.png\"><img loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"819\" src=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2021\/04\/Zrzut-ekranu-2021-04-30-o-20.42.01.png\" alt=\"\" class=\"wp-image-4362\" srcset=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2021\/04\/Zrzut-ekranu-2021-04-30-o-20.42.01.png 749w, https:\/\/arduino.net.pl\/wp-content\/uploads\/2021\/04\/Zrzut-ekranu-2021-04-30-o-20.42.01-274x300.png 274w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/a><\/figure>\n\n\n\n<p>Aby u\u017cy\u0107 (jednego) zewn\u0119trznego \u017ar\u00f3d\u0142a budzenia, nale\u017cy u\u017cy\u0107 nast\u0119puj\u0105cej funkcji:<br><code>esp_sleep_enable_ext0_wakeup(GPIO_NUM_X, level)<\/code><br>Przyjmuje ona jako pierwszy argument numer pinu, kt\u00f3ry ma by\u0107 u\u017cyty do budzenia, w tym formacie <strong>GPIO_NUM_X<\/strong>, gdzie <strong>X<\/strong> reprezentuje numer GPIO tego pinu.<br>Drugi argument to poziom sygna\u0142u wej\u015bciowego, mo\u017ce by\u0107 albo 1 (HIGH) albo 0 (LOW). Reprezentuje stan GPIO, kt\u00f3ry spowoduje wybudzenie.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">KOD<\/h3>\n\n\n\n<p>Do kodu doda\u0142em w\u0142\u0105czanie wbudowanej diody po wybudzeniu.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/*\nDeep Sleep with External Wake Up\n=====================================\nThis code displays how to use deep sleep with\nan external trigger as a wake up source and how\nto store data in RTC memory to use it over reboots\n\nThis code is under Public Domain License.\n\nHardware Connections\n======================\nPush Button to GPIO 34 pulled down with a 10K Ohm\nresistor\n\nNOTE:\n======\nOnly RTC IO can be used as a source for external wake\nsource. They are pins: 0,2,4,12-15,25-27,32-39.\n\nAuthor:\nPranav Cherukupalli &lt;cherukupallip@gmail.com&gt;\n*\/\n\n#ifdef ESP32\n#define LED_BUILTIN 2\n#elif ESP8266\n#define LED_BUILTIN D4\n#endif\n\n\/\/#define BUTTON_PIN_BITMASK 0x200000000 \/\/ 2^33 in hex\n\/\/#define BUTTON_PIN_BITMASK 0x400000000 \/\/ 2^34 in hex \n\nRTC_DATA_ATTR int bootCount = 0;\n\n\/*\nMethod to print the reason by which ESP32\nhas been awaken from sleep\n*\/\nvoid print_wakeup_reason(){\n  esp_sleep_wakeup_cause_t wakeup_reason;\n\n  wakeup_reason = esp_sleep_get_wakeup_cause();\n\n  switch(wakeup_reason)\n  {\n    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println(&quot;Wakeup caused by external signal using RTC_IO&quot;); break;\n    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println(&quot;Wakeup caused by external signal using RTC_CNTL&quot;); break;\n    case ESP_SLEEP_WAKEUP_TIMER : Serial.println(&quot;Wakeup caused by timer&quot;); break;\n    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println(&quot;Wakeup caused by touchpad&quot;); break;\n    case ESP_SLEEP_WAKEUP_ULP : Serial.println(&quot;Wakeup caused by ULP program&quot;); break;\n    default : Serial.printf(&quot;Wakeup was not caused by deep sleep: %d\\n&quot;,wakeup_reason); break;\n  }\n}\n\nvoid setup(){\n  Serial.begin(115200);\n  delay(1000); \/\/Take some time to open up the Serial Monitor\n  pinMode(LED_BUILTIN, OUTPUT);\n\n  \/\/Increment boot number and print it every reboot\n  ++bootCount;\n  Serial.println(&quot;Boot number: &quot; + String(bootCount));\n\n  \/\/Print the wakeup reason for ESP32\n  print_wakeup_reason();\n\n  \/*\n  First we configure the wake up source\n  We set our ESP32 to wake up for an external trigger.\n  There are two types for ESP32, ext0 and ext1 .\n  ext0 uses RTC_IO to wakeup thus requires RTC peripherals\n  to be on while ext1 uses RTC Controller so doesnt need\n  peripherals to be powered on.\n  Note that using internal pullups\/pulldowns also requires\n  RTC peripherals to be turned on.\n  *\/\n  esp_sleep_enable_ext0_wakeup(GPIO_NUM_34,1); \/\/1 = High, 0 = Low\n\n  \/\/If you were to use ext1, you would use it like\n  \/\/esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);\n\n  \/\/Go to sleep now\n  Serial.println(&quot;Going to sleep now&quot;);\n  digitalWrite(2, HIGH);\n  delay(3000);\n  digitalWrite(2, LOW);\n  esp_deep_sleep_start();\n  Serial.println(&quot;This will never be printed&quot;);\n}\n\nvoid loop(){\n  \/\/This is not going to be called\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">3. Kilka \u017ar\u00f3de\u0142 budzenia<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">ext1<\/h3>\n\n\n\n<p>W poprzednim skeczu u\u017cyli\u015bmy funkcji:<br><code>esp_sleep_enable_ext0_wakeup();<\/code><br>kt\u00f3ra pozwala kontrolowa\u0107 jedno \u017ar\u00f3d\u0142o wybudzenia.<br>Je\u015bli mamy kilka czujnik\u00f3w i chcemy by procesor wykonywa\u0142 r\u00f3\u017cne zadania w zale\u017cno\u015bci od \u017ar\u00f3d\u0142a wybudzenia, musimy u\u017cy\u0107 funkcji:<br><code>esp_sleep_enable_ext1_wakeup();<\/code><br>Funkcja ma dwa parametry\/argumenty. Pierwszy <em>mask<\/em>, drugi <em>mode<\/em>.<br>Argument <em>mask<\/em> informuje kt\u00f3re piny bior\u0105 udzia\u0142 przy wybudzaniu. Je\u015bli pin by\u0142by jeden, na przyk\u0142ad chcemy u\u017cy\u0107 GPIO34, w\u00f3wczas parametr <em>mask<\/em> przyjmie warto\u015b\u0107:<br><code>0x400000000.<\/code><br>Obliczamy go tak: 2^34 = 17179869184 nast\u0119pnie zamieniamy na liczb\u0119 w systemie szesnastkowym. Mo\u017cemy skorzysta\u0107 z konwertera:<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-fill\"><a class=\"wp-block-button__link\" href=\"https:\/\/www.rapidtables.com\/convert\/number\/decimal-to-hex.html\">Konwerter DEC -> HEX<\/a><\/div>\n<\/div>\n\n\n\n<p><code>#define PIN_BITMASK 0x400000000 \/\/ 2^34 in HEX <\/code><br>Je\u015bli chcemy wykorzysta\u0107 <strong>dwa piny<\/strong>, na przyk\u0142ad <strong>GPIO33 i GPIO34<\/strong> w\u00f3wczas musimy doda\u0107 <strong>2^33<\/strong> do <strong>2^34<\/strong>. Otrzymamy <strong>25769803776<\/strong>. Po zamianie na liczb\u0119 w systemie <strong>szesnastkowym<\/strong> otrzymamy <strong>600000000<\/strong>.<br><code>#define PIN_BITMASK 0x600000000 \/\/ 2^33+2^34 in HEX<\/code><\/p>\n\n\n\n<p class=\"has-large-font-size\"><span style=\"color:#FFFDFF\" class=\"tadv-color\"><span style=\"background-color:#444\" class=\"tadv-background-color\">Pami\u0119tajmy, \u017ce do wybudzania mo\u017cemy u\u017cy\u0107 pin\u00f3w: 0, 2, 4, 12-15, 25-27, 32-39.<\/span><\/span><\/p>\n\n\n\n<p>Poni\u017cej kod. \u017beby by\u0142o trudniej piny budzenia to <strong>GPIO4<\/strong> i <strong>GPIO15<\/strong>. Maska przyjmuje warto\u015b\u0107 <strong>0x8010<\/strong>. Do pinu 4 pod\u0142\u0105czy\u0142em czujnik przechy\u0142u, do pinu 15 mikrofalowy czujnik ruchu. Oba \u015bwietnie wybudzaj\u0105.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">KOD<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/*\nDeep Sleep with External Wake Up\n=====================================\nThis code displays how to use deep sleep with\nan external trigger as a wake up source and how\nto store data in RTC memory to use it over reboots\n\nThis code is under Public Domain License.\n\nHardware Connections\n======================\nPush Button to GPIO 33 pulled down with a 10K Ohm\nresistor\n\nNOTE:\n======\nOnly RTC IO can be used as a source for external wake\nsource. They are pins: 0,2,4,12-15,25-27,32-39.\n\nAuthor:\nPranav Cherukupalli &amp;lt;cherukupallip@gmail.com&gt;\n*\/\n\n#define LED_BUILTIN 2\n\n#define PIN_BITMASK 0x8010 \/\/ GPIOs 4 and 15\n\/\/#define PIN_BITMASK 0x300000000000 \/\/ 2^33+2^34 in HEX\n\nRTC_DATA_ATTR int bootCount = 0;\n\n\/*\nMethod to print the reason by which ESP32\nhas been awaken from sleep\n*\/\nvoid print_wakeup_reason(){\n  esp_sleep_wakeup_cause_t wakeup_reason;\n\n  wakeup_reason = esp_sleep_get_wakeup_cause();\n\n  switch(wakeup_reason)\n  {\n    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println(&quot;Wakeup caused by external signal using RTC_IO&quot;); break;\n    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println(&quot;Wakeup caused by external signal using RTC_CNTL&quot;); break;\n    case ESP_SLEEP_WAKEUP_TIMER : Serial.println(&quot;Wakeup caused by timer&quot;); break;\n    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println(&quot;Wakeup caused by touchpad&quot;); break;\n    case ESP_SLEEP_WAKEUP_ULP : Serial.println(&quot;Wakeup caused by ULP program&quot;); break;\n    default : Serial.printf(&quot;Wakeup was not caused by deep sleep: %d\\n&quot;,wakeup_reason); break;\n  }\n}\n\n\/*\nMethod to print the GPIO that triggered the wakeup\n*\/\nvoid print_GPIO_wake_up(){\n  int GPIO_reason = esp_sleep_get_ext1_wakeup_status();\n  Serial.print(&quot;GPIO that triggered the wake up: GPIO &quot;);\n  Serial.println((log(GPIO_reason))\/log(2), 0);\n}\n  \nvoid setup(){\n  Serial.begin(115200);\n  delay(1000); \/\/Take some time to open up the Serial Monitor\n  pinMode(LED_BUILTIN, OUTPUT);\n  \/\/Increment boot number and print it every reboot\n  ++bootCount;\n  Serial.println(&quot;Boot number: &quot; + String(bootCount));\n\n  \/\/Print the wakeup reason for ESP32\n  print_wakeup_reason();\n\n  \/\/Print the GPIO used to wake up\n  print_GPIO_wake_up();\n\n  \/*\n  First we configure the wake up source\n  We set our ESP32 to wake up for an external trigger.\n  There are two types for ESP32, ext0 and ext1 .\n  ext0 uses RTC_IO to wakeup thus requires RTC peripherals\n  to be on while ext1 uses RTC Controller so doesnt need\n  peripherals to be powered on.\n  Note that using internal pullups\/pulldowns also requires\n  RTC peripherals to be turned on.\n  *\/\n  \/\/esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_15,1); \/\/1 = High, 0 = Low\n\n  \/\/If you were to use ext1, you would use it like\n  esp_sleep_enable_ext1_wakeup(PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);\n\n  \/\/Go to sleep now\n  Serial.println(&quot;Going to sleep now&quot;);\n  digitalWrite(2, HIGH);\n  delay(3000);\n  digitalWrite(2, LOW);\n  esp_deep_sleep_start();\n  Serial.println(&quot;This will never be printed&quot;);\n}\n\nvoid loop(){\n  \/\/This is not going to be called\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">\u015awietny tutorial, kt\u00f3ry wykorzysta\u0142em we wpisie:<\/h4>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-random-nerd-tutorials wp-block-embed-random-nerd-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"0qXr1ByWtE\"><a href=\"https:\/\/randomnerdtutorials.com\/esp32-external-wake-up-deep-sleep\/\">ESP32 External Wake Up from Deep Sleep<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;ESP32 External Wake Up from Deep Sleep&#8221; &#8212; Random Nerd Tutorials\" src=\"https:\/\/randomnerdtutorials.com\/esp32-external-wake-up-deep-sleep\/embed\/#?secret=0qXr1ByWtE\" data-secret=\"0qXr1ByWtE\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">LINKI<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>educ8s:<ul><li><a href=\"https:\/\/youtu.be\/y1R2y8dCsIg\">ESP32 Deep Sleep Tutorial for Low Power Projects<\/a><\/li><li><a href=\"http:\/\/educ8s.tv\/esp32-deep-sleep-tutorial\/\">ESP32 Deep Sleep Tutorial<\/a><\/li><\/ul><\/li><li>randomnerdtutorials.com:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\">ESP32 Deep Sleep with Arduino IDE and Wake Up Sources<\/a><\/li><\/ul><ul><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-external-wake-up-deep-sleep\/\">ESP32 External Wake Up from Deep Sleep<\/a><\/li><\/ul><\/li><li>ESPRESSIF<ul><li><a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/system\/sleep_modes.html\">Sleep Modes<\/a><\/li><\/ul><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. Usypianie i budzenie za pomoc\u0105 funkcji touchAttachInterrupt(). \u0179r\u00f3d\u0142o: Touch Pins as Wake-up Source (ESP32 + Arduino series) Argumenty funkcji touchAttachInterrupt(): uint8_t pin. W moim przypadku T0 to pin GPIO0&#8230;.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,80],"tags":[92],"class_list":["post-3848","post","type-post","status-publish","format-standard","hentry","category-arduino","category-esp32","tag-usypianie"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=3848"}],"version-history":[{"count":19,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3848\/revisions"}],"predecessor-version":[{"id":4378,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3848\/revisions\/4378"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=3848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/categories?post=3848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/tags?post=3848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}