Repository navigation
Expand file tree
/
Copy pathCode Arduino
More file actions
95 lines (94 loc) · 2.69 KB
/
Copy pathCode Arduino
File metadata and controls
95 lines (94 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <SoftwareSerial.h>
#include <DHT.h>
#include <MQ135.h>
#include <SharpGP2Y10.h>
SoftwareSerial nodemcu(2,3);//tx-2, rx-3
#define DHTPIN 4
DHT dht(DHTPIN, DHT11);
#define PIN_MQ135 A0
MQ135 mq135_sensor = MQ135(PIN_MQ135);
int voPin = A1;
int ledPin = 5;
float dustDensity = 0;
SharpGP2Y10 dustSensor(voPin, ledPin);
int UVOUT = A2;
int REF_3V3 = A3;
const int trig = 6;
const int echo = 7;
void setup()
{
Serial.begin(9600);
nodemcu.begin(9600);
dht.begin();
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
unsigned long duration;
int distance;
digitalWrite(trig, 0);
delayMicroseconds(2);
digitalWrite(trig, 1);
delayMicroseconds(5);
digitalWrite(trig, 0);
duration = pulseIn(echo, HIGH);
distance = int(duration/2/29.412);
if (distance == 0)
return;
int a = 50 - distance;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
return;
float rzero = mq135_sensor.getRZero();
float correctedRZero = mq135_sensor.getCorrectedRZero(t, h);
float resistance = mq135_sensor.getResistance();
float ppm = mq135_sensor.getPPM();
float correctedPPM = mq135_sensor.getCorrectedPPM(t, h);
dustDensity = dustSensor.getDustDensity();
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);
float outputVoltage = 3.3 / refLevel * uvLevel;
float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0);
nodemcu.write(h);
nodemcu.write(t);
nodemcu.write(correctedPPM);
nodemcu.write(uvIntensity);
nodemcu.write(dustDensity);
nodemcu.write(a);
Serial.print("Temperature: ");
Serial.print(t);
Serial.println("*C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
Serial.print("Concentration of Dust: ");
Serial.print(dustDensity);
Serial.println("mg/m^3");
Serial.print("UV Ray Intensity: ");
Serial.print(uvIntensity);
Serial.println("mW/cm^2");
Serial.print("Air Quality: ");
Serial.print(correctedPPM);
Serial.println("ppm");
Serial.print("Sea Level Rise: ");
Serial.print(a);
Serial.println("cm");
delay(15000);
}
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;
for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;
return(runningValue);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}