· 5 years ago · Oct 22, 2020, 04:52 AM
1#include <WiFi.h>
2#include <Wire.h>
3#include <Adafruit_Sensor.h>
4#include <Adafruit_BME280.h>
5#include <ESPmDNS.h>
6#include <WiFiUdp.h>
7#include <Arduino.h>
8#include "MHZ19.h"
9
10#define RX_PIN 16 // Rx pin which the MHZ19 Tx pin is attached to
11#define TX_PIN 17 // Tx pin which the MHZ19 Rx pin is attached to
12#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
13MHZ19 myMHZ19; // Constructor for library
14HardwareSerial mySerial(1); // (ESP32 Example) create device to MH-Z19 serial
15
16#define relayPin 13
17#define heaterPin 12
18
19
20unsigned long getDataTimer = 0;
21unsigned long previousMillis = 60000;
22const long interval = 60000;
23
24// Replace with your SSID and Password
25const char* ssid = "ssid";
26const char* password = "password";
27
28// How your resource variable should look like, but with your own API KEY (that API KEY below is just an example):
29const char* resource = "/trigger/bme280_readings/with/key/key";
30
31// Maker Webhooks IFTTT
32const char* server = "maker.ifttt.com";
33
34
35// Uncomment to use BME280 SPI
36/*#include <SPI.h>
37 #define BME_SCK 13
38 #define BME_MISO 12
39 #define BME_MOSI 11
40 #define BME_CS 10*/
41
42#define SEALEVELPRESSURE_HPA (1013.25)
43
44Adafruit_BME280 bme; // I2C
45//Adafruit_BME280 bme(BME_CS); // hardware SPI
46//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
47
48void setup() {
49 Serial.begin(115200);
50 mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // (ESP32 Example) device to MH-Z19 serial start
51 myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
52 myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
53 Serial.println("Booting");
54 WiFi.mode(WIFI_STA);
55 WiFi.begin(ssid, password);
56 pinMode(relayPin, OUTPUT);
57 digitalWrite(relayPin, LOW);
58 pinMode(heaterPin, OUTPUT);
59 digitalWrite(heaterPin, LOW);
60 while (WiFi.waitForConnectResult() != WL_CONNECTED) {
61 Serial.println("Connection Failed! Rebooting...");
62 delay(5000);
63 ESP.restart();
64 }
65
66 Serial.println("Ready");
67 Serial.print("IP address: ");
68 Serial.println(WiFi.localIP());
69
70 delay(2000);
71
72 // initialize BME280 sensor
73 bool status;
74 status = bme.begin(0x76);
75 //status = bme.begin();
76 if (!status) {
77 Serial.println("Could not find a valid BME280 sensor, check wiring!");
78 while (1);
79 }
80
81
82
83}
84
85void loop() {
86 unsigned long currentMillis = millis();
87 float h = bme.readHumidity();
88 float t = bme.readTemperature();
89 t = (t * 1.8); // convert to F
90 t = (t + 32); // convert to F
91 relayLogic(h, t);
92 //relayLogic(h,t);
93 if (currentMillis - previousMillis >= interval) {
94 previousMillis = currentMillis;
95 int c;
96 c = myMHZ19.getCO2(); // Request CO2 (as ppm)
97 //makeIFTTTRequest(t,h,triggerStatus);
98 makeIFTTTRequest(t, h, c);
99 }
100 delay(1000);
101
102}
103
104void relayLogic(float h, float t) {
105 if (t < 65) {
106 digitalWrite(heaterPin, HIGH);
107 }
108 if (t > 70) {
109 digitalWrite(heaterPin, LOW);
110 }
111 if (h < 90) {
112 digitalWrite(relayPin, HIGH);
113 }
114 if (h > 95) {
115 digitalWrite(relayPin, LOW);
116 }
117
118}
119
120// Make an HTTP request to the IFTTT web service
121void makeIFTTTRequest(float t, float h, int c) {
122 Serial.print("Connecting to ");
123 Serial.print(server);
124 //int triggerStatus = digitalRead(relayPin);
125 WiFiClient client;
126 int retries = 5;
127 while (!!!client.connect(server, 80) && (retries-- > 0)) {
128 Serial.print(".");
129 }
130 Serial.println();
131 if (!!!client.connected()) {
132 Serial.println("Failed to connect...");
133 }
134
135 Serial.print("Request resource: ");
136 Serial.println(resource);
137
138
139 // Temperature in Celsius
140 //String jsonObject = String("{\"value1\":\"") + temp "\"}";
141 String jsonObject = String("{\"value1\":\"") + t + "\",\"value2\":\"" + c + "\",\"value3\":\"" + h + "\"}";
142
143 // Comment the previous line and uncomment the next line to publish temperature readings in Fahrenheit
144 /*String jsonObject = String("{\"value1\":\"") + (1.8 * bme.readTemperature() + 32) + "\",\"value2\":\""
145 + (bme.readPressure()/100.0F) + "\",\"value3\":\"" + bme.readHumidity() + "\"}";*/
146
147 client.println(String("POST ") + resource + " HTTP/1.1");
148 client.println(String("Host: ") + server);
149 client.println("Connection: close\r\nContent-Type: application/json");
150 client.print("Content-Length: ");
151 client.println(jsonObject.length());
152 client.println();
153 client.println(jsonObject);
154
155 int timeout = 5 * 10; // 5 seconds
156 while (!!!client.available() && (timeout-- > 0)) {
157 delay(100);
158 }
159 if (!!!client.available()) {
160 Serial.println("No response...");
161 }
162 while (client.available()) {
163 Serial.write(client.read());
164 }
165
166 Serial.println("\nclosing connection");
167 client.stop();
168}
169