From a1b1d83e29cebc958729022d4c67ed2af4ed92d1 Mon Sep 17 00:00:00 2001 From: Tilman Date: Wed, 26 Sep 2018 20:32:24 +0000 Subject: [PATCH] Add new file --- Doorbell-Sender.ino | 75 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Doorbell-Sender.ino diff --git a/Doorbell-Sender.ino b/Doorbell-Sender.ino new file mode 100644 index 0000000..6b8650f --- /dev/null +++ b/Doorbell-Sender.ino @@ -0,0 +1,75 @@ +/* + * Sources: + * https://www.arduino.cc/en/Tutorial/Button + * https://techtutorialsx.com/2017/05/19/esp32-http-get-requests/ + */ + +#include +#include + +const char* ssid = "LeWe"; +const char* password = "5275274365464843"; +const char* bellUrl = "http://192.168.1.117/bell/on"; + +const int buttonPin = 21; // the number of the pushbutton pin +const int ledPin = 23; // the number of the LED pin +int buttonState = 0; + + +void setup() { + + Serial.begin(115200); + btStop(); // turn off bluetooth + + pinMode(ledPin, OUTPUT); + pinMode(buttonPin, INPUT); + + + Serial.print("Connecting to "); + Serial.println(ssid); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println("WiFi connected."); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + +} + +void loop() { + + if ((WiFi.status() == WL_CONNECTED)) { + + buttonState = digitalRead(buttonPin); + Serial.println(buttonState); + delay(100); + + if (buttonState == HIGH) { + digitalWrite(ledPin, HIGH); + + HTTPClient http; + + http.begin(bellUrl); + int httpCode = http.GET(); + + if (httpCode > 0) { + String payload = http.getString(); + Serial.println(httpCode); + Serial.println(payload); + } + else { + Serial.println("Error on HTTP request"); + } + + http.end(); + delay(1000); + } + else { + digitalWrite(ledPin, LOW); + } + } + +} \ No newline at end of file