Ahmad Radhy
Published © MIT

Indoor Air Quality Detection During Covid19 Pandemic (IAQD)

IAQD technology is based on the Internet of Things (IoT) integrated with smart sensors able to collect data

IntermediateProtip2 days77
Indoor Air Quality Detection During Covid19 Pandemic (IAQD)

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Wemos D1 Mini
Espressif Wemos D1 Mini
×1
SparkFun Air Quality Breakout - CCS811
SparkFun Air Quality Breakout - CCS811
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

nano transmitter

wemos gateway

Code

nano transmitter

Arduino
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include "SparkFunCCS811.h"

#define CSS811_ADDR 0x5A
CCS811 ccs811(CSS811_ADDR);

boolean state = false;

#define RX 2
#define TX 3

SoftwareSerial HC12(RX, TX);

long lastData = 0;
//float t;
//float h;
DHT dht(5, DHT22);

void setup()
{
	pinMode(RX, INPUT);
	pinMode(TX, OUTPUT);
	pinMode(7, OUTPUT);
	pinMode(LED_BUILTIN, OUTPUT);
	Serial.begin(9600);
	HC12.begin(9600);
	dht.begin();

	if (HC12.isListening()) {

		Serial.println("HC12 is OK");
		}

	else{

		Serial.println("HC12 is NOT OK");

		}

	digitalWrite(7, LOW);
	HC12.print("AT+DEFAULT");
	delay(100);
	digitalWrite(7, HIGH);

	Wire.begin();

	if (ccs811.begin() == false) {
		Serial.print("CCS811 Error");
		while(1)
			;
	}


}


void loop()
{

	long now = millis();

	if (now - lastData > 15000) {


		lastData = now;

		if (ccs811.dataAvailable()) {

			float t = dht.readTemperature();
			float h = dht.readHumidity();

			if (isnan(t) || isnan(h)) {

				Serial.println("Failed to read from DHT");
			}

			uint16_t co2 = ccs811.getCO2();
			uint16_t voc = ccs811.getTVOC();

			const size_t capacity = JSON_OBJECT_SIZE(4);
			DynamicJsonDocument doc(capacity);

			doc["temperature"] = t;
			doc["humidity"] = h;
			doc["CO2"] = co2;
			doc["VOC"] = voc;

			char attributes[250];
			serializeJson(doc, attributes);

			ccs811.readAlgorithmResults();
			HC12.println(attributes);
			digitalWrite(LED_BUILTIN, state);
			state = not state;
		}


	}

}

wemos gateway

Arduino
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

void initialize_HC12();
void setup_wifi();
void reconnect();

#define RX D2
#define TX D1

SoftwareSerial HC12(RX, TX);


#define TOKEN ""

const char* ssid = "";
const char* password = "";
const char mqtt_server[] = "demo.thingsboard.io";
const char publishTopic[] = "v1/devices/me/telemetry";

WiFiClient espClient;
PubSubClient client(espClient);

long lastData = 0;
//String payload;
//char attributes[1000];
boolean state = false;

void initialize_HC12(){

	pinMode(RX, INPUT);
	pinMode(TX, OUTPUT);
	pinMode(D3, OUTPUT);

	HC12.begin(9600);

	if (HC12.isListening()) {


		Serial.println("HC12 is OK");
	}

	else{
		Serial.println("HC12 is NOT OK");
		}

	//Test HC-12
	digitalWrite(D3, LOW);
	HC12.write("AT+DEFAULT");
	delay(100);
	digitalWrite(D3, HIGH);

	Serial.println();
	Serial.println("Initialization done.");

}

void setup_wifi(){

	delay(10);
	Serial.println();
	Serial.println("Connecting to");
	Serial.println(ssid);
	WiFi.mode(WIFI_STA);
	WiFi.begin(ssid, password);

	while( WiFi.status() != WL_CONNECTED){

		delay(500);
		Serial.print(".");
	}

	randomSeed(micros());
	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());
}

void reconnect(){

	//Loop until we're reconnected

	while(!client.connected()){

		if (client.connect("", TOKEN, NULL)) {

			Serial.println("Connected to server");

		}

		else{

			Serial.print("Failed, rc=");
			Serial.print(client.state());
			Serial.println("try again in 5 seconds");
			delay(5000);
		}
	}


}

void setup()
{
	Serial.begin(9600);
	pinMode(LED_BUILTIN, OUTPUT);
	initialize_HC12();
	setup_wifi();
	client.setServer(mqtt_server, 1883);

}


void loop()
{

	if (!client.connected()) {

		reconnect();
	}

	client.loop();

	//String payload;
	//char attributes[1000];

	if(HC12.available()){

		String payload;

		while(HC12.available()){

			payload = HC12.readString();
			delay(100);
			//payload.toCharArray(attributes, 1000);
			//Serial.print(attributes);
			//client.publish(publishTopic, attributes);
		}

		char attributes[1000];
		long now = millis();

		if (now - lastData > 15000) {

			lastData = now;
			payload.toCharArray(attributes, 1000);
			Serial.print(attributes);
			client.publish(publishTopic, attributes);
			digitalWrite(LED_BUILTIN, state);
			state = not state;
		}


		//payload.toCharArray(attributes, 1000);
		//Serial.print(attributes);
		//client.publish(publishTopic, attributes);

		/*payload = HC12.readString();
		payload.toCharArray(attributes, 1000);
		Serial.print(attributes);
	    client.publish(publishTopic, attributes);
	    delay(3000);*/
	}

	//delay(3000);

}

Credits

Ahmad Radhy

Ahmad Radhy

13 projects • 11 followers
I'm now taking master degree in Physics Department Institut Teknologi Bandung (ITB).

Comments