Skip to content

Instantly share code, notes, and snippets.

@dupontgu
Created November 14, 2020 00:40
Show Gist options
  • Save dupontgu/8efcca2611103104eda88f33fd578c71 to your computer and use it in GitHub Desktop.
Save dupontgu/8efcca2611103104eda88f33fd578c71 to your computer and use it in GitHub Desktop.
ESP-32 Arduino code to accompany Freezer Door Alarm project: https://hackaday.io/project/175862-quick-n-dirty-freezer-door-alarm
/**
*
* ESP-32 code to accompany freezer door alarm project:
* https://hackaday.io/project/175862-quick-n-dirty-freezer-door-alarm
* by Guy Dupont - @dupontgu (GitHub)
*
* tl;dr
* Periodically check switch. If open for two consecutive checks, connect to Wi-Fi and make web request.
*
**/
#include "WiFi.h"
#include <HTTPClient.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */
#define SWITCH_PIN 13
#define LED_PIN 5
const char *ssid = "your-wifi";
const char *password = "your-password";
// This will persist through sleeps
RTC_DATA_ATTR int lastReading = 0;
void setup()
{
pinMode(SWITCH_PIN, INPUT_PULLUP);
int pinReading = digitalRead(SWITCH_PIN);
if (pinReading == 1)
{
// flash LED as a warning
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, 0);
delay(1000);
digitalWrite(LED_PIN, 1);
}
if (pinReading == 0 || lastReading == 0)
{
lastReading = pinReading;
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(1000);
esp_deep_sleep_start();
}
// only arrive here if current reading is 1, AND last reading was 1
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
HTTPClient http;
String serverPath = "http://maker.ifttt.com/trigger/freezer/with/key/YOUR_KEY_HERE";
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
// if you care, deal with failures here!
http.end();
}
void loop()
{
// If you've gotten here, bad things have already happened. Loop until death, I guess?
delay(30000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment