-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAufgabe.ino
158 lines (113 loc) · 3.34 KB
/
Aufgabe.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
154
155
156
157
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <iostream>
#include <ostream>
#include <string>
#include <OneWire.h>
#include <DallasTemperature.h>
float newTemperature;
float temperature;
float finalTemperature;
float Test = 388;
unsigned long zeita = 0;
//Farben der Kabel am Sensor:
//GND = GRAU !!
//3V3 = LILA !!
//D4 = GRÜN !!
#define ONE_WIRE_BUS_2 D4
OneWire oneWire_out(ONE_WIRE_BUS_2);
WiFiServer server(80);
DallasTemperature sensor_outhouse(&oneWire_out);
void mittelwert() {
if(millis() > zeita) {
for (size_t i = 0; i < 7; i++){
sensor_outhouse.requestTemperatures();
temperature = sensor_outhouse.getTempCByIndex(0);
newTemperature = sensor_outhouse.getTempCByIndex(0) + temperature;
zeita = millis() + 10000;
Serial.print(newTemperature);
}
}
finalTemperature = newTemperature/6;
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
//pinMode(4, INPUT_PULLUP);
sensor_outhouse.begin();
WiFi.begin("Enrichment", "enrichment");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected");
server.begin();
Serial.printf("Web server gestartet, offen %s im Browser\n", WiFi.localIP().toString().c_str());
}
String prepareHtmlPage()
{
sensor_outhouse.requestTemperatures();
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 3\r\n" // refresh the page automatically every 3 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta charset='utf-8'>"
"<titel>Temp-Arduino</title>"
"</head>"
"<body>"
"<p>Aktuelle Temperatur: </p>");
htmlPage += sensor_outhouse.getTempCByIndex(0);
htmlPage += F(
"<p>Durschnitt über 6 Werte: </p>");
htmlPage += finalTemperature;
htmlPage += F(
"</body>"
"</html>"
"\r\n");
return htmlPage;
}
void loop(void)
{
mittelwert();
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
while (client.available()) {
// but first, let client finish its request
// that's diplomatic compliance to protocols
// (and otherwise some clients may complain, like curl)
// (that is an example, prefer using a proper webserver library)
client.read();
}
// close the connection:
client.stop();
Serial.println("[Client disconnected]");
}
}