5
5
@version 3.0
6
6
*/
7
7
8
- #include < Arduino.h>
9
8
#include < DHT.h>
10
- #include < LiquidCrystal .h>
9
+ #include < Arduino .h>
11
10
#include < ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
11
+ #include < LiquidCrystal.h>
12
12
#include < SoftwareSerial.h>
13
13
14
14
#define DEBUG true
15
15
16
16
#define DHT_PIN 4 // pin connected to data pin of DHT11
17
17
#define DHT_TYPE DHT11 // Type of the DHT Sensor, DHT11/DHT22
18
-
19
- DHT sensor (DHT_PIN, DHT_TYPE);
18
+ int uvAnalogIn = A0;
20
19
21
20
// initialize the library with the numbers of the interface pins
22
21
LiquidCrystal lcd (7 , 8 , 9 , 10 , 11 , 12 );
23
22
23
+ DHT temperatureSensor (DHT_PIN, DHT_TYPE);
24
+
24
25
// ESP TX => Uno Pin 2
25
26
// ESP RX => Uno Pin 3
26
27
SoftwareSerial wifi (2 , 3 );
27
28
28
29
// **************
29
30
String sendDataToWiFiBoard (String command, const int timeout, boolean debug);
30
- String prepareDataForWiFi (float humidity, float temperature, float heatIndex);
31
+ String prepareDataForWiFi (float humidity, float temperature, float heatIndex, float uvIndex );
31
32
void setup ();
32
33
void loop ();
33
34
// **************
@@ -39,13 +40,14 @@ void loop();
39
40
* @param heatIndex
40
41
* @return
41
42
*/
42
- String prepareDataForWiFi (float humidity, float temperature, float heatIndex)
43
+ String prepareDataForWiFi (float humidity, float temperature, float heatIndex, float uvIndex )
43
44
{
44
45
StaticJsonDocument<200 > doc;
45
46
46
47
doc[" humidity" ] = String (humidity);
47
48
doc[" temperature" ] = String (temperature);
48
49
doc[" heat_index" ] = String (heatIndex);
50
+ doc[" uv_index" ] = String (uvIndex);
49
51
50
52
char jsonBuffer[512 ];
51
53
serializeJson (doc, jsonBuffer);
@@ -84,6 +86,9 @@ String sendDataToWiFiBoard(String command, const int timeout, boolean debug)
84
86
}
85
87
86
88
void setup () {
89
+ // read from GUVA-S12SD
90
+ pinMode (uvAnalogIn, INPUT);
91
+
87
92
Serial.begin (9600 );
88
93
wifi.begin (9600 );
89
94
@@ -94,10 +99,10 @@ void setup() {
94
99
lcd.setCursor (0 , 0 );
95
100
96
101
// display a welcome message
97
- lcd.print (" Temp & Humidity" );
102
+ lcd.print (" Temp, Humidity" );
98
103
// go to second line
99
104
lcd.setCursor (0 , 1 );
100
- lcd.print (" Sensor" );
105
+ lcd.print (" and UV Sensor" );
101
106
102
107
lcd.blink ();
103
108
delay (3000 );
@@ -124,30 +129,37 @@ void loop() {
124
129
Serial.println (" endbuffer" );
125
130
}
126
131
127
- // read from the digital pin
128
- sensor.begin ();
129
-
130
- float temperature = sensor.readTemperature (); // return temperature in °C
131
- float humidity = sensor.readHumidity (); // return humidity in %
132
+ // read from DHT11
133
+ temperatureSensor.begin ();
132
134
135
+ float temperature = temperatureSensor.readTemperature (); // return temperature in °C
136
+ float humidity = temperatureSensor.readHumidity (); // return humidity in %
133
137
// Compute heat index in Celsius (isFahrenheit = false)
134
- float heatIndex = sensor.computeHeatIndex (temperature, humidity, false );
138
+ float heatIndex = temperatureSensor.computeHeatIndex (temperature, humidity, false );
139
+ float uvIndex = analogRead (uvAnalogIn);
140
+
141
+ // integer index, divide by 100!
142
+ uvIndex /= 100.0 ;
135
143
136
144
// check whether reading was successful or not
137
145
if (temperature == NAN || humidity == NAN) { // NAN means no available data
138
146
lcd.print (" Reading failed." );
139
147
} else {
140
148
// display Humidity on the LCD screen
141
149
lcd.setCursor (0 , 0 );
142
- lcd.print (" Humidity (%): " );
143
- lcd.print (String (humidity));
150
+ lcd.print (" Humidity: " );
151
+ lcd.print (String (int ( humidity)) + " % " );
144
152
145
153
// display Temperature on the LCD screen
146
154
lcd.setCursor (0 , 1 );
147
- lcd.print (" Temp (C): " );
148
- lcd.print (String (temperature));
155
+ lcd.print (" Temp:" );
156
+ lcd.print (String (int (temperature)) + " C" );
157
+
158
+ // display UVIndex on the LCD screen
159
+ lcd.print (" UV:" );
160
+ lcd.print (String (uvIndex));
149
161
150
- String preparedData = prepareDataForWiFi (humidity, temperature, heatIndex);
162
+ String preparedData = prepareDataForWiFi (humidity, temperature, heatIndex, uvIndex );
151
163
if (DEBUG == true ) {
152
164
Serial.println (preparedData);
153
165
}
0 commit comments