-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanctl_2.0.ino
104 lines (88 loc) · 2.34 KB
/
fanctl_2.0.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
//#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiSetup.h>
#include "vars.h"
// Create WiFiSetup object
WiFiSetup wifiSetup("SuperFanMan", "fanman123", 80);
// Global variable to store current fan speed
int currentFanSpeed;
// Send main fan control page
void showFanControls() {
wifiSetup.server.send(200, "text/html", fanControlPage);
}
// Set fan speed to zero
void off() {
// Turn fan off or to zero percent
currentFanSpeed = FAN_SPEED_OFF;
showFanControls();
}
// Set fan speed to fifty percent we start at 50 percent because 25% isn't enough to even turn the fan
void low() {
// Set fan to fifty percent
currentFanSpeed = FAN_SPEED_LOW;
showFanControls();
Serial.println("Fan Low");
}
// Set fan to seventy five percent
void medium() {
// Set fan to seventy five percent
currentFanSpeed = FAN_SPEED_MEDIUM;
showFanControls();
Serial.println("Fan Medium");
}
// Set fan to one hundred percent
void high() {
// Set fan to full or one hundred percent
currentFanSpeed = FAN_SPEED_HIGH;
showFanControls();
Serial.println("Fan High");
}
// Generate fan speed signal based on passed fanSpeed
void setFanTo(int fanSpeed) {
switch (fanSpeed) {
case FAN_SPEED_OFF:
digitalWrite(PWM_PIN, LOW);
break;
case FAN_SPEED_LOW:
digitalWrite(PWM_PIN, HIGH);
delayMicroseconds(fanSpeed * 20);
digitalWrite(PWM_PIN, LOW);
delayMicroseconds(fanSpeed * 10);
break;
case FAN_SPEED_MEDIUM:
digitalWrite(PWM_PIN, HIGH);
delayMicroseconds(fanSpeed * 10);
digitalWrite(PWM_PIN, LOW);
delayMicroseconds((fanSpeed / 3) * 10);
break;
case FAN_SPEED_HIGH:
digitalWrite(PWM_PIN, HIGH);
break;
default:
Serial.println("Invalid fan speed.");
break;
}
}
void setup() {
Serial.begin(115200);
wifiSetup.setAppPage(fanControlPage);
wifiSetup.begin();
pinMode(PWM_PIN, OUTPUT);
while (!Serial) {
;
}
// Set initial fan speed to zero
currentFanSpeed = FAN_SPEED_OFF;
// Serve main page
wifiSetup.server.on("/off", off);
wifiSetup.server.on("/low", low);
wifiSetup.server.on("/medium", medium);
wifiSetup.server.on("/high", high);
// Print board's IP
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
wifiSetup.handleClient();
setFanTo(currentFanSpeed);
}