-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDigiTempClient.h
executable file
·150 lines (137 loc) · 4.51 KB
/
DigiTempClient.h
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
147
148
149
150
#ifndef DIGITEMP_CLIENT
#define DIGITEMP_CLIENT
/*
*
* Client for ESP8266 with DHT sensor
* Auto connect with last used network able to enter new SSID and Passphrase
*
* Victor Wheeler [email protected]
*
*/
#define client_copyrite " © Jan 2021 VictorWheeler [email protected] use, modify and distribute without restrictions"
#define client_compiledate __DATE__
#include "DigiTempESP.h"
void setupClient(){
WiFi.mode(WIFI_STA);
#if AUTOCONNECT // AutoConnect obtain credentials from web page
WiFiManager wm;
wm.autoConnect(MYHOSTNAME"_AP");
#else // Hard-coded connection
WiFi.begin(ssid, password); // auto generated AP name from chipid
Serial.println(ssid);
Serial.println(password);
#endif
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(250); yield();
Serial.print(".");
delay(250); yield();
}
Serial.println("");
Serial.print(F("Connected to "));
Serial.println(ssid);
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
Serial.print(F("Soft IP address: "));
Serial.println(WiFi.softAPIP().toString());
Serial.print(F("GateWay IP address: "));
Serial.println(WiFi.gatewayIP().toString());
}
void rootPage() {
if(DEBUG) Serial.println("Hello from rootPage()");
String content;
if(isnan(Th_t.c)){
content =
"<html>"
"<head>"
"<meta name=\"R_LabDigiTmp\" content=\"width=device-width, initial-scale=1\">"
"<script type=\"text/javascript\">"
"setTimeout(\"location.reload()\", 30000);"
"</script>"
"</head>"
"<body>"
"<h2 align=\"center\" style=\"color:blue;margin:20px;\">You are connected</h2>"
"<h2 align=\"center\" style=\"color:lightgray;margin:20px;\">{{MyIP}} {{myHostName}}</h2>"
"</body>"
"</html>";
} else {
content=
"<html>"
"<head>"
"<meta name=\"R_LabDigiTmp\" content=\"width=device-width, initial-scale=1\">"
"<script type=\"text/javascript\">"
"setTimeout(\"location.reload()\", 15000);"
"</script>"
"</head>"
"<body>"
"<h2 align=\"center\" style=\"color:lighttgray;margin:20px;\">{{MyIP}} {{myHostName}}</h2>"
"<h3 align=\"center\" style=\"color:blue;margin:15px;\">{{TempHumi}}</h3>"
"<h3 align=\"center\" style=\"color:gray;margin:15px;\">{{HighLow}}</h3>"
"</body>"
"</html>";
char tempHumi[80];
char highlow[40];
// "Humidity: 44.60% Temperature: 22.40 *C 72.32 *F Heat index: 21.86 *C 71.35 *F
sprintf(tempHumi,"Humidity: %02.1f%%\t\tTemperature: %02.2fC / %02.2fF\t\tHeat index: %04.2fC / %04.2fF",
Th_t.h, Th_t.c, Th_t.f, Th_t.hic, Th_t.hif);
content.replace("{{TempHumi}}", String(tempHumi));
if(scale){
sprintf(highlow, "High: %02.2fC\t\tLow: %02.2fC",dht.convertFtoC(Th_t.tmaxf),dht.convertFtoC(Th_t.tminf));
} else {
sprintf(highlow, "High: %02.2fF\t\tLow: %02.2fF",Th_t.tmaxf,Th_t.tminf);
}
content.replace("{{HighLow}}", String(highlow));
}
content.replace("{{MyIP}}", String(WiFi.localIP().toString()));
content.replace("{{UID}}", SUID);
content.replace("{{myHostName}}",hostName);
Server.send(200, "text/html", content);
Server.client().flush();
}
void my_setup() {
hostName = MY_HOSTNAME;
setupClient();
Serial.print("Hello from DigiTemp Client ");
Serial.println(hostName);
Serial.println(client_copyrite);
Serial.println(client_compiledate);
Serial.println(Server.client().localIP().toString());
}
/*
*
* Kind of a watch-dog if this client has not been accessed in 300 seconds
* Re establish the connection.
*
*
*/
// int still_here defined and reset in getData()
boolean still_alive(){
boolean retval = true;
if(still_here > 300){ // Number of seconds to try reconnect
if(DEBUG){Serial.print(F("Reseting connection to host still_here = ")); Serial.println(still_here);}
if(WiFi.reconnect()) {
still_here = 0;
} else {
retval = false;
}
}
return retval;
}
void my_loop() {
if(!still_alive()){
Serial.println(F("*** Connection lost ***"));
}
}
/*
* Process any serial monitor input for this client
*/
void do_serial(char r){
if(r == '?') loopDHT(); // Need to do something with r may as well force update data
Serial.print(hostName);
Serial.print("\t");
Serial.print(WiFi.localIP().toString());
Serial.print("\t");
Serial.println(client_compiledate);
}
#endif