-
Notifications
You must be signed in to change notification settings - Fork 1
/
frugal_clock.ino
175 lines (140 loc) · 4.83 KB
/
frugal_clock.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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "<SSID>";
const char *password = "<PSK>";
char hstring[3] = "12"; // init
char mstring[3] = "1"; // init
int iHr = 1;
int iMin = 0;
ESP8266WebServer server ( 80 );
void handleRoot() {
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
server.send ( 200, "text/html", "Hello from the Frugal Clock!" );
}
void setTime() {
if (server.arg("h")== "" && server.arg("m") == ""){ //Parameter not found
Serial.println("Parameter 'h' (hour) and/or 'm' (minute) not specified");
server.send(200, "text/html", "<H3>Parameter 'h' (hour) and/or 'm' (minute) not specified</H3>");
} else { //Parameter found
if (server.arg("h") != "") { (server.arg("h")).toCharArray(hstring, 1 + (server.arg("h")).length()); iHr = String(hstring).toInt(); }
if (server.arg("m") != "") { (server.arg("m")).toCharArray(mstring, 1 + (server.arg("m")).length()); iMin = String(mstring).toInt(); }
Serial.println("time set to " + String(hstring) + ":" + String(mstring));
server.send(200, "text/html", "<H3>time set to " + String(hstring) + ":" + String(mstring) + "</H3>");
}
}
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 );
}
// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// DP,G,F,E,D,C,B,A
const int segmentPins[] = { D1,D1,D2,D3,D4,D5,D6,D8 };
const int nbrDigits= 2; // the number of digits in the LED display
//dig 0 1
const int digitPins[nbrDigits] = { D0,D7 };
const int dp_pin = 3;
void setup()
{
Serial.begin(115200);
WiFi.begin ( ssid, password );
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
server.on("/", handleRoot);
server.on("/set", setTime);
server.onNotFound ( handleNotFound );
server.begin();
for(int i=0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
}
pinMode(dp_pin, FUNCTION_3);
pinMode(dp_pin, OUTPUT);
for(int i=0; i < nbrDigits; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int number, int digit)
{
if(number == -1) digitalWrite( digitPins[digit], LOW );
else digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment < 8; segment++) {
boolean isBitSet = bitRead(numeral[number], segment);
// isBitSet will be true if given bit is 1
// isBitSet = ! isBitSet; // Code Option*
// uncomment the above Code Option line for common anode display
digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}
void showNumber( int number, boolean zPad)
{
if(number < 10) {
if(zPad) showDigit( 0, nbrDigits-2) ;
}
if(number == 0) {
showDigit( 0, nbrDigits-1) ;
// showDigit( 0, nbrDigits-2) ;
} else if(number == -1) {
showDigit(number, 0);
showDigit(number, 1);
} else {
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--) {
if(number > 0) {
showDigit( number % 10, digit) ;
number = number / 10;
}
}
//if(number < 10) showDigit( 0, 0) ;
}
}
void loop()
{
digitalWrite( dp_pin, LOW );
for(int i=0; i<200; i++) { showNumber(iHr, false); server.handleClient(); } // Display Hours
digitalWrite( dp_pin, HIGH );
for(int i=0; i<200; i++) { showNumber(iMin, true); server.handleClient(); } // Display Minutes
for(int i=0; i<100; i++) { showNumber(-1, false); server.handleClient(); } // Switch OFF bboth digits
}