Skip to content

Instantly share code, notes, and snippets.

@maditnerd
Created August 19, 2019 17:36
Show Gist options
  • Save maditnerd/241dc235a0418ae308ddde3420a76023 to your computer and use it in GitHub Desktop.
Save maditnerd/241dc235a0418ae308ddde3420a76023 to your computer and use it in GitHub Desktop.
#include <M5StickC.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiMulti.h>
#include <PubSubClient.h>
#include <time.h>
#define BUTTON_PIN_BITMASK 0x4000000 // 2^26 in hex
RTC_DATA_ATTR int bootCount = 0;
#define HOME_SSID ""
#define HOME_PASS ""
#define LAB_SSID ""
#define LAB_PASS ""
#define MQTT_SERVER ""
#define MQTT_PORT 8883
#define MQTT_USERNAME ""
#define MQTT_PASSWORD ""
const char* root_ca = \
"-----BEGIN CERTIFICATE-----\n" \
"-----END CERTIFICATE-----";
WiFiMulti network;
WiFiClientSecure wifiClient;
PubSubClient mqttClient(wifiClient);
esp_sleep_wakeup_cause_t wakeup_reason;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
/*
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
*/
}
void setup() {
M5.Axp.ScreenBreath(0);
pinMode(M5_LED, OUTPUT);
digitalWrite(M5_LED, LOW);
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
startWiFi();
startMQTT();
updateMQTT();
/*
First we configure the wake up source
We set our ESP32 to wake up for an external trigger.
There are two types for ESP32, ext0 and ext1 .
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
to be on while ext1 uses RTC Controller so doesnt need
peripherals to be powered on.
Note that using internal pullups/pulldowns also requires
RTC peripherals to be turned on.
*/
esp_sleep_enable_timer_wakeup(3600000000);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_26, 1); //1 = High, 0 = Low
//If you were to use ext1, you would use it like
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
//Go to sleep now
Serial.println("Going to sleep now");
digitalWrite(M5_LED, HIGH);
esp_deep_sleep_start();
}
void loop() {
//This is not going to be called
}
void startWiFi() {
WiFi.mode(WIFI_STA);
network.addAP(HOME_SSID, HOME_PASS);
network.addAP(LAB_SSID, LAB_PASS);
while (network.run() != WL_CONNECTED) {
delay(100);
Serial.println("Connecting");
}
Serial.println("Connected");
}
void startMQTT() {
Serial.println("Starting MQTT");
wifiClient.setCACert(root_ca);
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
}
void updateMQTT() {
if (!mqttClient.connected()) {
Serial.println("Not Connected to MQTT");
while (!mqttClient.connected()) {
randomSeed(micros());
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
Serial.println("Trying to connect to MQTT");
if (mqttClient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD, "pir/", 0, 1, "0")) {
Serial.println("... [MQTT] Connected");
wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER){
mqttClient.publish("pir/", "2");
} else {
mqttClient.publish("pir/", "1");
}
} else {
Serial.println("... [MQTT] Connection...");
delay(1000);
}
}
} else {
mqttClient.loop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment