aboutsummaryrefslogtreecommitdiff
path: root/sensor/sensor.ino
blob: 8b56c26f42809343af43c2b752e1f300958d68d3 (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. /// @version 0.0.3
  10. /// @see <https://app.radicle.xyz/nodes/seed.radicle.garden/rad:z2tFBF4gN7ziG9oXtUytVQNYe3VhQ>
  11. /// @see <https://moodle.ruc.dk/course/view.php?id=23504>
  12. // arduino-esp32 Logging system
  13. // activate in Arduino IDE: Tools -> Core Debug Level
  14. // special: set Core Debug Level to Error for plot-friendly output
  15. #define CONFIG_ARDUHAL_ESP_LOG 1
  16. #define LOG_LOCAL_LEVEL CORE_DEBUG_LEVEL
  17. #include <esp32-hal-log.h>
  18. #undef ARDUHAL_LOG_FORMAT
  19. #define ARDUHAL_LOG_FORMAT(letter, format) \
  20. ARDUHAL_LOG_COLOR_##letter "[" #letter "] %s(): " format \
  21. ARDUHAL_LOG_RESET_COLOR "\r\n", __FUNCTION__
  22. // arduino-esp32 Bluetooth Low Energy (BLE) networking stack
  23. #include "BLEDevice.h"
  24. #include "BLEBeacon.h"
  25. #include "BLEAdvertising.h"
  26. #include "BLEEddystoneTLM.h"
  27. // Adjust these for production use
  28. //
  29. // * BEACON_NAME must be unique within deployment
  30. // * BEACON_UUID should be unique for each deployment
  31. //
  32. // @see https://www.uuidgenerator.net/
  33. #define BEACON_NAME "Dummy mussel sensor"
  34. #define BEACON_UUID "00000000-0000-0000-0000-000000000000"
  35. // maximum accumulated stress
  36. #define STRESS_MAX 50
  37. // arduino-esp32 Touch sensor
  38. #define TOUCH_PIN T0 // T0 is GPIO4
  39. #define TOUCH_THRESHOLD 40
  40. // arduino-esp32 LED PWM Controller (LEDC) as pacemaker for gaping rhythm
  41. #define LED_PIN LED_BUILTIN
  42. #define LEDC_BITS 7
  43. #define LEDC_FREQ 500
  44. #define LEDC_START_DUTY 0
  45. #define LEDC_TARGET_DUTY 90
  46. #define LEDC_CALM_PACE 3000
  47. #define LEDC_STRESSED_PACE 400
  48. // pacemaker variables
  49. int stress = 0;
  50. bool touch_detected = false;
  51. int pace = LEDC_STRESSED_PACE;
  52. bool fade_ended = false;
  53. bool fade_in = true;
  54. // pointer to control Bluetooth networking
  55. BLEAdvertising *pAdvertising;
  56. // Touch sensor callback
  57. void gotTouch() {
  58. // keepPace();
  59. touch_detected = true;
  60. pace = LEDC_STRESSED_PACE;
  61. }
  62. // pacemaker end-of-fade Interrupt Service Routine (ISR) a.k.a. callback
  63. void ARDUINO_ISR_ATTR LED_FADE_ISR() {
  64. fade_ended = true;
  65. keepPace();
  66. }
  67. // stress-inducing touch callback
  68. void beginTouchDetection() {
  69. touchAttachInterrupt(TOUCH_PIN, gotTouch, TOUCH_THRESHOLD);
  70. log_d("touch detected");
  71. }
  72. // pacemaker initialization
  73. void beginPace() {
  74. // Setup pacemaker timer
  75. ledcAttach(LED_PIN, LEDC_FREQ, LEDC_BITS);
  76. // fade in once uncontrolled and then begin fade out with ISR
  77. ledcFade(LED_PIN, LEDC_START_DUTY, LEDC_TARGET_DUTY, pace);
  78. delay(pace);
  79. ledcFadeWithInterrupt(LED_PIN, LEDC_TARGET_DUTY, LEDC_START_DUTY,
  80. pace, LED_FADE_ISR);
  81. }
  82. // pacemaker maintenance
  83. void keepPace() {
  84. // if (fade_ended || touch_detected) {
  85. if (fade_ended) {
  86. fade_ended = false;
  87. // stress management
  88. if (touch_detected) {
  89. touch_detected = false;
  90. log_i("Stressed by touch!");
  91. if (stress < STRESS_MAX) {
  92. stress = stress + 10;
  93. }
  94. } else if (stress > 0) {
  95. stress--;
  96. if (stress <= 0) {
  97. pace = LEDC_CALM_PACE;
  98. log_i("Calmed down...");
  99. } else {
  100. log_i("Still stressed...");
  101. }
  102. } else {
  103. pace = LEDC_CALM_PACE;
  104. }
  105. // begin fade at decided direction and pace
  106. ledcFadeWithInterrupt(LED_PIN,
  107. fade_in ? LEDC_START_DUTY : LEDC_TARGET_DUTY,
  108. fade_in ? LEDC_TARGET_DUTY : LEDC_START_DUTY,
  109. pace, LED_FADE_ISR);
  110. // remember next fade direction
  111. fade_in = !fade_in;
  112. }
  113. }
  114. // read fake gape angle sensor from pacemaker
  115. int getGapeAngle() {
  116. return ledcRead(LED_PIN);
  117. }
  118. // Encode static Bluetooth beacon advertisement data
  119. void setBeaconAdvertisement() {
  120. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  121. oAdvertisementData.setName(BEACON_NAME);
  122. pAdvertising->setAdvertisementData(oAdvertisementData);
  123. }
  124. // Encode variable Bluetooth beacon service data
  125. void setBeaconServiceData(int angle) {
  126. BLEEddystoneTLM EddystoneTLM;
  127. EddystoneTLM.setTemp(angle);
  128. log_i("Gape angle: %.2f°", EddystoneTLM.getTemp());
  129. BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  130. oScanResponseData.setServiceData(
  131. BLEUUID((uint16_t)0xFEAA),
  132. String(
  133. EddystoneTLM.getData().c_str(),
  134. EddystoneTLM.getData().length()));
  135. pAdvertising->setScanResponseData(oScanResponseData);
  136. }
  137. void setup() {
  138. // enable logging to serial
  139. Serial.begin(115200);
  140. esp_log_level_set("*", ESP_LOG_DEBUG);
  141. if (BEACON_UUID == "00000000-0000-0000-0000-000000000000")
  142. Serial.println("Please set a deployment-wide unique BEACON_UUID");
  143. beginPace();
  144. beginTouchDetection();
  145. // setup Bluetooth
  146. BLEDevice::init(BEACON_NAME);
  147. pAdvertising = BLEDevice::getAdvertising();
  148. setBeaconAdvertisement();
  149. setBeaconServiceData(getGapeAngle());
  150. pAdvertising->start();
  151. }
  152. void loop() {
  153. // update Bluetooth beacon service data
  154. setBeaconServiceData(getGapeAngle());
  155. // misuse error-only log level for plot-friendly output
  156. #if ARDUHAL_LOG_LEVEL == ARDUHAL_LOG_LEVEL_ERROR
  157. Serial.printf("gape_angle:%d\n",
  158. getGapeAngle());
  159. #endif
  160. delay(500);
  161. }