-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzer.ino
95 lines (75 loc) · 2.46 KB
/
buzzer.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
/*
NodeMCU-Sending Slack Message after Button press.
Button press does a reset of the device
After sending the Slack the Arduino goes in deep sleep
*/
#include <ESP8266WiFi.h>
#define LED 2 //GPIO2 Interne Led auf dem NodeMCU Board
#define BUZZER 5 //GPIO5 as buzzer input
#define SSID "SSID"
#define PASSWORD "WIFIpass"
// for using postMessageToSlack()
const String slack_hook_url = "https://hooks.slack.com/services/xxx/xxx/xxxxxx";
const String slack_icon_url = ":heart_eyes_cat:";
const String slack_message = "Die Katzen haben Futter bekommen.";
const String slack_username = "Katzefudderer";
void setup()
{
// Serial.begin(115200);
// Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
// Serial.print("Connecting to wifi...");
while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
delay(250);
}
// Serial.println();
// Serial.println(WiFi.localIP());
pinMode(LED, OUTPUT); // Port aus Ausgang schalten
pinMode(BUZZER, INPUT_PULLUP); // Taster einlesen
digitalWrite(LED, LOW); //Led port einschalten
while (!postMessageToSlack(slack_message))
{}
delay(2000);
digitalWrite(LED, HIGH); //Led port ausschalten
// go to deepsleep
ESP.deepSleep(0);
}
void loop()
{
}
bool postMessageToSlack(String msg)
{
const char* host = "hooks.slack.com";
// 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_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;
}
}