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.

277 lines
6.9 KiB

3 years ago
  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. //F.M. ENCSYS edit 1
  14. #include "OTA.h"
  15. #include "OSC.h"
  16. #include <Adafruit_Sensor.h>
  17. #include <DHT.h>
  18. #include <DHT_U.h>
  19. #include <Wire.h>
  20. #include <Adafruit_GFX.h>
  21. #include "fontdef.h"
  22. //#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  23. #include "SSD1306Wire.h"
  24. #include "OLEDDisplayUi.h"
  25. #include <driver/adc.h>
  26. // Initialize the OLED display using Wire library
  27. SSD1306Wire display(0x3c, 21, 22);
  28. #define DHTPIN 25 // Digital pin connected to the DHT sensor
  29. // Uncomment the type of sensor in use:
  30. #define DHTTYPE DHT11 // DHT 11
  31. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  32. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  33. DHT_Unified dht(DHTPIN, DHTTYPE);
  34. uint32_t delayMS;
  35. //bool light = false;
  36. int LEDpin = 16;
  37. //int SMOKETIME = 5000;
  38. //bool SMOKEFLAG = true;
  39. ///////////////////////////////////////////////////////
  40. void setup() {
  41. APConnect();
  42. setupOTA();
  43. Udp.begin(rxport);
  44. //Serial.begin(115200);
  45. pinMode(LEDpin, OUTPUT);
  46. pinMode(4, OUTPUT);
  47. digitalWrite(4, false);
  48. fog = false;
  49. // Initialising the OLED display
  50. display.init();
  51. display.flipScreenVertically();
  52. display.setTextAlignment(TEXT_ALIGN_LEFT);
  53. display.clear();
  54. display.setFont(Monospaced_bold_16);
  55. display.drawString(0, 0, "Hello Human!");
  56. display.setFont(Monospaced_plain_12);
  57. display.drawString(20, 30, "How are you");
  58. display.drawString(40, 50, "today?");
  59. display.display();
  60. delay(3000);
  61. // Initialize DHT device.
  62. dht.begin();
  63. Serial.println(F("DHTxx Unified Sensor Example"));
  64. // Print temperature sensor details.
  65. sensor_t sensor;
  66. dht.temperature().getSensor(&sensor);
  67. Serial.println(F("------------------------------------"));
  68. Serial.println(F("Temperature Sensor"));
  69. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  70. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  71. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  72. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  73. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  74. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  75. Serial.println(F("------------------------------------"));
  76. // Print humidity sensor details.
  77. dht.humidity().getSensor(&sensor);
  78. Serial.println(F("Humidity Sensor"));
  79. Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
  80. Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
  81. Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
  82. Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  83. Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  84. Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  85. Serial.println(F("------------------------------------"));
  86. // Set delay between sensor readings based on sensor details.
  87. delayMS = sensor.min_delay / 1000;
  88. }
  89. void humiditycontrol()
  90. {
  91. delay(delayMS);
  92. // Get humidity event and compare it to the wanted value
  93. sensors_event_t event;
  94. dht.humidity().getEvent(&event);
  95. if ((int(event.relative_humidity)) > (int(humidityctrl))){
  96. fogstate = false;
  97. }
  98. else {
  99. //delay(100);
  100. // delay(delayMS);
  101. fogstate = true;
  102. //if (prevfogstate != fogstate){
  103. //delay(100);
  104. //}
  105. }
  106. }
  107. void fogcontrol()
  108. {
  109. if (fogstate == 1){
  110. digitalWrite(4, true);
  111. delay (100);
  112. prevfogstate = bool(fogstate);
  113. digitalWrite(4, false);
  114. delay (5000);
  115. //prevfogstate = int(fogstate);
  116. }
  117. else if (fogstate == 0){
  118. if (prevfogstate != fogstate){
  119. //delay (100);
  120. //digitalWrite(4, true);
  121. delay (100);
  122. digitalWrite(4, false);
  123. prevfogstate = bool(fogstate);
  124. }
  125. else{
  126. digitalWrite(4, false);
  127. }
  128. }
  129. }
  130. ///////////////////////////////////////////////////////
  131. void loop() {
  132. osc_message_pump();
  133. ArduinoOTA.handle();
  134. // FOG SWITCH
  135. //delay(SMOKETIME);
  136. //SMOKEFLAG = !(SMOKEFLAG);
  137. //digitalWrite(4, fog);
  138. digitalWrite(LEDpin, light);
  139. humiditycontrol();
  140. fogcontrol();
  141. /////////////////////// atomizer toggle
  142. Serial.println(int(humidityctrl));
  143. Serial.print(F("fog state: "));
  144. if (fogstate == 1){
  145. Serial.println (F("active"));
  146. }
  147. else{
  148. Serial.println (F("off"));
  149. }
  150. //////////////////
  151. // SENSOR AND DISPLAY
  152. display.clear();
  153. // Delay between measurements.
  154. delay(delayMS);
  155. // Get temperature event and print its value.
  156. sensors_event_t event;
  157. dht.temperature().getEvent(&event);
  158. if (isnan(event.temperature)) {
  159. Serial.println(F("Error reading temperature!"));
  160. }
  161. else {
  162. display.setFont(Monospaced_bold_16);
  163. display.drawString(0, 10, "Temp:");
  164. display.drawString(50, 10, String(int(event.temperature)) + " c" );
  165. Serial.print(F("Temperature: "));
  166. Serial.print(event.temperature);
  167. Serial.println(F("°C"));
  168. }
  169. // Get humidity event and print its value.
  170. dht.humidity().getEvent(&event);
  171. if (isnan(event.relative_humidity)) {
  172. Serial.println(F("Error reading humidity!"));
  173. }
  174. else {
  175. display.setFont(Monospaced_bold_16);
  176. display.drawString(0, 50, "Hum:");
  177. display.drawString(50, 50, String(int(event.relative_humidity)) + " %");
  178. Serial.print(F("Humidity: "));
  179. Serial.print(event.relative_humidity);
  180. Serial.println(F("%"));
  181. Serial.println (int(event.relative_humidity));
  182. Serial.println(fog);
  183. }
  184. display.display();
  185. // printBuffer();
  186. }
  187. ////////////////////////////////////////////
  188. void printBuffer(void) {
  189. // Initialize the log buffer
  190. // allocate memory to store 8 lines of text and 30 chars per line.
  191. display.setLogBuffer(5, 30);
  192. // Some test data
  193. const char* test[] = {
  194. "Hello",
  195. "World" ,
  196. "----",
  197. "Show off",
  198. "how",
  199. "the log buffer",
  200. "is",
  201. "working.",
  202. "Even",
  203. "scrolling is",
  204. "working"
  205. };
  206. for (uint8_t i = 0; i < 11; i++) {
  207. display.clear();
  208. // Print to the screen
  209. display.println(test[i]);
  210. // Draw it to the internal screen buffer
  211. display.drawLogBuffer(0, 0);
  212. // Display it on the screen
  213. display.display();
  214. delay(500);
  215. }
  216. }