Skip to content

Commit 9ead2fc

Browse files
authored
Merge pull request #4 from MecaHumArduino/uv-sensor
Uv sensor
2 parents 186d4cf + 38001d4 commit 9ead2fc

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

.gitignore

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,4 @@
44
.vscode/launch.json
55
.vscode/ipch
66
.DS_Store
7-
aws/lambdas/weather-station-writer/.serverless
8-
aws/cfn-templates/real-time-iot-device-monitoring-with-kinesis.template
9-
cmake-build-uno/
10-
cmake-build-debug
11-
CMakeLists.txt
12-
CMakeListsPrivate.txt
13-
.idea
7+
aws/lambdas/weather-station-writer/.serverless

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"guva"
4+
]
5+
}

src/main.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,30 @@
55
@version 3.0
66
*/
77

8-
#include <Arduino.h>
98
#include <DHT.h>
10-
#include <LiquidCrystal.h>
9+
#include <Arduino.h>
1110
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
11+
#include <LiquidCrystal.h>
1212
#include <SoftwareSerial.h>
1313

1414
#define DEBUG true
1515

1616
#define DHT_PIN 4 // pin connected to data pin of DHT11
1717
#define DHT_TYPE DHT11 // Type of the DHT Sensor, DHT11/DHT22
18-
19-
DHT sensor(DHT_PIN, DHT_TYPE);
18+
int uvAnalogIn = A0;
2019

2120
// initialize the library with the numbers of the interface pins
2221
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
2322

23+
DHT temperatureSensor(DHT_PIN, DHT_TYPE);
24+
2425
// ESP TX => Uno Pin 2
2526
// ESP RX => Uno Pin 3
2627
SoftwareSerial wifi(2, 3);
2728

2829
// **************
2930
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);
3132
void setup();
3233
void loop();
3334
// **************
@@ -39,13 +40,14 @@ void loop();
3940
* @param heatIndex
4041
* @return
4142
*/
42-
String prepareDataForWiFi(float humidity, float temperature, float heatIndex)
43+
String prepareDataForWiFi(float humidity, float temperature, float heatIndex, float uvIndex)
4344
{
4445
StaticJsonDocument<200> doc;
4546

4647
doc["humidity"] = String(humidity);
4748
doc["temperature"] = String(temperature);
4849
doc["heat_index"] = String(heatIndex);
50+
doc["uv_index"] = String(uvIndex);
4951

5052
char jsonBuffer[512];
5153
serializeJson(doc, jsonBuffer);
@@ -84,6 +86,9 @@ String sendDataToWiFiBoard(String command, const int timeout, boolean debug)
8486
}
8587

8688
void setup() {
89+
// read from GUVA-S12SD
90+
pinMode(uvAnalogIn, INPUT);
91+
8792
Serial.begin(9600);
8893
wifi.begin(9600);
8994

@@ -94,10 +99,10 @@ void setup() {
9499
lcd.setCursor(0, 0);
95100

96101
// display a welcome message
97-
lcd.print("Temp & Humidity");
102+
lcd.print("Temp, Humidity");
98103
// go to second line
99104
lcd.setCursor(0, 1);
100-
lcd.print("Sensor");
105+
lcd.print("and UV Sensor");
101106

102107
lcd.blink();
103108
delay(3000);
@@ -124,30 +129,37 @@ void loop() {
124129
Serial.println(" endbuffer");
125130
}
126131

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();
132134

135+
float temperature = temperatureSensor.readTemperature(); // return temperature in °C
136+
float humidity = temperatureSensor.readHumidity(); // return humidity in %
133137
// 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;
135143

136144
// check whether reading was successful or not
137145
if (temperature == NAN || humidity == NAN) { // NAN means no available data
138146
lcd.print("Reading failed.");
139147
} else {
140148
// display Humidity on the LCD screen
141149
lcd.setCursor(0, 0);
142-
lcd.print("Humidity (%): ");
143-
lcd.print(String(humidity));
150+
lcd.print("Humidity:");
151+
lcd.print(String(int(humidity)) + "% ");
144152

145153
// display Temperature on the LCD screen
146154
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));
149161

150-
String preparedData = prepareDataForWiFi(humidity, temperature, heatIndex);
162+
String preparedData = prepareDataForWiFi(humidity, temperature, heatIndex, uvIndex);
151163
if (DEBUG == true) {
152164
Serial.println(preparedData);
153165
}

0 commit comments

Comments
 (0)