materials for TeZ Bioreactor course
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.0 KiB

  1. #include <Arduino.h>
  2. #include <analogWrite.h>
  3. #include <ESP32Servo.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <DHT.h>
  6. #include <DHT_U.h>
  7. #include <Wire.h>
  8. #include <Adafruit_GFX.h>
  9. #include "fontdef.h"
  10. //#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  11. #include "SSD1306Wire.h"
  12. #include "OLEDDisplayUi.h"
  13. #include <driver/adc.h>
  14. Servo myservo; // create servo object to control a servo
  15. int servoPin = 23;
  16. // Initialize the OLED display using Wire library
  17. SSD1306Wire display(0x3c, 21, 22);
  18. #define DHTPIN 25 // Digital pin connected to the DHT sensor
  19. #define DHTTYPE DHT11 // DHT 11
  20. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  21. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  22. DHT_Unified dht(DHTPIN, DHTTYPE);
  23. uint32_t delayMS;
  24. int count1 = 0;
  25. long int deltask1 = 30 * 60 * 1000 / portTICK_PERIOD_MS; // 30 min
  26. //long int deltask1 = 1 * 60 * 1000 / portTICK_PERIOD_MS; // 30 min
  27. int transPin = 14;
  28. int ledPin = 22;
  29. ////////////////////////
  30. void task1(void * parameters) {
  31. for(;;){
  32. servoDance();
  33. vTaskDelay(deltask1);
  34. }
  35. }
  36. /////////////////////////
  37. void servoDance(){
  38. for(int n=1; n<=10; n++){
  39. myservo.write(90);
  40. delay(500);
  41. myservo.write(180);
  42. delay(500);
  43. }
  44. myservo.write(90);
  45. }
  46. ////////////////////////
  47. void setup() {
  48. Serial.begin(115200);
  49. myservo.setPeriodHertz(10);
  50. myservo.attach(servoPin);
  51. // pinMode(transPin, OUTPUT);
  52. // pinMode(ledPin, OUTPUT);
  53. //
  54. // digitalWrite(ledPin,HIGH);
  55. helloHuman(); // init OLED display
  56. initTempSensor(); // init temperature sensor
  57. xTaskCreate(
  58. task1, // function name
  59. "Task1", // task name
  60. 1000, // stack size
  61. NULL, // task parameters
  62. 1, // priority (low = more priority)
  63. NULL // task handle
  64. );
  65. }
  66. void ShowTemperature(){
  67. // SENSOR AND DISPLAY
  68. display.clear();
  69. // Delay between measurements.
  70. delay(delayMS);
  71. // Get temperature event and print its value.
  72. sensors_event_t event;
  73. dht.temperature().getEvent(&event);
  74. if (isnan(event.temperature)) {
  75. Serial.println(F("Error reading temperature!"));
  76. }
  77. else {
  78. display.setFont(Monospaced_bold_16);
  79. display.drawString(0, 10, "Temp:");
  80. display.drawString(50, 10, String(int(event.temperature)) + " c" );
  81. Serial.print(F("Temperature: "));
  82. Serial.print(event.temperature);
  83. Serial.println(F("°C"));
  84. }
  85. // Get humidity event and print its value.
  86. dht.humidity().getEvent(&event);
  87. if (isnan(event.relative_humidity)) {
  88. Serial.println(F("Error reading humidity!"));
  89. }
  90. else {
  91. display.setFont(Monospaced_bold_16);
  92. display.drawString(0, 50, "Hum:");
  93. display.drawString(50, 50, String(int(event.relative_humidity)) + " %");
  94. Serial.print(F("Humidity: "));
  95. Serial.print(event.relative_humidity);
  96. Serial.println(F("%"));
  97. }
  98. display.display();
  99. }
  100. ////////////////////////////
  101. void initTempSensor(){
  102. // Initialize DHT device.
  103. dht.begin();
  104. Serial.println(F("DHTxx Unified Sensor Example"));
  105. // Print temperature sensor details.
  106. sensor_t sensor;
  107. dht.temperature().getSensor(&sensor);
  108. Serial.println(F("------------------------------------"));
  109. Serial.println(F("Temperature Sensor"));
  110. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  111. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  112. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  113. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  114. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  115. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  116. Serial.println(F("------------------------------------"));
  117. // Print humidity sensor details.
  118. dht.humidity().getSensor(&sensor);
  119. Serial.println(F("Humidity Sensor"));
  120. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  121. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  122. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  123. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  124. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  125. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  126. Serial.println(F("------------------------------------"));
  127. // Set delay between sensor readings based on sensor details.
  128. delayMS = sensor.min_delay / 1000;
  129. }
  130. ////////////////////////
  131. void helloHuman(){
  132. // Initialising the OLED display
  133. display.init();
  134. display.flipScreenVertically();
  135. display.setTextAlignment(TEXT_ALIGN_LEFT);
  136. display.clear();
  137. display.setFont(Monospaced_bold_16);
  138. display.drawString(0, 0, "Hello Human!");
  139. display.setFont(Monospaced_plain_12);
  140. display.drawString(20, 30, "How are you");
  141. display.drawString(40, 50, "today?");
  142. display.display();
  143. delay(3000);
  144. }
  145. ////////////////////////
  146. void loop() {
  147. ShowTemperature();
  148. delay(1000);
  149. }