1/*
2 * Arduino Weather Station - Receiver Unit
3 * Code by Andrew Lorimer (http://lorimer.id.au)
4 * Credit to @bram2202 (Instructables) and Mike McCauley (mikem@airspayce.com)
5 */
6
7
8// Libraries
9#include <VirtualWire.h> // Communicates with 433 MHz receiver
10#include <LiquidCrystal.h> // Displays text on LCD
11
12// Objects and pins
13LiquidCrystal lcd(7, 8, 5, 4, 3, 2); //LCD Pins
14#define pir A0 // Pin for PIR input
15#define backpin A1 // Pin for backlight output
16
17// Data received ([0] = current; [1] = min; [2] = max)
18String TempC[3]; // Temp (deg C)
19String Humidity[3]; // Humidity (%)
20String Pressure[3]; // Pressure (Pa)
21
22
23
24
25
26
27void setup() {
28
29 Serial.begin(9600);
30 Serial.println("Starting setup routine...");
31
32 // Motion sensor
33 pinMode(pir, INPUT);
34 Serial.println("> Successfully started motion sensor");
35
36 // LCD Display
37 pinMode(backpin, OUTPUT); // Set backlight pin
38 lcd.begin(20, 4); // Set resolution
39
40 // Show splash
41 lcd.setCursor(0, 1); // Set cursor at first line
42 lcd.print(" arduino "); // Print splash
43 lcd.setCursor(0, 2); // Set cursor at second line
44 lcd.print(" weather "); // Print splash
45 lcd.display();
46 Serial.println("> Successfully started LCD display");
47
48 // Wireless receiver
49 pinMode(11, INPUT);
50 vw_set_rx_pin(11);
51 vw_set_ptt_inverted(true); // Required for DR3100
52 vw_setup(2000); // Bits per sec
53 vw_rx_start(); // Start the receiver PLL running
54 Serial.println("> Successfully started 433 MHz receiver");
55 Serial.println("Finished setup routine.\n");
56
57 // Buttons
58 pinMode(A2, INPUT);
59 pinMode(A3, INPUT);
60
61 delay(1500);
62}
63
64
65
66
67void loop() {
68
69 analogWrite(backpin, analogRead(pir)); // If PIR is high, LCD backlight goes on. Duration & sensitivity adjusted on sensor.
70 Serial.println(analogRead(pir));
71 // Receive data
72 uint8_t buf[VW_MAX_MESSAGE_LEN];
73 uint8_t buflen = VW_MAX_MESSAGE_LEN;
74
75 if (vw_get_message(buf, &buflen)) {
76 digitalWrite(13, true); // Flash onboard LED
77 char Chars[buflen];
78 for (int i = 0; i < buflen; i++) {Chars[i] = char(buf[i]);} // From buffer to char array
79 Decode(Chars); // Send to deconder
80 digitalWrite(13, false); // Turn off onboard LED
81 }
82
83 if (analogRead(A2) > 1020) {
84 DisplayMin();
85 delay(7000);
86 DisplayCurrent();
87 }
88 if (analogRead(A3) > 1020) {
89 DisplayMax();
90 delay(7000);
91 DisplayCurrent();
92 }
93}
94
95
96
97
98void Decode(char* Raw) { // Filter wireless signals and put data into strings
99
100 String Code = (String)Raw; // From char array to string
101
102 if (Code.startsWith("#C")) {
103 TempC[0] = Code.substring(2,7); // Get chars 2-7
104 if (TempC[1] == "") {TempC[1] = TempC[0]; TempC[2] = TempC[0];}
105 if (TempC[0] < TempC[1]) {TempC[1] = TempC[0];}
106 if (TempC[0] > TempC[2]) {TempC[2] = TempC[0];}
107 Serial.println("Current Temp: " + TempC[0]);
108 Serial.println("Min Temp: " + TempC[1]);
109 Serial.println("Max Temp: " + TempC[2] + "\n");
110 }
111
112 if (Code.startsWith("#H")) {
113 Humidity[0] = Code.substring(2,4); // Get chars 2-4
114 if (Humidity[1] == "") {Humidity[1] = Humidity[0]; Humidity[2] = Humidity[0];}
115 if (Humidity[0] < Humidity[1]) {Humidity[1] = Humidity[0];}
116 if (Humidity[0] > Humidity[2]) {Humidity[2] = Humidity[0];}
117 Serial.println("Current Humidity: " + Humidity[0]);
118 Serial.println("Min Humidity: " + Humidity[1]);
119 Serial.println("Max Humidity: " + Humidity[2] + "\n");
120 }
121
122 if (Code.startsWith("#P")) {
123 Pressure[0] = Code.substring(2,8); // Get chars 2-8
124 if (Pressure[1] == "") {Pressure[1] = Pressure[0]; Pressure[2] = Pressure[0];}
125 if (Pressure[0] < Pressure[1]) {Pressure[1] = Pressure[0];}
126 if (Pressure[0] > Pressure[2]) {Pressure[2] = Pressure[0];}
127 Serial.println("Current Pressure: " + Pressure[0]);
128 Serial.println("Min Pressure: " + Pressure[1]);
129 Serial.println("Max Pressure: " + Pressure[2] + "\n");
130 }
131
132 DisplayCurrent();
133
134}
135
136
137
138
139void DisplayCurrent() { // Show text on screen
140
141 lcd.clear(); // Clear LCD
142 lcd.begin(20, 4); // Set resolution
143
144 if (TempC[0] != "") {
145 lcd.setCursor(0, 0); // Set cursor position to origin
146 lcd.print(((String)" Temp. " + TempC[0] + " oC")); // Print received data
147 }
148
149 if (Humidity[0] != "") {
150 lcd.setCursor(0, 1); // Set cursor position to second line
151 lcd.print(((String)"Humid. " + Humidity[0] + " %")); // Print received data
152 }
153
154 if (Pressure[0] != "") {
155 lcd.setCursor(0, 2); // Set cursor position to third line
156 lcd.print(((String)"Press. " + Pressure[0] + " hPa")); // Print received data
157 }
158
159 lcd.display(); // Display data in buffer
160
161}
162
163
164
165
166void DisplayMin() { // Show text on screen
167
168 lcd.clear(); // Clear LCD
169 lcd.begin(20, 4); // Set resolution
170
171 if (TempC[1] != "") {
172 lcd.setCursor(0, 0); // Set cursor position to origin
173 lcd.print(((String)" Temp. " + TempC[1] + " oC")); // Print received data
174 }
175
176 if (Humidity[1] != "") {
177 lcd.setCursor(0, 1); // Set cursor position to second line
178 lcd.print(((String)"Humid. " + Humidity[1] + " %")); // Print received data
179 }
180
181 if (Pressure[1] != "") {
182 lcd.setCursor(0, 2); // Set cursor position to third line
183 lcd.print(((String)"Press. " + Pressure[1] + " hPa")); // Print received data
184 }
185
186 lcd.display(); // Display data in buffer
187
188}
189
190
191
192
193void DisplayMax() { // Show text on screen
194
195 lcd.clear(); // Clear LCD
196 lcd.begin(20, 4); // Set resolution
197
198 if (TempC[2] != "") {
199 lcd.setCursor(0, 0); // Set cursor position to origin
200 lcd.print(((String)" Temp. " + TempC[2] + " oC")); // Print received data
201 }
202
203 if (Humidity[2] != "") {
204 lcd.setCursor(0, 1); // Set cursor position to second line
205 lcd.print(((String)"Humid. " + Humidity[2] + " %")); // Print received data
206 }
207
208 if (Pressure[2] != "") {
209 lcd.setCursor(0, 2); // Set cursor position to third line
210 lcd.print(((String)"Press. " + Pressure[2] + " hPa")); // Print received data
211 }
212
213 lcd.display(); // Display data in buffer
214
215}