forked from alex-mcdermott/MX-Break-in-Machine-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch Machine Webui.txt
78 lines (58 loc) · 1.89 KB
/
Switch Machine Webui.txt
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
//PURPOSE OF SCRIPT
//Create webpage
//Count number of presses and presses per minute for button connected to pin D2.
//Output this to web page
//Hardware is Wemos D1 Mini
// Include libraries
#include <WiFi.h>
#include <WebServer.h>
// Set up the ESP32 webserver
WebServer server(80);
// Set up a variable to count the number of button presses
int buttonPresses = 0;
// Set up a variable to count the number of presses per minute
int pressesPerMin = 0;
// Set up the pins
#define BUTTON_PIN D2
// Create a function to handle the button press
void handleButtonPress() {
buttonPresses++; // Increment the button presses
pressesPerMin++; // Increment the presses per minute
}
// Setup the ESP32
void setup() {
Serial.begin(115200);
// Connect to the wifi
WiFi.begin("The House That Jack Built", "Huskytree406");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
// Print the IP address
Serial.println(WiFi.localIP());
// Set up the button pin
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set up the web server
server.on("/", [](){
String response = "Button presses: " + String(buttonPresses) + "<br>Presses per minute: " + String(pressesPerMin);
server.send(200, "text/plain", response);
});
server.begin();
Serial.println("Server started");
// Attach an interrupt to the button pin
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, RISING);
// Set up a timer to reset the presses per minute
timer1_isr_init();
timer1_attachInterrupt(resetPressesPerMin);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(60000);
}
// Reset the presses per minute
void resetPressesPerMin() {
pressesPerMin = 0;
}
// Main loop
void loop() {
server.handleClient();
}