Skip to content

Instantly share code, notes, and snippets.

@a-h
Created August 23, 2018 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-h/a714c7ace353c227ff308dac6dc3488a to your computer and use it in GitHub Desktop.
Save a-h/a714c7ace353c227ff308dac6dc3488a to your computer and use it in GitHub Desktop.
#include <WiFiServerSecure.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
#include <ESP8266WiFiType.h>
#include <ESP8266WiFiAP.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiSTA.h>
#include "RestClient.h"
#include "Debounce.h"
// Uses:
// * https://github.com/wkoch/Debounce
// * https://github.com/esp8266/Arduino/tree/master/doc/esp8266wifi
// * https://github.com/csquared/arduino-restclient
// To install libraries:
// * cd ~/Documents/Arduino/libraries
// * git clone <repo>
// * (For https://github.com/csquared/arduino-restclient, git clone https://github.com/csquared/arduino-restclient RestClient)
const char *ssid = "";
const char *password = "";
RestClient client = RestClient("xxxxx.execute-api.eu-west-1.amazonaws.com", 443, 1);
const char *eventDoorOpened = "{ \"event\": \"door_opened\" }";
const char *eventDoorClosed = "{ \"event\": \"door_closed\" }";
const int inPin = D1;
void setup()
{
pinMode(inPin, INPUT);
Serial.begin(115200);
// Using
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print("Connecting..");
}
Serial.println("Connected...");
}
Debounce doorSwitch(inPin); // the number of the input pin, D1 is set for the ESP8266.
int previousState = -1;
void loop()
{
int newState = doorSwitch.read();
if (previousState == -1) {
// On the first iteration, just set it to the latest value and don't trigger an event.
// This makes the device succeptible to a power outage making it not work.
previousState = newState;
}
if (newState != previousState) {
if (newState == HIGH) {
Serial.println("Door was closed");
postSecure(eventDoorClosed);
} else {
Serial.println("Door was opened");
postSecure(eventDoorOpened);
}
}
previousState = newState;
}
void postSecure(const char *event)
{
String response = "";
Serial.println("Posting...");
int statusCode = client.post("/dev/sensor/detect", event, &response);
Serial.println("Results (status / response)...");
Serial.println(statusCode);
Serial.println(response);
}
'use strict';
var AWS = require('aws-sdk');
module.exports.detect = detect;
const response = {
statusCode: 200,
body: JSON.stringify({ ok: true }),
};
function detect(event, context, callback) {
console.log(event.body);
var e = JSON.parse(event.body);
var from = process.env.FROM_EMAIL_ADDRESS;
var to = process.env.TO_EMAIL_ADDRESS;
console.log({ "msg": "sending email", from, to });
sendEmail(from, to, "Door", e.event)
.then(function (data) {
console.log({ emailsSent: 1 });
})
.catch(function (err) {
console.log({ msg: "sending email failed", err });
});
callback(null, response);
}
function sendEmail(from, to, subject, body) {
AWS.config.update({ region: 'eu-west-1' });
var params = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Charset: "UTF-8",
Data: body
}
},
Subject: {
Charset: 'UTF-8',
Data: subject
}
},
Source: from,
};
var ses = new AWS.SES({ apiVersion: '2010-12-01' });
return ses.sendEmail(params).promise();
}
service: alarm-web
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-1
environment:
FROM_EMAIL_ADDRESS: ${file(variables.yml):environment.FROM_EMAIL_ADDRESS}
TO_EMAIL_ADDRESS: ${file(variables.yml):environment.TO_EMAIL_ADDRESS}
iamRoleStatements:
- Effect: "Allow"
Action:
- "ses:SendEmail"
- "ses:SendRawEmail"
Resource: "*"
functions:
detect:
handler: handler.detect
events:
- http:
path: sensor/detect
method: post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment