Components and supplies
Arduino Oplà IoT Kit
Axial Fan, 12 V
Grove - Dust Sensor(PPD42NS)
Grove - Air quality sensor v1.3
Project description
Code
Code
c_cpp
1//#include"AirQuality.h" 2#include "thingProperties.h" 3#include <Arduino_MKRIoTCarrier.h> 4MKRIoTCarrier carrier; 5 6//Colors 7uint32_t noColor = carrier.leds.Color( 0, 0, 0); // no color 8uint32_t colorRed = carrier.leds.Color(0, 255, 0); //RED 9uint32_t colorGreen = carrier.leds.Color(255, 0, 0); //GREEN 10uint32_t colorWhite = carrier.leds.Color( 255, 255, 255); // WHITE 11uint32_t colorYellow = carrier.leds.Color( 255, 255, 0); // yellow 12uint32_t colorBlue = carrier.leds.Color( 0, 0, 255); // blue 13uint32_t colorPurple = carrier.leds.Color( 255, 0, 255); // purple 14 15//int pir_movement = A4; 16int pir_movement_state = 0; 17 18int pir_dust = A6; 19int air_quality_sensor = A5 ; 20 21 22unsigned long duration; 23unsigned long start_time; 24unsigned long sample_time_ms = 3000;//sample 3s ; 25unsigned long low_pulse_occupancy = 0; 26float ratio = 0; 27 28void setup() { 29 // Initialize serial and wait for port to open: 30 Serial.begin(9600); 31 32 // Defined in thingProperties.h 33 initProperties(); 34 35 // Connect to Arduino IoT Cloud 36 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 37 //Get Cloud Info/errors , 0 (only errors) up to 4 38 setDebugMessageLevel(2); 39 ArduinoCloud.printDebugInfo(); 40 41 //Wait to get cloud connection to init the carrier 42 while (ArduinoCloud.connected() != 1) { 43 ArduinoCloud.update(); 44 delay(500); 45 } 46 47 delay(500); 48 CARRIER_CASE = false; 49 carrier.begin(); 50 carrier.display.setRotation(0); 51 delay(1000); 52 pinMode(A5, INPUT); 53 pinMode(A6, INPUT); 54 //pinMode(A4, INPUT); 55 56 start_time = millis(); 57} 58 59void loop() { 60 ArduinoCloud.update(); 61 carrier.Buttons.update(); 62 63 temperature = carrier.Env.readTemperature(); 64 humidity = carrier.Env.readHumidity(); 65 Serial.print("Temperature: "); 66 Serial.print(temperature); 67 Serial.print(", Humidity: "); 68 Serial.println(humidity); 69 70 71 duration = pulseIn(pir_dust, LOW); 72 low_pulse_occupancy = low_pulse_occupancy + duration; 73 74 if ((millis()-start_time) > sample_time_ms)//if the sampel time == 3s 75 { 76 // operations on the dust sensor 77 ratio = low_pulse_occupancy / (sample_time_ms*10.0); // Integer percentage 0=>100 78 concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve 79 Serial.print("concentration = "); 80 Serial.print(concentration); 81 Serial.println(" pcs/0.01cf - "); 82 low_pulse_occupancy = 0; 83 start_time = millis(); 84 85 // operations on the air-pollution sensor 86 current_quality = analogRead(air_quality_sensor); 87 if (current_quality >= 1000){ 88 Serial.println("Very High Pollution!"); 89 } 90 else if (current_quality >= 700){ 91 Serial.println("High pollution!"); 92 } 93 else if (current_quality >= 300){ 94 Serial.println("Low pollution!"); 95 } 96 else{ 97 Serial.println("Fresh air"); 98 } 99 100 if((concentration > 6000) || (current_quality >= 1000)){ 101 carrier.display.fillScreen(ST77XX_BLACK); //black background 102 carrier.display.setTextColor(ST77XX_BLUE); //blue text 103 carrier.display.setTextSize(3); //medium sized text 104 //sets position for printing (x and y) 105 carrier.display.setCursor(45, 70); 106 carrier.display.println("HAZARD!"); 107 carrier.leds.fill(colorBlue, 0, 5); 108 carrier.leds.show(); 109 110 } 111 112 else if((concentration > 4500 && concentration < 10000) || (current_quality <= 1000 && current_quality >= 700)){ 113 carrier.display.fillScreen(ST77XX_BLACK); //black background 114 carrier.display.setTextColor(ST77XX_RED); //red text 115 carrier.display.setTextSize(3); //medium sized text 116 //sets position for printing (x and y) 117 carrier.display.setCursor(50, 70); 118 carrier.display.println("HEAVY"); 119 carrier.leds.fill(colorRed, 0, 5); 120 carrier.leds.show(); 121 122 } 123 124 else if ((concentration > 3000 && concentration < 8000) || (current_quality <= 700 && current_quality >= 500)){ 125 carrier.display.fillScreen(ST77XX_BLACK); //black background 126 carrier.display.setTextColor(ST77XX_YELLOW); //yellow text 127 carrier.display.setTextSize(3); //medium sized text 128 //sets position for printing (x and y) 129 carrier.display.setCursor(30, 70); 130 carrier.display.println("ACCEPTABLE"); 131 carrier.leds.fill(colorYellow, 0, 5); 132 carrier.leds.show(); 133 134 } 135 136 else if ((concentration > 1500 && concentration < 6000) || (current_quality <= 500 && current_quality >= 300)) { 137 carrier.display.fillScreen(ST77XX_BLACK); //black background 138 carrier.display.setTextColor(ST77XX_GREEN); //green text 139 carrier.display.setTextSize(3); //medium sized text 140 //sets position for printing (x and y) 141 carrier.display.setCursor(55, 70); 142 carrier.display.println("GOOD"); 143 carrier.leds.fill(colorGreen, 0, 5); 144 carrier.leds.show(); 145 } 146 147 else{ 148 carrier.display.fillScreen(ST77XX_BLACK); //black background 149 carrier.display.setTextColor(ST77XX_WHITE); //white text 150 carrier.display.setTextSize(3); //medium sized text 151 //sets position for printing (x and y) 152 carrier.display.setCursor(55, 70); 153 carrier.display.println("CLEAN"); 154 carrier.leds.fill(colorWhite, 0, 5); 155 carrier.leds.show(); 156 } 157 158 if(fan_flag){ 159 if(concentration > 6000 || current_quality >= 500) carrier.Relay1.open(); 160 else carrier.Relay1.close(); 161 } 162 163 } 164 165 // motion check 166 //pir_movement_state = digitalRead(pir_movement); 167 168 //if(pir_movement_state == HIGH) { // if motion detected 169 // onMovementEventChange(); 170 //} 171 172 if(carrier.Button0.onTouchDown()) { // button press 173 fan_flag = !fan_flag; 174 carrier.display.fillScreen(ST77XX_BLACK); 175 carrier.display.setTextColor(colorPurple); 176 carrier.display.setTextSize(2); 177 if(fan_flag){ 178 carrier.display.print("fan mode is active"); 179 } 180 else{ 181 carrier.display.print("fan mode is off"); 182 } 183 } 184} 185 186void onMovementEventChange() { 187 carrier.leds.fill(colorPurple, 0, 5); 188 carrier.leds.show(); 189 carrier.display.fillScreen(ST77XX_BLACK); //black background 190 carrier.display.setTextColor(colorPurple); //purple text 191 carrier.display.setTextSize(2); //medium sized text 192 //sets position for printing (x and y) 193 carrier.display.setCursor(20, 40); 194 carrier.display.print("Temperature: "); 195 carrier.display.println(temperature); 196 carrier.display.setCursor(20, 60); 197 carrier.display.print("Humidity: "); 198 carrier.display.println(humidity); 199 carrier.display.setCursor(20, 80); 200 carrier.display.print("concentration: "); 201 carrier.display.print(concentration); 202 carrier.display.println(" pcs/0.01cf"); 203 carrier.display.setCursor(20, 100); 204 carrier.display.print("Air-Pollute measure: "); 205 carrier.display.print(current_quality); 206 delay(3000); 207} 208 209void onHumidityChange() { 210 211} 212 213void onTemperatureChange() { 214 // Do something 215} 216 217void onConcentrationChange() { 218 // Do something 219} 220 221void onCurrentQualityChange() { 222 // Do something 223} 224 225void onFanFlagChange() { 226 // Do something 227} 228
Code
c_cpp
1//#include"AirQuality.h" 2#include "thingProperties.h" 3#include <Arduino_MKRIoTCarrier.h> 4MKRIoTCarrier carrier; 5 6//Colors 7uint32_t noColor = carrier.leds.Color( 0, 0, 0); // no color 8uint32_t colorRed = carrier.leds.Color(0, 255, 0); //RED 9uint32_t colorGreen = carrier.leds.Color(255, 0, 0); //GREEN 10uint32_t colorWhite = carrier.leds.Color( 255, 255, 255); // WHITE 11uint32_t colorYellow = carrier.leds.Color( 255, 255, 0); // yellow 12uint32_t colorBlue = carrier.leds.Color( 0, 0, 255); // blue 13uint32_t colorPurple = carrier.leds.Color( 255, 0, 255); // purple 14 15//int pir_movement = A4; 16int pir_movement_state = 0; 17 18int pir_dust = A6; 19int air_quality_sensor = A5 ; 20 21 22unsigned long duration; 23unsigned long start_time; 24unsigned long sample_time_ms = 3000;//sample 3s ; 25unsigned long low_pulse_occupancy = 0; 26float ratio = 0; 27 28void setup() { 29 // Initialize serial and wait for port to open: 30 Serial.begin(9600); 31 32 // Defined in thingProperties.h 33 initProperties(); 34 35 // Connect to Arduino IoT Cloud 36 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 37 //Get Cloud Info/errors , 0 (only errors) up to 4 38 setDebugMessageLevel(2); 39 ArduinoCloud.printDebugInfo(); 40 41 //Wait to get cloud connection to init the carrier 42 while (ArduinoCloud.connected() != 1) { 43 ArduinoCloud.update(); 44 delay(500); 45 } 46 47 delay(500); 48 CARRIER_CASE = false; 49 carrier.begin(); 50 carrier.display.setRotation(0); 51 delay(1000); 52 pinMode(A5, INPUT); 53 pinMode(A6, INPUT); 54 //pinMode(A4, INPUT); 55 56 start_time = millis(); 57} 58 59void loop() { 60 ArduinoCloud.update(); 61 carrier.Buttons.update(); 62 63 temperature = carrier.Env.readTemperature(); 64 humidity = carrier.Env.readHumidity(); 65 Serial.print("Temperature: "); 66 Serial.print(temperature); 67 Serial.print(", Humidity: "); 68 Serial.println(humidity); 69 70 71 duration = pulseIn(pir_dust, LOW); 72 low_pulse_occupancy = low_pulse_occupancy + duration; 73 74 if ((millis()-start_time) > sample_time_ms)//if the sampel time == 3s 75 { 76 // operations on the dust sensor 77 ratio = low_pulse_occupancy / (sample_time_ms*10.0); // Integer percentage 0=>100 78 concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve 79 Serial.print("concentration = "); 80 Serial.print(concentration); 81 Serial.println(" pcs/0.01cf - "); 82 low_pulse_occupancy = 0; 83 start_time = millis(); 84 85 // operations on the air-pollution sensor 86 current_quality = analogRead(air_quality_sensor); 87 if (current_quality >= 1000){ 88 Serial.println("Very High Pollution!"); 89 } 90 else if (current_quality >= 700){ 91 Serial.println("High pollution!"); 92 } 93 else if (current_quality >= 300){ 94 Serial.println("Low pollution!"); 95 } 96 else{ 97 Serial.println("Fresh air"); 98 } 99 100 if((concentration > 6000) || (current_quality >= 1000)){ 101 carrier.display.fillScreen(ST77XX_BLACK); //black background 102 carrier.display.setTextColor(ST77XX_BLUE); //blue text 103 carrier.display.setTextSize(3); //medium sized text 104 //sets position for printing (x and y) 105 carrier.display.setCursor(45, 70); 106 carrier.display.println("HAZARD!"); 107 carrier.leds.fill(colorBlue, 0, 5); 108 carrier.leds.show(); 109 110 } 111 112 else if((concentration > 4500 && concentration < 10000) || (current_quality <= 1000 && current_quality >= 700)){ 113 carrier.display.fillScreen(ST77XX_BLACK); //black background 114 carrier.display.setTextColor(ST77XX_RED); //red text 115 carrier.display.setTextSize(3); //medium sized text 116 //sets position for printing (x and y) 117 carrier.display.setCursor(50, 70); 118 carrier.display.println("HEAVY"); 119 carrier.leds.fill(colorRed, 0, 5); 120 carrier.leds.show(); 121 122 } 123 124 else if ((concentration > 3000 && concentration < 8000) || (current_quality <= 700 && current_quality >= 500)){ 125 carrier.display.fillScreen(ST77XX_BLACK); //black background 126 carrier.display.setTextColor(ST77XX_YELLOW); //yellow text 127 carrier.display.setTextSize(3); //medium sized text 128 //sets position for printing (x and y) 129 carrier.display.setCursor(30, 70); 130 carrier.display.println("ACCEPTABLE"); 131 carrier.leds.fill(colorYellow, 0, 5); 132 carrier.leds.show(); 133 134 } 135 136 else if ((concentration > 1500 && concentration < 6000) || (current_quality <= 500 && current_quality >= 300)) { 137 carrier.display.fillScreen(ST77XX_BLACK); //black background 138 carrier.display.setTextColor(ST77XX_GREEN); //green text 139 carrier.display.setTextSize(3); //medium sized text 140 //sets position for printing (x and y) 141 carrier.display.setCursor(55, 70); 142 carrier.display.println("GOOD"); 143 carrier.leds.fill(colorGreen, 0, 5); 144 carrier.leds.show(); 145 } 146 147 else{ 148 carrier.display.fillScreen(ST77XX_BLACK); //black background 149 carrier.display.setTextColor(ST77XX_WHITE); //white text 150 carrier.display.setTextSize(3); //medium sized text 151 //sets position for printing (x and y) 152 carrier.display.setCursor(55, 70); 153 carrier.display.println("CLEAN"); 154 carrier.leds.fill(colorWhite, 0, 5); 155 carrier.leds.show(); 156 } 157 158 if(fan_flag){ 159 if(concentration > 6000 || current_quality >= 500) carrier.Relay1.open(); 160 else carrier.Relay1.close(); 161 } 162 163 } 164 165 // motion check 166 //pir_movement_state = digitalRead(pir_movement); 167 168 //if(pir_movement_state == HIGH) { // if motion detected 169 // onMovementEventChange(); 170 //} 171 172 if(carrier.Button0.onTouchDown()) { // button press 173 fan_flag = !fan_flag; 174 carrier.display.fillScreen(ST77XX_BLACK); 175 carrier.display.setTextColor(colorPurple); 176 carrier.display.setTextSize(2); 177 if(fan_flag){ 178 carrier.display.print("fan mode is active"); 179 } 180 else{ 181 carrier.display.print("fan mode is off"); 182 } 183 } 184} 185 186void onMovementEventChange() { 187 carrier.leds.fill(colorPurple, 0, 5); 188 carrier.leds.show(); 189 carrier.display.fillScreen(ST77XX_BLACK); //black background 190 carrier.display.setTextColor(colorPurple); //purple text 191 carrier.display.setTextSize(2); //medium sized text 192 //sets position for printing (x and y) 193 carrier.display.setCursor(20, 40); 194 carrier.display.print("Temperature: "); 195 carrier.display.println(temperature); 196 carrier.display.setCursor(20, 60); 197 carrier.display.print("Humidity: "); 198 carrier.display.println(humidity); 199 carrier.display.setCursor(20, 80); 200 carrier.display.print("concentration: "); 201 carrier.display.print(concentration); 202 carrier.display.println(" pcs/0.01cf"); 203 carrier.display.setCursor(20, 100); 204 carrier.display.print("Air-Pollute measure: "); 205 carrier.display.print(current_quality); 206 delay(3000); 207} 208 209void onHumidityChange() { 210 211} 212 213void onTemperatureChange() { 214 // Do something 215} 216 217void onConcentrationChange() { 218 // Do something 219} 220 221void onCurrentQualityChange() { 222 // Do something 223} 224 225void onFanFlagChange() { 226 // Do something 227} 228
Downloadable files
Demo
Demo
Comments
Only logged in users can leave comments
2 Team members on this project
1
0