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.

193 lines
5.6 KiB

  1. // ----- TeZ ---- DHT Display on OLED screen -----
  2. //* OLED DISPLAY 3.3V(VCC), GND, SCL (21), SDA (22) [ESP32]
  3. //* model: SSD1306 Resolution: 128 x 64 (small = 128 x 32)
  4. // FONTS >> in file OLEDdisplayFonts.h
  5. // ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
  6. // SansSerif_plain_10, SansSerif_plain_12
  7. // Dingbats__12
  8. // Monospaced_plain_8, Monospaced_plain_10, Monospaced_plain_12, Monospaced_bold_10, Monospaced_bold_16, Monospaced_bold_24 //
  9. // ceated with http://oleddisplay.squix.ch/
  10. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  11. // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
  12. // https://learn.adafruit.com/dht/overview
  13. #include <Adafruit_Sensor.h>
  14. #include <DHT.h>
  15. #include <DHT_U.h>
  16. #include <Wire.h>
  17. #include <Adafruit_GFX.h>
  18. #include "fontdef.h"
  19. //#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  20. #include "SSD1306Wire.h"
  21. #include "OLEDDisplayUi.h"
  22. #include <driver/adc.h>
  23. // Initialize the OLED display using Wire library
  24. SSD1306Wire display(0x3c, 21, 22);
  25. // SSD1306 pinout for ESP32
  26. // SDA >> 21
  27. // SCL >> 22
  28. // VCC >> Vin 5V
  29. // GND >> GND
  30. #define DHTPIN 25 // Digital pin connected to the DHT sensor
  31. // Uncomment the type of sensor in use:
  32. #define DHTTYPE DHT11 // DHT 11
  33. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  34. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  35. DHT_Unified dht(DHTPIN, DHTTYPE);
  36. uint32_t delayMS;
  37. int wemosLEDpin = 02;
  38. int SMOKETIME = 5000;
  39. bool SMOKEFLAG = true;
  40. ///////////////////////////////////////////////////////
  41. void setup() {
  42. Serial.begin(115200);
  43. pinMode(wemosLEDpin, OUTPUT);
  44. pinMode(4, OUTPUT);
  45. digitalWrite(4, true);
  46. digitalWrite(wemosLEDpin, true);
  47. // Initialising the OLED display
  48. display.init();
  49. display.flipScreenVertically();
  50. display.setTextAlignment(TEXT_ALIGN_LEFT);
  51. display.clear();
  52. display.setFont(Monospaced_bold_16);
  53. display.drawString(0, 0, "Hello Human!");
  54. display.setFont(Monospaced_plain_12);
  55. display.drawString(20, 30, "How are you");
  56. display.drawString(40, 50, "today?");
  57. display.display();
  58. delay(3000);
  59. // Initialize DHT device.
  60. dht.begin();
  61. Serial.println(F("DHTxx Unified Sensor Example"));
  62. // Print temperature sensor details.
  63. sensor_t sensor;
  64. dht.temperature().getSensor(&sensor);
  65. Serial.println(F("------------------------------------"));
  66. Serial.println(F("Temperature Sensor"));
  67. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  68. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  69. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  70. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  71. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  72. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  73. Serial.println(F("------------------------------------"));
  74. // Print humidity sensor details.
  75. dht.humidity().getSensor(&sensor);
  76. Serial.println(F("Humidity Sensor"));
  77. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  78. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  79. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  80. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  81. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  82. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  83. Serial.println(F("------------------------------------"));
  84. // Set delay between sensor readings based on sensor details.
  85. delayMS = sensor.min_delay / 1000;
  86. }
  87. ///////////////////////////////////////////////////////
  88. void loop() {
  89. // FOG SWITCH
  90. delay(SMOKETIME);
  91. SMOKEFLAG = !(SMOKEFLAG);
  92. digitalWrite(4, SMOKEFLAG);
  93. digitalWrite(wemosLEDpin, SMOKEFLAG);
  94. // SENSOR AND DISPLAY
  95. display.clear();
  96. // Delay between measurements.
  97. delay(delayMS);
  98. // Get temperature event and print its value.
  99. sensors_event_t event;
  100. dht.temperature().getEvent(&event);
  101. if (isnan(event.temperature)) {
  102. Serial.println(F("Error reading temperature!"));
  103. }
  104. else {
  105. display.setFont(Monospaced_bold_16);
  106. display.drawString(0, 10, "Temp:");
  107. display.drawString(50, 10, String(int(event.temperature)) + " c" );
  108. Serial.print(F("Temperature: "));
  109. Serial.print(event.temperature);
  110. Serial.println(F("°C"));
  111. }
  112. // Get humidity event and print its value.
  113. dht.humidity().getEvent(&event);
  114. if (isnan(event.relative_humidity)) {
  115. Serial.println(F("Error reading humidity!"));
  116. }
  117. else {
  118. display.setFont(Monospaced_bold_16);
  119. display.drawString(0, 50, "Hum:");
  120. display.drawString(50, 50, String(int(event.relative_humidity)) + " %");
  121. Serial.print(F("Humidity: "));
  122. Serial.print(event.relative_humidity);
  123. Serial.println(F("%"));
  124. }
  125. display.display();
  126. // printBuffer();
  127. }
  128. ////////////////////////////////////////////
  129. void printBuffer(void) {
  130. // Initialize the log buffer
  131. // allocate memory to store 8 lines of text and 30 chars per line.
  132. display.setLogBuffer(5, 30);
  133. // Some test data
  134. const char* test[] = {
  135. "Hello",
  136. "World" ,
  137. "----",
  138. "Show off",
  139. "how",
  140. "the log buffer",
  141. "is",
  142. "working.",
  143. "Even",
  144. "scrolling is",
  145. "working"
  146. };
  147. for (uint8_t i = 0; i < 11; i++) {
  148. display.clear();
  149. // Print to the screen
  150. display.println(test[i]);
  151. // Draw it to the internal screen buffer
  152. display.drawLogBuffer(0, 0);
  153. // Display it on the screen
  154. display.display();
  155. delay(500);
  156. }
  157. }