-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_poe_temperatursensor.ino
153 lines (131 loc) · 3.56 KB
/
esp32_poe_temperatursensor.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
147
148
149
150
151
152
153
/*
This sketch shows the Ethernet event usage
*/
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12
#include <ETH.h>
#include <WebServer.h>
#include <DHTesp.h>
/* Put IP Address details */
IPAddress local_ip(192,168,10,95);
IPAddress gateway(192,168,10,1);
IPAddress subnet(255,255,255,0);
IPAddress dns1(192,168,10,10);
WebServer server(80);
DHTesp dht;
char * Hostname = "ESP32-temp-sensor1";
// float Temperature = 10;
int dhtPin = 0;
static bool eth_connected = false;
void WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
// ETH.setHostname("esp32-ethernet");
ETH.setHostname(Hostname);
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}
void testClient(const char * host, uint16_t port)
{
Serial.print("\nconnecting to ");
Serial.println(host);
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
while (client.connected() && !client.available());
while (client.available()) {
Serial.write(client.read());
}
Serial.println("closing connection\n");
client.stop();
}
void handleRoot() {
server.send(200, "text/plain", "hello from esp32!");
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void handleTempReadout() {
delay(dht.getMinimumSamplingPeriod());
float Temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.println("Temperature: " + String(Temperature));
server.send(200, "text/plain", String(Temperature));
}
void handleHumReadout() {
delay(dht.getMinimumSamplingPeriod());
float Humidity = dht.getHumidity();
Serial.print(dht.getStatusString());
Serial.println("Humidity: " + String(Humidity));
server.send(200, "text/plain", String(Humidity));
}
void setup()
{
dht.setup(dhtPin, DHTesp::DHT11);
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
ETH.begin();
ETH.config(IPAddress(local_ip),
IPAddress(gateway),
IPAddress(subnet),
IPAddress(dns1));
server.on("/", handleRoot);
server.on("/test", []() {
server.send(200, "text/plain", "this works as well");
});
server.on("/temp", handleTempReadout);
server.on("/hum", handleHumReadout);
server.onNotFound(handleNotFound);
server.begin();
}
void loop()
{
// if (eth_connected) {
// testClient("google.com", 80);
// testServer();
// }
server.handleClient();
}