Last active
June 12, 2018 17:11
-
-
Save robertschnuell/f54d32ab3f0e39b4284b26df11ff54a7 to your computer and use it in GitHub Desktop.
ESP32 touch detection - mqtt - deepsleep example
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
/* | |
Example code: Detects a touch input which wakes up the esp32 from deepsleep. | |
Then connects to a WiFi and post the touch event via MQTT. | |
After finishing this the esp32 will go back to deppsleep. | |
This whole process takes around 800ms to finish. | |
author: @robertschnuell | |
https://interaktives.design | |
*/ | |
#include "Arduino.h" | |
////// NETWORK //////// | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <PubSubClient.h> | |
////// NETWORK //////// | |
IPAddress server(192, 168, 0, 2); | |
const char* ssid = "............"; | |
const char* password = ".........."; | |
WiFiClient ethClient; | |
PubSubClient client(ethClient); | |
char message_buff[100]; | |
IPAddress local_IP(192, 168, 0, 105); | |
IPAddress gateway(192, 168, 0, 254); | |
IPAddress subnet(255, 255, 255, 0); | |
#define Threshold 40 | |
touch_pad_t touchPin; | |
void wakeupCheckProcess() { | |
esp_sleep_wakeup_cause_t wakeup_reason; | |
wakeup_reason = esp_sleep_get_wakeup_cause(); | |
if(wakeup_reason == 4) { | |
touch_pad_t pin; | |
touchPin = esp_sleep_get_touchpad_wakeup_status(); | |
if(touchPin == 3) { | |
connect(); | |
checkConnection(); | |
client.publish("topic/touch","touched"); | |
delay(500); | |
} | |
} | |
} | |
void setup(){ | |
Serial.begin(115200); | |
wakeupCheckProcess(); | |
// deepsleep functions | |
touchAttachInterrupt(T3, callback, Threshold); //Interuppt to wake up via touch | |
esp_sleep_enable_touchpad_wakeup(); // define wakeup via touch | |
esp_deep_sleep_start(); // go to sleep | |
} | |
// Placeholders | |
void loop(){ | |
//Limbus | |
} | |
void callback(){ | |
// nice place which will never be used :( | |
} | |
////// HELPER FUNCTIONS //////// | |
void connect() { | |
WiFi.onEvent(WiFiEvent); | |
WiFi.mode(WIFI_STA); | |
WiFi.config(local_IP, gateway, subnet); | |
WiFi.begin(ssid,password,1,NULL,true); //begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); | |
WiFi.config(local_IP, gateway, subnet); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(10); | |
} | |
Serial.println("connected"); | |
client.setServer(server, 1883); //IP of mqtt broker and port - 1883 is default port | |
Serial.println(millis()); // time passed since wakeup | |
} | |
void checkConnection() { | |
if(WiFi.status() != WL_CONNECTED) { // check WiFi | |
connect(); | |
} | |
while (!client.connected()) { // check MQTT | |
if ( client.connect("client name") ) { | |
} | |
String tmp = "Device online "; | |
tmp += millis(); | |
char temp[tmp.length() + 1]; | |
tmp.toCharArray(temp, tmp.length() + 1); | |
client.publish("topic/status", temp); | |
} | |
} | |
void WiFiEvent(WiFiEvent_t event) | |
{ | |
Serial.printf("[WiFi-event] event: %d\n", event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment