Skip to content

Instantly share code, notes, and snippets.

@howdydoody123
Created February 12, 2015 00:41
Show Gist options
  • Save howdydoody123/234dfab2bfd13487d11b to your computer and use it in GitHub Desktop.
Save howdydoody123/234dfab2bfd13487d11b to your computer and use it in GitHub Desktop.
A sketch for connecting a PIR to wireless and sending an email when motion is detected.
/*
The MIT License (MIT)
Copyright (c) 2015 Sean M Beck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <Adafruit_CC3000_Server.h>
#include <ccspi.h>
#include <Client.h>
#include <Temboo.h>
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "network name" // cannot be longer than 32 characters!
#define WLAN_PASS "network password"
#define WLAN_SECURITY WLAN_SEC_WPA2
#define TEMBOO_ACCOUNT "" // Your Temboo account name
#define TEMBOO_APP_KEY_NAME "" // Your Temboo app key name
#define TEMBOO_APP_KEY "" // Your Temboo app key
int led_pin = 8;
int pir_input = 2;
int pir_state = LOW;
int val = 0;
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT);
Adafruit_CC3000_Client client;
void setup() {
pinMode(led_pin, OUTPUT);
pinMode(pir_input, INPUT);
Serial.begin(115200);
Serial.println(F("Initializing..."));
if (!cc3000.begin()) {
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
init_network();
}
void loop() {
val = digitalRead(pir_input);
if (val == HIGH) {
digitalWrite(led_pin, HIGH);
delay(150);
if (pir_state == LOW) {
Serial.println("Motion detected");
pir_state = HIGH;
send_email();
}
}
else {
digitalWrite(led_pin, LOW);
delay(300);
if (pir_state == HIGH) {
Serial.println("Motion ended");
pir_state = LOW;
}
}
}
void send_email() {
if (!cc3000.checkConnected()) {
Serial.println(F("Not connected. Restarting connection."));
init_network();
}
Serial.println("Running SendEmail");
TembooChoreo SendEmailChoreo(client);
// Invoke the Temboo client
SendEmailChoreo.begin();
// Set Temboo account credentials
SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);
Serial.println("Set Choreo details");
// Set profile to use for execution
SendEmailChoreo.setProfile("Sendemail");
// Identify the Choreo to run
SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");
// Run the Choreo; when results are available, print them to serial
SendEmailChoreo.run();
Serial.println("Choreo finished running\n");
while(SendEmailChoreo.available()) {
char c = SendEmailChoreo.read();
Serial.print(c);
}
SendEmailChoreo.close();
}
void init_network() {
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP()) {
delay(100); // ToDo: Insert a DHCP timeout!
}
while (!displayConnectionDetails()) {
delay(1000);
}
}
bool displayConnectionDetails(void) {
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) {
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else {
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment