aboutsummaryrefslogtreecommitdiff
path: root/sensor/sensor.ino
blob: 867c17daad9034ba7222fae9ba7017f5ac0376c4 (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) as pacemaker for 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. // pacemaker variables
  59. int stress = 0;
  60. bool touch_detected = false;
  61. int pace = LEDC_STRESSED_PACE;
  62. bool fade_ended = false;
  63. bool fade_in = true;
  64. // pointer to control Bluetooth networking
  65. BLEAdvertising *pAdvertising;
  66. // Touch sensor callback
  67. void gotTouch() {
  68. // keepPace();
  69. touch_detected = true;
  70. pace = LEDC_STRESSED_PACE;
  71. }
  72. // pacemaker end-of-fade Interrupt Service Routine (ISR) a.k.a. callback
  73. void ARDUINO_ISR_ATTR LED_FADE_ISR() {
  74. fade_ended = true;
  75. keepPace();
  76. }
  77. // stress-inducing touch callback
  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 uncontrolled 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 at decided direction and 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. // remember next fade direction
  121. fade_in = !fade_in;
  122. }
  123. }
  124. // read fake gape angle sensor from pacemaker
  125. int getGapeAngle() {
  126. return ledcRead(LED_PIN);
  127. }
  128. // Encode static Bluetooth beacon advertisement data
  129. void setBeaconAdvertisement() {
  130. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  131. oAdvertisementData.setName(BEACON_NAME);
  132. pAdvertising->setAdvertisementData(oAdvertisementData);
  133. }
  134. // Encode variable Bluetooth beacon service data
  135. void setBeaconServiceData(int angle) {
  136. BLEEddystoneTLM EddystoneTLM;
  137. EddystoneTLM.setTemp(angle);
  138. log_i("Gape angle: %.2f°", EddystoneTLM.getTemp());
  139. BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  140. oScanResponseData.setServiceData(
  141. BLEUUID((uint16_t)0xFEAA),
  142. String(
  143. EddystoneTLM.getData().c_str(),
  144. EddystoneTLM.getData().length()));
  145. pAdvertising->setScanResponseData(oScanResponseData);
  146. }
  147. void setup() {
  148. // enable logging to serial
  149. Serial.begin(115200);
  150. esp_log_level_set("*", ESP_LOG_DEBUG);
  151. if (BEACON_UUID == "00000000-0000-0000-0000-000000000000")
  152. Serial.println("Please set a deployment-wide unique BEACON_UUID");
  153. beginPace();
  154. beginTouchDetection();
  155. // setup Bluetooth
  156. BLEDevice::init(BEACON_NAME);
  157. pAdvertising = BLEDevice::getAdvertising();
  158. setBeaconAdvertisement();
  159. setBeaconServiceData(getGapeAngle());
  160. pAdvertising->start();
  161. }
  162. void loop() {
  163. // update Bluetooth beacon service data
  164. setBeaconServiceData(getGapeAngle());
  165. delay(500);
  166. }