Guest User

Arduino Automatic Smart Humidistat

a guest
Nov 18th, 2023
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.89 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Adafruit_BusIO_Register.h>
  4. #include <Adafruit_I2CDevice.h>
  5. #include <Adafruit_I2CRegister.h>
  6. #include <Adafruit_SPIDevice.h>
  7. #include "DHT.h"
  8. #include <ESPAsyncTCP.h>
  9. #include <ESPAsyncWebServer.h>
  10. #include <AsyncElegantOTA.h>
  11. #include <BlynkSimpleEsp8266.h>
  12. #include <Wire.h>
  13. #include "time.h"
  14. #include <Average.h>
  15. #include <Adafruit_ADS1X15.h>
  16. #include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
  17. #include "SH1106Wire.h"   // legacy: #include "SH1106.h"
  18. Adafruit_ADS1115 ads;
  19.  
  20.  SH1106Wire display(0x3c, SDA, SCL);
  21.  
  22. #define RELAY_PIN 14 //D5
  23. #define DHT_PIN 2 //D4
  24. DHT dht (DHT_PIN, DHT22);
  25. const float a = 0.148571;
  26. const float c = 9.68571; //line of best fit for relationship between outside air temp and set abs humidity
  27.  
  28. float hysteresis = 0.7;  //g/m3
  29. float sethum = 5.0;  //g/m3
  30.  
  31. #define VREF 4.096         // analog reference voltage(Volt) of the ADC
  32.  
  33.  
  34.  
  35. bool relaystate = false;
  36.  
  37.  
  38. float correctedGas;
  39. float aaH = 0.0000669005032586238;
  40. float baH = -0.0159747107540879;
  41. float abH = -0.00748672213648967;
  42. float bbH = 1.78146466558454; //MQ5 gas sensor temp/hum compensation factors
  43.  
  44. float correctionFactor(float temp, float hum) { //MQ5 gas sensor temp/hum compensation function
  45.   return (aaH * hum * temp) + (baH * temp) + (abH * hum) + bbH;
  46. }
  47.  
  48. Average<float> gasAvg(6);
  49. int gasRead, ledValue;
  50.  
  51. const char* ntpServer = "pool.ntp.org";
  52. const long gmtOffset_sec = -18000;  //Replace with your GMT offset (seconds)
  53. const int daylightOffset_sec = 0;   //Replace with your daylight offset (seconds)
  54.  
  55. float adc0, adc1, adc2, adc3;
  56. float volts0, volts1, volts2, volts3;
  57.  
  58.  
  59. const char* ssid = "mikesnet";
  60. const char* password = "springchicken";
  61.  
  62.  
  63.  
  64. char auth[] = "pO--Yj8ksH2fjJLMW6yW9trkHBhd9-wc";  //BLYNK
  65.  
  66. AsyncWebServer server(80);
  67. float abshum, tempDHT, humDHT;
  68. float tempprobe = 20;
  69. unsigned long millisBlynk = 0;
  70. unsigned long millisTFT = 0;
  71. unsigned long millisAvg = 0;
  72. int firstvalue = 1;
  73.  
  74.  
  75.  
  76. WidgetTerminal terminal(V10);
  77.  
  78. float bridgetemp;
  79.  
  80.  
  81. BLYNK_WRITE(V73) {
  82.   bridgetemp = param.asFloat();
  83. }
  84.  
  85. BLYNK_WRITE(V10) {
  86.   if (String("help") == param.asStr()) {
  87.     terminal.println("==List of available commands:==");
  88.     terminal.println("wifi");
  89.     terminal.println("readgas");
  90.     terminal.println("temps");
  91.     terminal.println("==End of list.==");
  92.   }
  93.   if (String("wifi") == param.asStr()) {
  94.     terminal.print("Connected to: ");
  95.     terminal.println(ssid);
  96.     terminal.print("IP address:");
  97.     terminal.println(WiFi.localIP());
  98.     terminal.print("Signal strength: ");
  99.     printLocalTime();
  100.     terminal.println(WiFi.RSSI());
  101.   }
  102.  
  103.   if (String("readgas") == param.asStr()) {
  104.     printtemp();
  105.   }
  106.   if (String("temps") == param.asStr()) {
  107.     humDHT = dht.readHumidity();
  108.     tempDHT = dht.readTemperature();
  109.     abshum = (6.112 * pow(2.71828, ((17.67 * tempDHT) / (tempDHT + 243.5))) * humDHT * 2.1674) / (273.15 + tempDHT);
  110.     terminal.print("> Temp: ");
  111.     terminal.print(tempDHT);
  112.     terminal.print("*C, Hum: ");
  113.     terminal.print(humDHT);
  114.     terminal.print("%, abshum: ");
  115.     terminal.println(abshum);
  116.   }
  117.  
  118.   terminal.flush();
  119. }
  120.  
  121.  
  122.  
  123.  
  124. void printLocalTime() {
  125.   time_t rawtime;
  126.   struct tm* timeinfo;
  127.   time(&rawtime);
  128.   timeinfo = localtime(&rawtime);
  129.   terminal.print("-");
  130.   terminal.print(asctime(timeinfo));
  131.   terminal.print(" - ");
  132.   terminal.flush();
  133. }
  134.  
  135.  
  136. void printtemp() {
  137.   adc3 = ads.readADC_SingleEnded(3);
  138.   volts3 = ads.computeVolts(adc3);
  139.   terminal.println("----------GAS---------");
  140.   terminal.println("AIN3: ");
  141.   terminal.print(adc3);
  142.   terminal.print("  ");
  143.   terminal.print(volts3);
  144.   terminal.println("V");
  145.   terminal.flush();
  146. }
  147.  
  148.  
  149.  
  150. char time_value[20];
  151. int hours, mins, secs;
  152.  
  153.  
  154. void setup() {
  155.   digitalWrite(RELAY_PIN, LOW);
  156.   pinMode(RELAY_PIN, OUTPUT);
  157.   pinMode(DHT_PIN, INPUT_PULLUP);
  158.    dht.begin ();
  159.   bridgetemp = -20;
  160.   Serial.begin(115200);
  161.   WiFi.mode(WIFI_STA);
  162.   WiFi.setPhyMode(WIFI_PHY_MODE_11B);
  163.   WiFi.begin("mikesnet", "springchicken");
  164.   delay(10);
  165.   display.init();
  166.   display.setFont(ArialMT_Plain_10);
  167.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  168.   display.flipScreenVertically();
  169.   display.drawString(0,0, "Connecting...");
  170.   while (WiFi.status() != WL_CONNECTED) {
  171.     delay(500);
  172.  
  173.   }
  174.  
  175.   delay(500);
  176.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  177.   delay(500);
  178.   Serial.println("");
  179.   Serial.print("Connected to ");
  180.   Serial.println(ssid);
  181.   Serial.print("IP address: ");
  182.   Serial.println(WiFi.localIP());
  183.   Blynk.config(auth, IPAddress(192, 168, 50, 197), 8080);
  184.   Blynk.connect();
  185.   terminal.println("***COSTELLO THE HUMIDISTAT v2.1***");
  186.   terminal.print("Connected to ");
  187.   terminal.println(ssid);
  188.   terminal.print("IP address: ");
  189.   terminal.println(WiFi.localIP());
  190.   printLocalTime();
  191.   terminal.flush();
  192.  
  193.   server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
  194.     request->send(200, "text/plain", "I am Costello");
  195.   });
  196.  
  197.   AsyncElegantOTA.begin(&server);  // Start ElegantOTA
  198.   server.begin();
  199.   terminal.println("HTTP server started");
  200.   terminal.flush();
  201.   if (!ads.begin()) {
  202.     terminal.println("Failed to initialize ADS.");
  203.     while (1)
  204.       ;
  205.   } else {
  206.     terminal.println("ADS initialized");
  207.   }
  208.   terminal.println("Startup complete.");
  209.   terminal.flush();
  210.   ads.setGain(GAIN_SIXTEEN);
  211.   gasRead = ads.readADC_SingleEnded(3);
  212.   gasAvg.push(gasRead);
  213.     humDHT = dht.readHumidity();
  214.     tempDHT = dht.readTemperature();
  215.     abshum = (6.112 * pow(2.71828, ((17.67 * tempDHT) / (tempDHT + 243.5))) * humDHT * 2.1674) / (273.15 + tempDHT);
  216. }
  217.  
  218. void loop() {
  219.   // put your main code here, to run repeatedly:
  220.   Blynk.run();
  221.  
  222.   static unsigned long analogSampleTimepoint = millis();
  223.  
  224.   if ((abshum < sethum) && (abshum > 0)) {
  225.     relaystate = true;
  226.     digitalWrite(RELAY_PIN, HIGH);
  227.     ledValue = 255;
  228.   }
  229.   if (abshum > (sethum + hysteresis)) {
  230.     relaystate = false;
  231.     digitalWrite(RELAY_PIN, LOW);
  232.     ledValue = 0;
  233.   }
  234.  
  235. if (hours > 11) {display.invertDisplay();} else {display.normalDisplay();}
  236.  
  237.  
  238.   if (millis() - millisBlynk >= 30000)  //if it's been 30 seconds
  239.   {
  240.     humDHT = dht.readHumidity();
  241.     tempDHT = dht.readTemperature();
  242.     abshum = (6.112 * pow(2.71828, ((17.67 * tempDHT) / (tempDHT + 243.5))) * humDHT * 2.1674) / (273.15 + tempDHT);
  243.     millisBlynk = millis();
  244.       if (tempDHT > 0) {Blynk.virtualWrite(V2, tempDHT);}
  245.     if (humDHT > 0) {Blynk.virtualWrite(V3, humDHT);}
  246.     Blynk.virtualWrite(V2, tempDHT);
  247.     Blynk.virtualWrite(V3, humDHT);
  248.     Blynk.virtualWrite(V4, abshum);
  249.  
  250.     Blynk.virtualWrite(V9, gasAvg.mean());
  251.  
  252.     float Rs = (ads.computeVolts(gasAvg.mean()) * 47000) / (5.0 - ads.computeVolts(gasAvg.mean()));
  253.     correctedGas = (Rs / correctionFactor(tempDHT, humDHT));
  254.     Blynk.virtualWrite(V13, correctedGas);
  255.     Blynk.virtualWrite(V14, relaystate);
  256.     Blynk.virtualWrite(V15, ledValue);
  257.     Blynk.virtualWrite(V16, sethum);
  258.     Blynk.virtualWrite(V17, bridgetemp);
  259.   }
  260.  
  261.  
  262.  
  263.   if (millis() - millisAvg >= 5000)  //if it's been 5 second
  264.   {
  265.     struct tm timeinfo;
  266.     getLocalTime(&timeinfo);
  267.     hours = timeinfo.tm_hour;
  268.     mins = timeinfo.tm_min;
  269.     secs = timeinfo.tm_sec;
  270.     millisAvg = millis();
  271.     gasRead = ads.readADC_SingleEnded(3);
  272.     gasAvg.push(gasRead);
  273.     String tempstring = "OUT TEMP: " + String(bridgetemp) + "°C";
  274.     String humstring = "IN HUM: " + String(abshum) + "g";
  275.     String sethumstring = "SET HUM: " + String(sethum) + "g";
  276.     display.clear();
  277.     display.drawString(0,0, tempstring);
  278.     display.drawString(0,12, humstring);
  279.     display.drawString(0,24, sethumstring);
  280.     if (relaystate) {String relaystring = "RELAY: [ON]";
  281.       display.drawString(0,36, relaystring);} else {String relaystring = "RELAY: [off]";
  282.       display.drawString(0,36, relaystring);}
  283.     display.display();
  284.     sethum = (a * bridgetemp) + c;
  285.   }
  286. }
  287.  
Advertisement
Add Comment
Please, Sign In to add comment