-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoffeeTimer.ino
More file actions
231 lines (196 loc) · 6.45 KB
/
Copy pathcoffeeTimer.ino
File metadata and controls
231 lines (196 loc) · 6.45 KB
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
//display connect ip address
//connect to network
//set time from NTP
//get the time from the clock
//display time and time since coffee brewed
//send messge to slack when brewed
//send the time coffee was brewed to GitHub for storage?
//reset NTP every 6 hours if it is available
//TODO: OTA updates
//TODO: SECURITY
#include <Ticker.h>
Ticker displayer;
const int buttonPin = D3; // the number of the pushbutton pin constant
int buttonState = HIGH; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
/*
CONFIGURATION
const String slack_hook_url = "https://hooks.slack.com/services/123456789";
*/
#include "config.h"
const String slack_message = "A rockstar made coffee!!!";
const String slack_username = "BunnBot";
const String icon_emoji = ":coffee:";
#include <ESP8266WiFi.h>
//WIFI MANAGER
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager0
//WiFiClient client;
// or... use WiFiFlientSecure for SSL
WiFiClientSecure client;
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
//SDA>D2 AND SCL>D1
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <SNTPtime.h>
SNTPtime NTPch("ca.pool.ntp.org");
strDateTime dateTime;
strDateTime madeTimeF;
unsigned long madeTime;
unsigned long elapsed;
#include <Timezone.h>
//TimeZone
TimeChangeRule myDST = {"PDT", Second, Sun, Mar, 2, -420}; //Daylight time = UTC - 7 hours
TimeChangeRule mySTD = {"PST", First, Sun, Nov, 2, -480}; //Standard time = UTC - 8 hours
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
time_t utc, local;
byte actualHour = dateTime.hour;
byte actualMinute = dateTime.minute;
byte actualSecond = dateTime.second;
int actualYear = dateTime.year;
byte actualMonth = dateTime.month;
byte actualDay = dateTime.day;
byte actualDayofWeek = dateTime.dayofWeek;
boolean daylight = dateTime.valid;
void LCDdisplay()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(dateTime.hour);
lcd.print(":");
if (dateTime.minute < 10)
{
lcd.print("0");
}
lcd.print(dateTime.minute);
lcd.setCursor(5, 0);
lcd.print(" Brew:");
if (madeTimeF.hour)
{
lcd.print(madeTimeF.hour);
lcd.print(":");
if (madeTimeF.minute < 10)
{
lcd.print("0");
}
lcd.print(madeTimeF.minute);
}
lcd.setCursor(0, 1);
elapsed = (time(NULL) - madeTime) / 60;
lcd.print("Elapsed:");
lcd.print(elapsed);
lcd.setCursor(13, 1);
lcd.print("min");
lcd.display();
} //end LCDdisplay()
bool postMessageToSlack(String msg)
{
const char *host = "hooks.slack.com";
const char *fingerprint = "3b f8 1c a7 74 a8 6b c5 d8 95 fe e2 2e de 65 a1";
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClientSecure client;
const int httpsPort = 443;
if (!client.connect(host, httpsPort))
{
Serial.println("Connection failed :-(");
return false;
}
// We now create a URI for the request
Serial.print("Posting to URL: ");
Serial.println(slack_hook_url);
String postData = "payload={\"link_names\": 1, \"icon_emoji\": \"" + icon_emoji + "\", \"username\": \"" + slack_username + "\", \"text\": \"" + msg + "\"}";
//String postData="payload={\"link_names\": 1, \"icon_url\": \"" + slack_icon_url + "\", \"username\": \"" + slack_username + "\", \"text\": \"" + msg + "\"}";
// This will send the request to the server
client.print(String("POST ") + slack_hook_url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Connection: close" + "\r\n" +
"Content-Length:" + postData.length() + "\r\n" +
"\r\n" + postData);
Serial.println("Request sent");
String line = client.readStringUntil('\n');
Serial.printf("Response code was: ");
Serial.println(line);
if (line.startsWith("HTTP/1.1 200 OK"))
{
return true;
}
else
{
return false;
}
}
void setup()
{
int error;
Serial.begin(115200);
pinMode(buttonPin, INPUT);
//WiFiManager
//Local initialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "coffeeTime"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("coffeeTime");
while (!NTPch.setSNTPtime())
Serial.print("."); // set internal clock
Serial.println();
Serial.println("Time set");
dateTime = NTPch.getTime(-8.0, 2); // get time from internal clock
NTPch.printDateTime(dateTime);
Serial.println("LCD...");
while (!Serial)
;
Serial.println("checking LCD");
// See http://playground.arduino.cc/Main/I2cScanner
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0)
{
Serial.println(": LCD found.");
}
else
{
Serial.println(": LCD not found.");
} // if
lcd.begin(16, 2); // initialize the lcd
lcd.setBacklight(155);
lcd.home();
displayer.attach(1, LCDdisplay);
} // setup()
void loop()
{
dateTime = NTPch.getTime(-8.0, 2); // get time from internal clock
// first parameter: Time zone; second parameter: 1 for European summer time; 2 for US daylight saving time (not implemented yet)
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
Serial.print("reading:");
Serial.println(reading);
// if the button state has changed:
Serial.print("buttonState:");
Serial.println(buttonState);
if (reading != buttonState)
{
buttonState = reading;
// log time button pushed
if (buttonState == HIGH)
{
madeTime = time(NULL);
madeTimeF = NTPch.getTime(-8.0, 1); // get time from internal clock
postMessageToSlack(slack_message);
Serial.print("button pressed");
Serial.println(madeTime);
}
}
delay(200);
} // loop()