-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
WT32-ETH01-i2c-BME280-DS3231.ino
288 lines (222 loc) · 9.72 KB
/
WT32-ETH01-i2c-BME280-DS3231.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// i used Arduino IDE Menu => Tools => Board => 'ESP32 Dev Module'
// powerup WT32-ETH01 with IO0 pulled to ground, remove after programming and powercycle
// used USB FTDI programmer TX->RX0, RX->TX0, 3.3Volt and ground
//tested works with wt32-eth01
//dhcp webserver
//webpage shows button when pressed random data is displayed
// mcu to ajax? String message = "Random data: ";
// message += String(random(10000)); // Get random number
// next i2c data is printed to serial monitor for now
// i2c BME280 temp humid milibar sensor
// i2c DS3231 RTC realtimeclock
// I2C_SCL 32 // WT32-ETH01 CFG = Gpio 32 non standard i2c adress
// I2C_SDA 33 // WT32-ETH01 485_EN = Gpio 33 non standard i2c adress
// https://github.com/ldijkman/WT32-ETH01-LAN-8720-RJ45-/blob/main/WT32-ETH01-i2c-BME280-DS3231.ino
/*
Copyright statement: Part of This article is the original article of the CSDN blogger "Naisu_kun",
which follows the CC 4.0 BY-SA copyright agreement.
For reprinting, please attach the original source link and this statement.
Original link:https://blog.csdn.net/Naisu_kun/article/details/88572129
- Explanation of the code and other parameters: see the link above.
- Note: Original language: Chinese (You can use the Google translator to read English, or another language)
Part of This article is added by luberth dijkman
Copyright 2021 Dirk Luberth Dijkman Bangert 30 1619GJ Andijk The Netherlands
GNU General Public License,
which basically means that you may freely copy, change, and distribute it,
but you may not impose any restrictions on further distribution,
and you must make the source code available.
All above must be included in any redistribution
*/
#include <ETH.h> // quote to use ETH
#include <WiFi.h>
#include <WebServer.h> // Introduce corresponding libraries
#include <Wire.h>
#define I2C_SCL 32 // WT32-ETH01 CFG = Gpio 32 non standard i2c adress
#define I2C_SDA 33 // WT32-ETH01 485_EN = Gpio 33 non standard i2c adress
#include "RTClib.h" // https://github.com/adafruit/RTClib
RTC_DS3231 rtc; // download zip from above and install library from zip
// Sketch=>include library=>Add ZIP Library
#include <Adafruit_Sensor.h> // used zip from https://github.com/adafruit/Adafruit_Sensor
#include <Adafruit_BME280.h> // used zip from https://github.com/adafruit/Adafruit_BME280_Library
// Sketch=>include library=>Add ZIP Library
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 BME280; // I2C
bool BME280_status;
int last_second = 0, second_ = 0; // some variables for time
unsigned long unix_epoch;
const char *monthName[12] = {"Januari", "Februari", "March", "April", "May", "Juni",
"Juli", "Augustus", "September", "October", "November", "December"
};
char dayname[7][12] = {"Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "};
#define ETH_ADDR 1
#define ETH_POWER_PIN 16//-1 //16 // Do not use it, it can cause conflict during the software reset.
#define ETH_POWER_PIN_ALTERNATIVE 16 //17
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT // ETH_CLOCK_GPIO0_IN
IPAddress local_ip(192, 168, 178, 112);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2 = (uint32_t)0x00000000;
// Web page
String myhtmlPage =
String("") + "\r\n" +
"<html>" + "\r\n" +
"<head>" + "\r\n" +
" <title>ESP32 WebServer Test</title>" + "\r\n" +
" <script>" + "\r\n" +
" function getData() {" + "\r\n" +
" var xmlhttp;" + "\r\n" +
" if (window.XMLHttpRequest) {" + "\r\n" +
" xmlhttp = new XMLHttpRequest();" + "\r\n" +
" }" + "\r\n" +
" else {" + "\r\n" +
" xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");" + "\r\n" +
" }" + "\r\n" +
" xmlhttp.onreadystatechange = function() {" + "\r\n" +
" if (this.readyState == 4 && this.status == 200) {" + "\r\n" +
" document.getElementById(\"txtRandomData\").innerHTML = this.responseText;" + "\r\n" +
" }" + "\r\n" +
" };" + "\r\n" +
" xmlhttp.open(\"GET\", \"getRandomData\", true); " + "\r\n" +
" xmlhttp.send();" + "\r\n" +
" }" + "\r\n" +
" </script>" + "\r\n" +
"</head>" + "\r\n" +
"<body>" + "\r\n" +
" <div id=\"txtRandomData\">Unkwon</div>" + "\r\n" +
" <input type=\"button\" value=\"random\" onclick=\"getData()\">" + "\r\n" +
"</body>" + "\r\n" +
"</html>";
WebServer server(80); // Declare the WebServer object
void handleRoot() // Callback
{
server.send(200, "text/html", myhtmlPage); //!!! Note that returning to the web page requires "text / html" !!!
}
void handleAjax() // Callback
{
String message = "Random data: ";
message += String(random(10000)); // Get random number
server.send(200, "text/plain", message); // Send message back to page
}
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");
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 setup() {
pinMode(ETH_POWER_PIN_ALTERNATIVE, OUTPUT);
digitalWrite(ETH_POWER_PIN_ALTERNATIVE, HIGH);
Serial.begin(115200);
delay(2500);
Serial.println("hello");
Wire.begin(I2C_SDA, I2C_SCL); // start i2c on non stndard i2c pins
WiFi.onEvent(WiFiEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); // Enable ETH
//ETH.config(local_ip, gateway, subnet, dns1, dns2); // Static IP, leave without this line to get IP via DHCP
while(!((uint32_t)ETH.localIP())){}; // Waiting for IP (leave this line group to get IP via DHCP)
server.on("/", handleRoot); // Register link and callback function
server.on("/getRandomData", HTTP_GET, handleAjax); // Request and callback function of the get method sent by ajax in the registration web page
server.begin(); // Start server
Serial.println("Web server started");
if (! rtc.begin()) {
Serial.println("Could not find RTC, i2c DS3231 realtimeclock not found");
}
BME280_status = BME280.begin(0x76);
if (!BME280_status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
// set the time from PC to DS3231 RTC
if (rtc.lostPower() ) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// 2020 juli 24 20:15:00 you would call:
// rtc.adjust(DateTime(2020, 7, 24, 20, 15, 0));
}
}
void loop() {
DateTime now = rtc.now(); // DS3231 RTC RealTimeClock
second_ = now.second();
if (last_second != second_) { // do next only once each new second
last_second = second_;
if (now.hour() < 10)Serial.print(" "); // print 01 to 09 as 1 to 9
Serial.print(now.hour());
Serial.print(":");
if (now.minute() < 10)Serial.print("0"); // print 1 to 9 as 01 to 09
Serial.print(now.minute());
Serial.print(":");
if (now.second() < 10)Serial.print("0"); // print 1 to 9 as 01 to 09
Serial.print(now.second());
Serial.print("Day ");
Serial.print(now.dayOfTheWeek()); // prints daynumber of the week, weekend sux, sunday=0, saturday=6
Serial.print(dayname[now.dayOfTheWeek()]);
Serial.print(now.day());
Serial.print(" ");
Serial.print(monthName[now.month() - 1]);
Serial.print(" ");
Serial.print(now.year());
Serial.print(" ");
float TempCelsius;
TempCelsius = BME280.readTemperature(); // get temp from BME280
//TempCelsius = TempCelsius + CalibrationOffset;
// Serial.print("Fahrenheit = "); Serial.print(Tf, 1);
Serial.print(" Celsius = "); Serial.println(TempCelsius, 1);
// https://www.google.com/search?q=3+fahrenheit+to+celsius
Serial.print("Temperature = ");
Serial.print(TempCelsius);
Serial.println(" *C");
// Convert temperature to Fahrenheit
// Serial.print("Temperature = ");
// Serial.print(1.8 * BME280.readTemperature() + 32);
// Serial.println(" *F");
Serial.print("Pressure = ");
Serial.print(BME280.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(BME280.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(BME280.readHumidity());
Serial.println(" %");
Serial.println();
} // end last_second not is second_
server.handleClient(); // Handling requests from clients
}
// A Penny for Sharing My Thoughts?
// www.paypal.me/LDijkman