Created
November 10, 2023 04:21
-
-
Save kakopappa/e9d05233bc97f29a403a101810e36795 to your computer and use it in GitHub Desktop.
Complete tutorial : https://help.sinric.pro/pages/tutorials/custom-device-types/alcohol-sensor/MQ-3.html
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
#ifndef _GASSENSOR_H_ | |
#define _GASSENSOR_H_ | |
#include <SinricProDevice.h> | |
#include <Capabilities/ModeController.h> | |
#include <Capabilities/PushNotification.h> | |
class GasSensor | |
: public SinricProDevice | |
, public ModeController<GasSensor> | |
, public PushNotification<GasSensor> { | |
friend class ModeController<GasSensor>; | |
friend class PushNotification<GasSensor>; | |
public: | |
GasSensor(const String &deviceId) : SinricProDevice(deviceId, "GasSensor") {}; | |
}; | |
#endif |
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
// Uncomment the following line to enable serial debug output | |
//#define ENABLE_DEBUG | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#endif | |
#ifdef ESP32 | |
#include <WiFi.h> | |
#endif | |
#include <SinricPro.h> | |
#include "GasSensor.h" | |
#define WIFI_SSID "" // Your WiFI SSID name | |
#define WIFI_PASS "" // Your WiFi Password. | |
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" (Get it from Portal -> Secrets) | |
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" (Get it from Portal -> Secrets) | |
#define DEVICE_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx" (Get it from Portal -> Devices) | |
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor) | |
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds | |
#if defined(ESP8266) | |
const int adcPin = A0; | |
#elif defined(ESP32) | |
const int adcPin = 34; | |
#elif defined(ARDUINO_ARCH_RP2040) | |
const int adcPin = 26; | |
#endif | |
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor) | |
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds | |
const int GAS_DETECTED_THRESHOLD = 850; // Change accordingly | |
GasSensor &gasSensor = SinricPro[DEVICE_ID]; | |
int sensorVal; | |
String sensorState = ""; | |
String lastSensorState = ""; | |
// ModeController | |
void updateMode(String mode) { | |
gasSensor.sendModeEvent("modeInstance1", mode, "PHYSICAL_INTERACTION"); | |
} | |
// PushNotificationController | |
void sendPushNotification(String notification) { | |
gasSensor.sendPushNotification(notification); | |
} | |
void setupSinricPro() { | |
SinricPro.onConnected([]{ Serial.printf("[SinricPro]: Connected\r\n"); }); | |
SinricPro.onDisconnected([]{ Serial.printf("[SinricPro]: Disconnected\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void handleM3Sensor() { | |
if (SinricPro.isConnected() == false) { | |
return; | |
} | |
static unsigned long last_millis; | |
unsigned long current_millis = millis(); | |
if (last_millis && current_millis - last_millis < EVENT_WAIT_TIME) return; | |
last_millis = current_millis; | |
sensorVal = analogRead(adcPin); | |
Serial.printf("Sensor value: %d\r\n", sensorVal); | |
if (sensorVal > GAS_DETECTED_THRESHOLD) { // "Detected" or "Not-Detected" (from ModeController mode values) | |
sensorState = "Detected"; | |
} else { | |
sensorState = "Not-Detected"; | |
} | |
if(sensorState != lastSensorState) { | |
/* update server with "detected" / "Not-Detected" status */ | |
updateMode(sensorState); | |
/* send a push notification when gas detected */ | |
if(sensorState == "Detected") { | |
sendPushNotification("Gas Detected!!!"); | |
} | |
} | |
lastSensorState = sensorState; // save to compare next time. | |
} | |
void setupWiFi() { | |
#if defined(ESP8266) | |
WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
WiFi.setAutoReconnect(true); | |
#elif defined(ESP32) | |
WiFi.setSleep(false); | |
WiFi.setAutoReconnect(true); | |
#endif | |
WiFi.begin(SSID, PASS); | |
Serial.printf("[WiFi]: Connecting to %s", SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
Serial.printf("connected\r\n"); | |
} | |
void setup() { | |
Serial.begin(BAUD_RATE); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
/******** | |
* Loop * | |
********/ | |
void loop() { | |
handleM3Sensor(); | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment