-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD_tempF.ino
146 lines (136 loc) · 4.2 KB
/
LCD_tempF.ino
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*Use NodeMCU-32S in adafruit's ESP32
if not available, add the following to File/Preferences/Additional Boards Manaager
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
*/
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <WiFi.h>
#include "time.h"
#include <WiFiUdp.h>
#define DHTPIN 23 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
// set the LCD number of columns and rows
int lcdColumns = 20;
int lcdRows = 4;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
DHT dht(DHTPIN, DHTTYPE);
//const char* ssid = "Quinn and Cole";
const char* ssid = "Quinn and Cole 3";
const char* password = "ClevelandRocks";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -28800;
int daylightOffset_sec = 3600;//3600;
const char* streamId = "tempdata.txt";
char replyPacket[] = "success!";
WiFiUDP Udp;
unsigned int localPort = 1026;
unsigned int localUDPPort = 1026;
int counter = 0;
void setup() {
Serial.begin(9600);
Serial.println(F("Thermostat"));
dht.begin();
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
Serial.print("Connecting to ");
lcd.setCursor(0, 0);
lcd.print(ssid);
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println(WiFi.status());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(WiFi.status());
}
Serial.println("");
Serial.println("WiFi connected.");
lcd.print(" Connected");
delay(5000);
lcd.setCursor(0,0);
lcd.print(" ");
Udp.begin(localPort);
IPAddress ip(10, 0, 0, 20); //hardcoded to Raspi
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// printLocalTime();
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
}
void loop() {
counter++;
//if (counter % 600 == 0)
Serial.println(WiFi.status());
if (WiFi.status() != 3 || counter % 100 == 0 )
{ //reset the wifi just in case
Serial.println("Wifi reconnecting.");
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Udp.begin(localPort);
IPAddress ip(10, 0, 0, 20); //hardcoded to Raspi
}
delay(10000); //10 sec delay
// set cursor to first column, first row
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
Serial.println(&timeinfo, "%l:%M %p");
char time_disp[9];
strftime(time_disp, 9, "%l:%M %p", &timeinfo);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.println(f);
// print message
lcd.setCursor(0, 0);
lcd.print(time_disp);
// lcd.setCursor(0,1);
// lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(f);
lcd.print(" F");
// lcd.setCursor(0,2);
// lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("RH:");
lcd.print(h);
lcd.print("%");
//send data to UDP
char f_char[7] = "999.99";
char rh_char[] = "99";
dtostrf(f, 6, 2, f_char);
dtostrf(h, 2, 1, rh_char);
// replyPacket = f_char + "," + rh_char
IPAddress ip(10, 0, 0, 20); //hardcoded to Raspi
Udp.beginPacket(ip, localUDPPort);
Udp.print(f);
Udp.print(",");
Udp.print(rh_char);
Udp.endPacket();
}