-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyWeather2.ino
More file actions
123 lines (106 loc) · 3.67 KB
/
myWeather2.ino
File metadata and controls
123 lines (106 loc) · 3.67 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*********************************************************************
This is an example of a solution which employs Arduino along with the
DHT11 Temperature and Humidy sensor and SSD1306 display
This example is for a 128x64 size display using I2C to communicate
3 pins are required to interface (2 I2C and one reset)
Written by Johnathan Lightfoot
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
#include <Wire.h>
// two libraries inserted into the 'libraries' folder
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// dht library
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET); //Resets the OLED
//#define DHTTYPE DHT22
#define DHTTYPE DHT11 // DHT 11
//Checks if the display is of the correct size
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//Declares DHT pin and type
DHT dht(DHTPIN, DHTTYPE);
//Set hot and cold limits
const int hot = 82.0; //set hot parameter
const int cold = 75.0; //set cold parameter
void setup() {
//Define pin inputs and outputs
pinMode(A2, INPUT); //sensor
pinMode(3, OUTPUT); //blue
pinMode(4, OUTPUT); //green
pinMode(5, OUTPUT); //red
//Begin Serial port for displaying on data in the Serial Monitor
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
dht.begin();
delay(2000);
}
void loop() {
//int sensor = analogRead(A2);
//float voltage = (sensor / 1024.0) * 5.0;
//float tempC = (voltage - .5) * 100;
//float tempF = (tempC * 1.8) + 32;
//Serial.print(F("temp: "));
//Serial.print(tempF);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
//display.println(F("Inside Tmp"));
float h = dht.readHumidity();
// Read temperature as Celsius (isCelsius = true)
float t = dht.readTemperature(true);
// Read temperature as Fahrenheit (the default)
float f = dht.readTemperature();
// Check if any reads failed.
if (isnan(h) || isnan(t) || isnan(f)) {
delay(2000);
} else{
// routine for converting temp/hum floats to char arrays
char temp_buff[5]; char hum_buff[5];
char temp_disp_buff[11] = "Temp:";
char hum_disp_buff[11] = "Hum:";
// appending temp/hum to buffers
dtostrf(t,2,1,temp_buff);
strcat(temp_disp_buff,temp_buff);
dtostrf(h,2,1,hum_buff);
strcat(hum_disp_buff,hum_buff);
if (dht.readTemperature(true) < cold) { //cold
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
display.println(F("Too Cold"));
//Serial.println(F(" Too Cold."));
Serial.println(dht.readTemperature(true));
}
else if (dht.readTemperature(true) >= hot) { //hot
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
display.println(F("Too Hot"));
//Serial.println(F(" Too Hot."));
Serial.println(f = dht.readTemperature(true));
}
else { //fine
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
display.println(F("Ideal"));
//Serial.println(F(" It's Fine."));
Serial.println(f = dht.readTemperature(true));
}
// routine for displaying text for temp/hum readout
display.setCursor(0,17);
display.println(temp_disp_buff);
display.println(hum_disp_buff);
display.display();
delay(2000);
}
}