TouchWakeUp

はじめに

タッチパッドからの入力によって、ディープスリープ状態から復帰します。ESP-IDFの機能を利用しています。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch as a wake up source and how to store data in
RTC memory to use it over reboots

This code is under Public Domain License.

Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/

#define Threshold 40 /* Greater the value, more the sensitivity */

RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;

bootCountは、何回ブートされたかをカウントする変数です。RTC_DATA_ATTRは、変数をRTC SLOW Memoryに配置するよう指示します。RTC SLOW Memoryはディープスリープ中でもデータが消えないメモリです。

touchPinは、タッチパッド用のピンです。タッチパッドについては、こちらも参照してください。

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}
 

print_wakeup_reason()は、ディープスリープからの復帰理由を表示します。

esp_sleep_get_wakeup_cause()は、ディープスリープから復帰した理由を返すESP-IDFのAPIです。

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad(){
  touchPin = esp_sleep_get_touchpad_wakeup_status();

  switch(touchPin)
  {
    case 0  : Serial.println("Touch detected on GPIO 4"); break;
    case 1  : Serial.println("Touch detected on GPIO 0"); break;
    case 2  : Serial.println("Touch detected on GPIO 2"); break;
    case 3  : Serial.println("Touch detected on GPIO 15"); break;
    case 4  : Serial.println("Touch detected on GPIO 13"); break;
    case 5  : Serial.println("Touch detected on GPIO 12"); break;
    case 6  : Serial.println("Touch detected on GPIO 14"); break;
    case 7  : Serial.println("Touch detected on GPIO 27"); break;
    case 8  : Serial.println("Touch detected on GPIO 33"); break;
    case 9  : Serial.println("Touch detected on GPIO 32"); break;
    default : Serial.println("Wakeup not by touchpad"); break;
  }
}
  

どのタッチパッドから復帰したかを表示します。

esp_sleep_get_touchpad_wakeup_status()は、どのタッチパッドから復帰したかを返すESP-IDFの関数です。

callback()

61
62
63
64
void callback(){
  //placeholder callback function
}
 

タッチパッドがタッチされたときに実行されるコールバック関数です。

何もしません。

setup()

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32 and touchpad too
  print_wakeup_reason();
  print_wakeup_touchpad();

  //Setup interrupt on Touch Pad 3 (GPIO15)
  touchAttachInterrupt(T3, callback, Threshold);

  //Configure Touchpad as wakeup source
  esp_sleep_enable_touchpad_wakeup();

  //Go to sleep now
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}
 

最初にbootCountをインクリメントすることで、ブート回数を設定した後、print_wakeup_reason()でブート理由を表示します。

次に、print_wakeup_touchpad()で、どのタッチパッドによって復帰したかを表示します。

touchAttachInterrupt()で、タッチパッドの設定を行います。T3(GPIO15)を利用します。今回はディープスリープから復帰させるためだけの利用なので、何もしないコールバック関数を設定しているようです。

esp_sleep_enable_touchpad_wakeup()で、タッチパッドによるディープスリープからの復帰を可能にします。

次に、esp_deep_sleep_start()で、実際にディープスリープします。

loop()

89
90
91
void loop(){
  //This will never be reached
}

何もしません。

実行結果

以下に実行結果を示します。GPIO15にジャンパワイヤを接続し、スリープ中にジャンパワイヤに触りました。

初回のブートは、Wakeup was not caused by deep sleep / Wakeup not by touchpad と表示され、2回目のブート(ディープスリープからの復帰)は、Wakeup caused by touhpad / Touch detected on GPIO 15 と表示されました。

バージョン

Hardware:ESP-WROOM-32
Software:Arduino core for the ESP32 2.0.4

最終更新日

September 4, 2022

inserted by FC2 system