aboutsummaryrefslogtreecommitdiff
path: root/sensor/sensor.ino
blob: f051b0b940253dc7fbc4c6c4b73d6e0551a794dd (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. int stress = 0;
  59. bool touch_detected = false;
  60. int pace = LEDC_STRESSED_PACE;
  61. bool fade_ended = false;
  62. bool fade_in = true;
  63. // pointer to control Bluetooth networking
  64. BLEAdvertising *pAdvertising;
  65. // Touch sensor callback
  66. void gotTouch() {
  67. // keepPace();
  68. touch_detected = true;
  69. pace = LEDC_STRESSED_PACE;
  70. }
  71. // pacemaker Interrupt Service Routine (ISR)
  72. void ARDUINO_ISR_ATTR LED_FADE_ISR() {
  73. fade_ended = true;
  74. keepPace();
  75. }
  76. void beginTouchDetection() {
  77. touchAttachInterrupt(TOUCH_PIN, gotTouch, TOUCH_THRESHOLD);
  78. log_d("touch detected");
  79. }
  80. // pacemaker initialization
  81. void beginPace() {
  82. // Setup pacemaker timer
  83. ledcAttach(LED_PIN, LEDC_FREQ, LEDC_BITS);
  84. // fade in once supervised and then begin fade out with ISR
  85. ledcFade(LED_PIN, LEDC_START_DUTY, LEDC_TARGET_DUTY, pace);
  86. delay(pace);
  87. ledcFadeWithInterrupt(LED_PIN, LEDC_TARGET_DUTY, LEDC_START_DUTY,
  88. pace, LED_FADE_ISR);
  89. }
  90. // pacemaker maintenance
  91. void keepPace() {
  92. // if (fade_ended || touch_detected) {
  93. if (fade_ended) {
  94. fade_ended = false;
  95. // stress management
  96. if (touch_detected) {
  97. touch_detected = false;
  98. log_i("Stressed by touch!");
  99. if (stress < STRESS_MAX) {
  100. stress = stress + 10;
  101. }
  102. } else if (stress > 0) {
  103. stress--;
  104. if (stress <= 0) {
  105. pace = LEDC_CALM_PACE;
  106. log_i("Calmed down...");
  107. } else {
  108. log_i("Still stressed...");
  109. }
  110. } else {
  111. pace = LEDC_CALM_PACE;
  112. }
  113. // begin fade in opposite direction at decided pace
  114. ledcFadeWithInterrupt(LED_PIN,
  115. fade_in ? LEDC_START_DUTY : LEDC_TARGET_DUTY,
  116. fade_in ? LEDC_TARGET_DUTY : LEDC_START_DUTY,
  117. pace, LED_FADE_ISR);
  118. fade_in = !fade_in;
  119. }
  120. }
  121. // Sensor reading
  122. int getGapeAngle() {
  123. return ledcRead(LED_PIN);
  124. }
  125. // Encode static Bluetooth beacon advertisement data
  126. void setBeaconAdvertisement() {
  127. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  128. oAdvertisementData.setName(BEACON_NAME);
  129. pAdvertising->setAdvertisementData(oAdvertisementData);
  130. }
  131. // Encode variable Bluetooth beacon service data
  132. void setBeaconServiceData(int angle) {
  133. BLEEddystoneTLM EddystoneTLM;
  134. EddystoneTLM.setTemp(angle);
  135. log_i("Gape angle: %.2f°", EddystoneTLM.getTemp());
  136. BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  137. oScanResponseData.setServiceData(BLEUUID(
  138. (uint16_t)0xFEAA),
  139. String(EddystoneTLM.getData().c_str(),
  140. EddystoneTLM.getData().length()));
  141. pAdvertising->setScanResponseData(oScanResponseData);
  142. }
  143. void setup() {
  144. // enable logging to serial
  145. Serial.begin(115200);
  146. esp_log_level_set("*", ESP_LOG_DEBUG);
  147. if (BEACON_UUID == "00000000-0000-0000-0000-000000000000")
  148. Serial.println("Please set a deployment-wide unique BEACON_UUID");
  149. beginPace();
  150. beginTouchDetection();
  151. // setup Bluetooth
  152. BLEDevice::init(BEACON_NAME);
  153. pAdvertising = BLEDevice::getAdvertising();
  154. setBeaconAdvertisement();
  155. setBeaconServiceData(getGapeAngle());
  156. pAdvertising->start();
  157. }
  158. void loop() {
  159. setBeaconServiceData(getGapeAngle());
  160. delay(500);
  161. }