The code below is for connecting your sensor to IFTTT.
// IFTTT SMS Version of the Code
// All of the following includes are for WIFI
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
// MQTT IOT Includes
#include <IFTTTWebhook.h>
const char* ifttt_key = "7hWCwDwmUGOrCCGdM_VGh";
const char* ifttt_event_name = "MailboxDelivered";
WiFiClientSecure client;
const char* host = "maker.ifttt.com";
// current fingerprint can be found on a *nix system with the following command
// echo | openssl s_client -connect maker.ifttt.com:443 |& openssl x509 -fingerprint -noout
const char fingerprint[] PROGMEM = "AA 75 CB 41 2E D5 F9 97 FF 5D A0 8B 7D AC 12 21 08 4B 00 8C";
const String url = String("/trigger/") + ifttt_event_name + "/with/key/" + ifttt_key;
const int httpsPort = 443;
// enable reading battery voltage
ADC_MODE(ADC_VCC);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Starts the serial communication
// Connect to WiFi
Serial.print("\n\nConnecting Wifi... ");
WiFiManager wifiManager;
// uncomment the next line to clear all saved settings
//wifiManager.resetSettings();
wifiManager.autoConnect("MailboxSensorSetup");
// Connected
Serial.print("My Wifi IP is: ");
Serial.println(WiFi.localIP());
// main code
// trigger webhook
Serial.println("connecting to IFTTT");
Serial.print("connecting to ");
Serial.println(host);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
// for a trustfire 14500 I want to send myself a low battery warning @ just under 3.5 volts.
Serial.print("Read VCC: ");
Serial.println(ESP.getVcc());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266Mailbox\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
Serial.println("Triggered event, going to sleep");
//deep sleep until reset
ESP.deepSleep(0);
}
void loop() {
}
// MQTT/Home Assistant Version of the Code
// All of the following includes are for WIFI
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
// MQTT IOT Includes
#include <PubSubClient.h>
// MQTT config
#define MQTT_SERV "YOUR MQTT IP ADDRESS"
#define MQTT_PORT 1883
#define MQTT_NAME "YOUR MQTT USER"
#define MQTT_PASS "YOUR MQTT PASS"
#define MQTT_VOLT_TOPIC "stat/mailbox/voltage"
#define MQTT_OPENED_TOPIC "stat/mailbox/opened"
WiFiClient client;
PubSubClient pubclient(MQTT_SERV, MQTT_PORT, client);
// enable reading battery voltage
ADC_MODE(ADC_VCC);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Starts the serial communication
// Connect to WiFi
Serial.print("\n\nConnecting Wifi... ");
WiFiManager wifiManager;
// uncomment the next line to clear all saved settings
//wifiManager.resetSettings();
wifiManager.autoConnect("MailboxSensorSetup");
// Connected
Serial.print("My Wifi IP is: ");
Serial.println(WiFi.localIP());
// main code
// for a trustfire 14500 I want to send myself a low battery warning @ just under 3.5 volts.
Serial.print("Read VCC: ");
Serial.println(ESP.getVcc());
MQTT_connect();
//because I don't understand C++ varables!
String strVoltage = String(ESP.getVcc());
char voltage[5];
strVoltage.toCharArray(voltage, 5);
pubclient.publish(MQTT_VOLT_TOPIC, voltage);
pubclient.publish(MQTT_OPENED_TOPIC, "OPENED");
//the publish commands actually run async, so let's give them a few seconds to resolve before going to sleep.
delay(10000);
Serial.println("Triggered event, going to sleep");
//deep sleep until reset
ESP.deepSleep(0);
}
void loop() {
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void MQTT_connect()
{
// Stop if already connected.
if (pubclient.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
Serial.print("Connecting to ");
Serial.print(MQTT_SERV);
Serial.print(" as ");
Serial.println(clientName);
if (pubclient.connect((char*) clientName.c_str(), MQTT_NAME, MQTT_PASS)) {
Serial.println("Connected to MQTT broker");
}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
Serial.println("MQTT Connected!");
}