Created
January 22, 2022 08:56
-
-
Save samy/485b57436e441c6f0d19ec24fc39d163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AsyncHTTPSRequest_Generic.h> | |
#include <AsyncHTTPSRequest_Impl_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic | |
#include <Ticker.h> | |
//https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce | |
#include <WiFi.h> // Utilisation de la librairie WiFi.h | |
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager | |
#include <HTTPClient.h> | |
bool isButtonPressed = false; | |
const byte BUTTON_PIN = 14; | |
const byte LED_PIN = 12; | |
int ledState = HIGH; // the current state of the output pin | |
int buttonState; // the current reading from the input pin | |
int lastButtonState = LOW; // the previous reading from the input pin | |
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled | |
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers | |
AsyncHTTPSRequest request; | |
Ticker ticker; | |
Ticker ticker1; | |
void setup() { | |
pinMode(BUTTON_PIN, INPUT); | |
pinMode(LED_PIN, OUTPUT); | |
Serial.begin(115200); | |
WiFiManager wifiManager; | |
// WiFiManager | |
wifiManager.autoConnect("Buzzer"); | |
digitalWrite(LED_PIN, ledState); | |
ticker.attach(1, sendRequest); | |
} | |
void loop() { | |
int reading = digitalRead(BUTTON_PIN); | |
if (reading != lastButtonState) { | |
lastDebounceTime = millis(); | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
if (reading != buttonState) { | |
buttonState = reading; | |
ledState = !ledState; | |
if (ledState == LOW) { | |
isButtonPressed = true; | |
} | |
} | |
} | |
digitalWrite(LED_PIN, ledState); | |
lastButtonState = reading; | |
} | |
void sendRequest() | |
{ | |
if (!isButtonPressed) { | |
return; | |
} | |
isButtonPressed = false; | |
static bool requestOpenResult; | |
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) | |
{ | |
requestOpenResult = request.open("GET", "https://webhook.site/"); | |
if (requestOpenResult) | |
{ | |
// Only send() if open() returns true, or crash | |
Serial.println("Request is OK"); | |
request.send(); | |
} | |
else | |
{ | |
Serial.println("Can't send bad request"); | |
} | |
} | |
else | |
{ | |
Serial.println("Can't send request"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment