Created
June 24, 2020 13:29
-
-
Save EdNutting/34faa7965703e683f121b9d8bb2e2a3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include "MQ135.h" | |
#include "DHTesp.h" | |
DHTesp dht; | |
MQ135 mq135_sensor = MQ135(A0); | |
float accumTemperature = 0.0; | |
float accumHumidity = 0.0; | |
float accumPPM = 0.0; | |
int readingCount = 0; | |
const int readingInterval = 2000; // milliseconds | |
const int submissionInterval = 20; // seconds | |
float previousTemp = 0.0; | |
float previousHumidity = 0.0; | |
float previousPPM = 0.0; | |
String writeApiKey = "1FA6872EYILY415V"; | |
const char* ssid = "PLUSNET-2SFH"; | |
const char* password = "24acd379e2"; | |
const char* server = "api.thingspeak.com"; | |
WiFiClient client; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED){ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("WiFi connected"); | |
dht.setup(D7, DHTesp::DHT11); | |
} | |
void printValueAndDifference(float val, float previousVal) { | |
float diff = val - previousVal; | |
Serial.print(val); | |
Serial.print(" ("); | |
Serial.print(diff >= 0 ? "+" : ""); | |
Serial.print(diff); | |
Serial.print(")"); | |
} | |
void loop() { | |
float humidityReading = dht.getHumidity(); | |
float temperatureReading = dht.getTemperature(); | |
bool humidityAndTempOK = !isnan(humidityReading) && !isnan(temperatureReading); | |
if (humidityAndTempOK) { | |
accumHumidity += humidityReading; | |
accumTemperature += temperatureReading; | |
readingCount++; | |
} | |
else { | |
Serial.println("Humidity or temperature reading was NAN!"); | |
} | |
float humidity = accumHumidity / readingCount; | |
float temperature = accumTemperature / readingCount; | |
if (humidityAndTempOK) { | |
float ppmReading = mq135_sensor.getCorrectedPPM(temperature, humidity); | |
if (!isnan(ppmReading)) { | |
accumPPM += ppmReading; | |
Serial.print("Readings: Humidity: "); | |
Serial.print(humidityReading); | |
Serial.print(", temp: "); | |
Serial.print(temperatureReading); | |
Serial.print(", ppm: "); | |
Serial.print(ppmReading); | |
Serial.print(", c: "); | |
Serial.println(readingCount); | |
} | |
else { | |
// The reading count was incremented so we have to add something | |
accumPPM += previousPPM; | |
Serial.println("PPM reading was NAN!"); | |
} | |
} | |
float ppm = accumPPM / readingCount; | |
if ((readingCount - 1) * readingInterval > submissionInterval * 1000) { | |
Serial.print("Humidity: "); | |
printValueAndDifference(humidity, previousHumidity); | |
Serial.print(", temp: "); | |
printValueAndDifference(temperature, previousTemp); | |
Serial.print(", ppm: "); | |
printValueAndDifference(ppm, previousPPM); | |
Serial.println(); | |
Serial.println("Sending data to Thingspeak"); | |
if (client.connect(server,80)) { | |
String postStr = writeApiKey; | |
postStr +="&field1="; | |
postStr += String(ppm); | |
postStr +="&field2="; | |
postStr += String(humidity); | |
postStr +="&field3="; | |
postStr += String(temperature); | |
client.print("POST /update HTTP/1.1\n"); | |
client.print("Host: api.thingspeak.com\n"); | |
client.print("Connection: close\n"); | |
client.print("X-THINGSPEAKAPIKEY: "+writeApiKey+"\n"); | |
client.print("Content-Type: application/x-www-form-urlencoded\n"); | |
client.print("Content-Length: "); | |
client.print(postStr.length()); | |
client.print("\n\n"); | |
client.print(postStr); | |
} | |
else { | |
Serial.println("Unable to connect to server! Reading not submitted."); | |
} | |
client.stop(); | |
previousHumidity = humidity; | |
previousTemp = temperature; | |
previousPPM = ppm; | |
accumHumidity = humidity; | |
accumTemperature = temperature; | |
accumPPM = ppm; | |
readingCount = 1; | |
} | |
delay(readingInterval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment