aboutsummaryrefslogtreecommitdiff
path: root/sensor/sensor.ino
blob: b9c13ab3d3fb0b405fe28aca9a5e6115479c1825 (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. // arduino-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. // arduino-esp32 Bluetooth Low Energy (BLE) networking stack
  33. #include "BLEDevice.h"
  34. #include "BLEBeacon.h"
  35. #include "BLEAdvertising.h"
  36. #include "BLEEddystoneTLM.h"
  37. // Adjust these for production use
  38. //
  39. // * BEACON_NAME must be unique within deployment
  40. // * BEACON_UUID should be unique for each deployment
  41. //
  42. // @see https://www.uuidgenerator.net/
  43. #define BEACON_NAME "Dummy mussel sensor"
  44. #define BEACON_UUID "00000000-0000-0000-0000-000000000000"
  45. // maximum accumulated stress
  46. #define STRESS_MAX 50
  47. // arduino-esp32 Touch sensor
  48. #define TOUCH_PIN T0 // T0 is GPIO4
  49. #define TOUCH_THRESHOLD 40
  50. // arduino-esp32 LED PWM Controller (LEDC) to track gaping rhythm
  51. #define LED_PIN LED_BUILTIN
  52. #define LEDC_BITS 7
  53. #define LEDC_FREQ 500
  54. #define LEDC_START_DUTY 0
  55. #define LEDC_TARGET_DUTY 90
  56. #define LEDC_CALM_PACE 3000
  57. #define LEDC_STRESSED_PACE 400
  58. // Bluetooth beacon
  59. #define BEACON_POWER ESP_PWR_LVL_N12
  60. int stress = 0;
  61. bool touch_detected = false;
  62. int pace = LEDC_STRESSED_PACE;
  63. bool fade_ended = false;
  64. bool fade_in = true;
  65. BLEAdvertising *pAdvertising;
  66. // Touch sensor callback
  67. void gotTouch() {
  68. // keepPace();
  69. touch_detected = true;
  70. pace = LEDC_STRESSED_PACE;
  71. }
  72. // pacemaker Interrupt Service Routine (ISR)
  73. void ARDUINO_ISR_ATTR LED_FADE_ISR() {
  74. fade_ended = true;
  75. keepPace();
  76. }
  77. void beginTouchDetection() {
  78. touchAttachInterrupt(TOUCH_PIN, gotTouch, TOUCH_THRESHOLD);
  79. log_d("touch detected");
  80. }
  81. // pacemaker initialization
  82. void beginPace() {
  83. // Setup pacemaker timer
  84. ledcAttach(LED_PIN, LEDC_FREQ, LEDC_BITS);
  85. // fade in once supervised and then begin fade out with ISR
  86. ledcFade(LED_PIN, LEDC_START_DUTY, LEDC_TARGET_DUTY, pace);
  87. delay(pace);
  88. ledcFadeWithInterrupt(LED_PIN, LEDC_TARGET_DUTY, LEDC_START_DUTY,
  89. pace, LED_FADE_ISR);
  90. }
  91. // pacemaker maintenance
  92. void keepPace() {
  93. // if (fade_ended || touch_detected) {
  94. if (fade_ended) {
  95. fade_ended = false;
  96. // stress management
  97. if (touch_detected) {
  98. touch_detected = false;
  99. log_i("Stressed by touch!");
  100. if (stress < STRESS_MAX) {
  101. stress = stress + 10;
  102. }
  103. } else if (stress > 0) {
  104. stress--;
  105. if (stress <= 0) {
  106. pace = LEDC_CALM_PACE;
  107. log_i("Calmed down...");
  108. } else {
  109. log_i("Still stressed...");
  110. }
  111. } else {
  112. pace = LEDC_CALM_PACE;
  113. }
  114. // begin fade in opposite direction at decided pace
  115. ledcFadeWithInterrupt(LED_PIN,
  116. fade_in ? LEDC_START_DUTY : LEDC_TARGET_DUTY,
  117. fade_in ? LEDC_TARGET_DUTY : LEDC_START_DUTY,
  118. pace, LED_FADE_ISR);
  119. fade_in = !fade_in;
  120. }
  121. }
  122. // Sensor reading
  123. int getGapeAngle() {
  124. return ledcRead(LED_PIN);
  125. }
  126. // Construct Bluetooth beacon
  127. void setBeacon(int angle) {
  128. BLEEddystoneTLM EddystoneTLM;
  129. EddystoneTLM.setTemp(angle);
  130. log_i("Gape angle: %.2f°", EddystoneTLM.getTemp());
  131. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  132. BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  133. oScanResponseData.setServiceData(BLEUUID(
  134. (uint16_t)0xFEAA),
  135. String(EddystoneTLM.getData().c_str(),
  136. EddystoneTLM.getData().length()));
  137. oAdvertisementData.setName(BEACON_NAME);
  138. pAdvertising->setAdvertisementData(oAdvertisementData);
  139. pAdvertising->setScanResponseData(oScanResponseData);
  140. }
  141. void setup() {
  142. // enable logging to serial
  143. Serial.begin(115200);
  144. esp_log_level_set("*", ESP_LOG_DEBUG);
  145. beginPace();
  146. beginTouchDetection();
  147. // setup Bluetooth
  148. if (BEACON_UUID == "00000000-0000-0000-0000-000000000000")
  149. log_w("Please set a unique BEACON_UUID");
  150. BLEDevice::init("TLMBeacon");
  151. BLEDevice::setPower(BEACON_POWER);
  152. pAdvertising = BLEDevice::getAdvertising();
  153. setBeacon(getGapeAngle());
  154. pAdvertising->start();
  155. }
  156. void loop() {
  157. setBeacon(getGapeAngle());
  158. delay(500);
  159. }