aboutsummaryrefslogtreecommitdiff
path: root/sensor/sensor.ino
blob: 5a1f0750873ee7cba2db2c1b5234fce638684261 (plain)
  1. // SPDX-FileCopyrightText: 2025 Amal Mazrah <mazrah@ruc.dk>
  2. // SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  3. // SPDX-FileCopyrightText: 2025 Mennatullah Hatim Kassim <stud-mennatulla@ruc.dk>
  4. // SPDX-FileCopyrightText: 2025 Noor Ahmad <noora@ruc.dk>
  5. // SPDX-FileCopyrightText: 2025 Tanishka Suwalka <tanishkas@ruc.dk>
  6. // SPDX-License-Identifier: GPL-3.0-or-later
  7. /// Sensor mussel - an Arduino sketch to emulate a mussel biosensor
  8. ///
  9. /// * v0.0.3
  10. /// * rewrite sensor as sketch,
  11. /// using ESP32 LEDC, Touch sensor and Logging library
  12. ///
  13. /// * v0.0.2
  14. /// * rewrite attitude #2 to also handle button press
  15. /// * add voting functions and example
  16. ///
  17. /// * v0.0.1
  18. /// * initial release to radicle
  19. ///
  20. /// @version 0.0.3
  21. /// @see <https://app.radicle.xyz/nodes/seed.radicle.garden/rad:z2tFBF4gN7ziG9oXtUytVQNYe3VhQ/tree/Mussel/README.md>
  22. /// @see <https://moodle.ruc.dk/course/view.php?id=23504>
  23. // include and refine ESP32 Logging system
  24. // activate in Arduino IDE: Tools -> Core Debug Level
  25. #define CONFIG_ARDUHAL_ESP_LOG 1
  26. #define LOG_LOCAL_LEVEL CORE_DEBUG_LEVEL
  27. #include <esp32-hal-log.h>
  28. #undef ARDUHAL_LOG_FORMAT
  29. #define ARDUHAL_LOG_FORMAT(letter, format) \
  30. ARDUHAL_LOG_COLOR_##letter "[" #letter "] %s(): " format \
  31. ARDUHAL_LOG_RESET_COLOR "\r\n", __FUNCTION__
  32. #include "BLEDevice.h"
  33. #include "BLEBeacon.h"
  34. #include "BLEAdvertising.h"
  35. #include "BLEEddystoneTLM.h"
  36. // Adjust these for production use
  37. //
  38. // * BEACON_UUID should be unique for each deployment
  39. // * INSTANCE_ID should be unique for each sensor
  40. //
  41. // @see https://www.uuidgenerator.net/
  42. #define BEACON_NAME "Dummy mussel sensor"
  43. #define BEACON_UUID "00000000-0000-0000-0000-000000000000"
  44. #define INSTANCE_ID "0xbeac0101"
  45. #define STRESS_MAX 50
  46. // touch sensor using ESP32 Touch sensor
  47. // https://docs.espressif.com/projects/arduino-esp32/en/latest/api/touch.html
  48. #define TOUCH_PIN T0 // T0 is GPIO4
  49. #define TOUCH_THRESHOLD 40
  50. // pacemaker using ESP32 LED PWM Controller (LEDC)
  51. // https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
  52. #define LED_PIN LED_BUILTIN
  53. #define LEDC_BITS 7
  54. #define LEDC_FREQ 500
  55. #define LEDC_START_DUTY 0
  56. #define LEDC_TARGET_DUTY 90
  57. #define LEDC_CALM_PACE 3000
  58. #define LEDC_STRESSED_PACE 400
  59. // Bluetooth beacon
  60. #define BEACON_POWER ESP_PWR_LVL_N12
  61. int stress = 0;
  62. bool touch_detected = false;
  63. int pace = LEDC_STRESSED_PACE;
  64. bool fade_ended = false;
  65. bool fade_in = true;
  66. BLEAdvertising *pAdvertising;
  67. // Touch sensor callback
  68. void gotTouch() {
  69. // keepPace();
  70. touch_detected = true;
  71. pace = LEDC_STRESSED_PACE;
  72. }
  73. // pacemaker Interrupt Service Routine (ISR)
  74. void ARDUINO_ISR_ATTR LED_FADE_ISR() {
  75. fade_ended = true;
  76. keepPace();
  77. }
  78. void beginTouchDetection() {
  79. touchAttachInterrupt(TOUCH_PIN, gotTouch, TOUCH_THRESHOLD);
  80. log_d("touch detected");
  81. }
  82. // pacemaker initialization
  83. void beginPace() {
  84. // Setup pacemaker timer
  85. ledcAttach(LED_PIN, LEDC_FREQ, LEDC_BITS);
  86. // fade in once supervised and then begin fade out with ISR
  87. ledcFade(LED_PIN, LEDC_START_DUTY, LEDC_TARGET_DUTY, pace);
  88. delay(pace);
  89. ledcFadeWithInterrupt(LED_PIN, LEDC_TARGET_DUTY, LEDC_START_DUTY,
  90. pace, LED_FADE_ISR);
  91. }
  92. // pacemaker maintenance
  93. void keepPace() {
  94. // if (fade_ended || touch_detected) {
  95. if (fade_ended) {
  96. fade_ended = false;
  97. // stress management
  98. if (touch_detected) {
  99. touch_detected = false;
  100. log_i("Stressed by touch!");
  101. if (stress < STRESS_MAX) {
  102. stress = stress + 10;
  103. }
  104. } else if (stress > 0) {
  105. stress--;
  106. if (stress <= 0) {
  107. pace = LEDC_CALM_PACE;
  108. log_i("Calmed down...");
  109. } else {
  110. log_i("Still stressed...");
  111. }
  112. } else {
  113. pace = LEDC_CALM_PACE;
  114. }
  115. // begin fade in opposite direction at decided pace
  116. ledcFadeWithInterrupt(LED_PIN,
  117. fade_in ? LEDC_START_DUTY : LEDC_TARGET_DUTY,
  118. fade_in ? LEDC_TARGET_DUTY : LEDC_START_DUTY,
  119. pace, LED_FADE_ISR);
  120. fade_in = !fade_in;
  121. }
  122. }
  123. // Sensor reading
  124. int getGapeAngle() {
  125. return ledcRead(LED_PIN);
  126. }
  127. // Construct Bluetooth beacon
  128. void setBeacon(int angle) {
  129. BLEEddystoneTLM EddystoneTLM;
  130. EddystoneTLM.setTemp(angle);
  131. log_i("Gape angle: %.2f°", EddystoneTLM.getTemp());
  132. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  133. BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  134. oScanResponseData.setServiceData(BLEUUID(
  135. (uint16_t)0xFEAA),
  136. String(EddystoneTLM.getData().c_str(),
  137. EddystoneTLM.getData().length()));
  138. oAdvertisementData.setName(BEACON_NAME);
  139. pAdvertising->setAdvertisementData(oAdvertisementData);
  140. pAdvertising->setScanResponseData(oScanResponseData);
  141. }
  142. void setup() {
  143. // enable logging to serial
  144. Serial.begin(115200);
  145. esp_log_level_set("*", ESP_LOG_DEBUG);
  146. beginPace();
  147. beginTouchDetection();
  148. // setup Bluetooth
  149. if (BEACON_UUID == "00000000-0000-0000-0000-000000000000")
  150. log_w("Please set a unique BEACON_UUID");
  151. BLEDevice::init("TLMBeacon");
  152. BLEDevice::setPower(BEACON_POWER);
  153. pAdvertising = BLEDevice::getAdvertising();
  154. setBeacon(getGapeAngle());
  155. pAdvertising->start();
  156. }
  157. void loop() {
  158. setBeacon(getGapeAngle());
  159. delay(500);
  160. }