Skip to content

Instantly share code, notes, and snippets.

@jaykru
Last active June 2, 2016 21:47
Show Gist options
  • Save jaykru/ef17e21c5fe5c846614c319caf452cc2 to your computer and use it in GitHub Desktop.
Save jaykru/ef17e21c5fe5c846614c319caf452cc2 to your computer and use it in GitHub Desktop.
#include <Adafruit_CC3000.h>
#include <Adafruit_CC3000_Server.h>
#include <ccspi.h>
#include <string.h>
#include <SPI.h> // spi bus support
#include <OneWire.h> // general serial communications library
#include <DallasTemperature.h> // for temperature sensor
// Constants: IFTTT key/triggers for temperature reporting and alarm
#define IFTTT_KEY "b9_YqwnM9KIUiQG-dPXQMGgvcfc1vAAJTd4Kiz3j4uo"
#define TEMP_TRIGGER "temp_report"
#define ALARM_TRIGGER "temp_beyond_threshold"
#define ONE_WIRE_BUS 9 // pin for one-wire
#define BEEG "maker.ifttt.com" // host to send http requests to
#define ADAFRUIT_CC3000_IRQ 3 // irq pin for cc3000
// vbat and cs pins for cc3000
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// credentials for testing purposes
#define WLAN_SSID "A. Cool iPhone"
#define WLAN_PASS "silenceplease"
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // initializes the cc3000 device
Adafruit_CC3000_Client client; // wifi client instance.
// timeout values to determine when to lcose the connections
int dhcpTimeout = 60000; // 60000ms = 6 seconds
int connectTimeout = 15000; // 15000ms = 15 seconds
int responseTimeout = 15000; // 15000ms = 15 seconds
int currentTemperature;
int sheetCounter = 1;
int getCounter = 0;
uint32_t ip;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// just a function prototype
void http_request();
void setup(void) {
Serial.begin(115200); // 115200 baud serial connection for fast text printing
Serial.println(F("KASICH 2016"));
Serial.print(F("Checking wiring..."));
if(!cc3000.begin()) {
failHang();
}
Serial.print(F("OK\r\nDeleting old connection profiles..."));
if(!cc3000.deleteProfiles()){
failHang();
}
Serial.println(F("OK\r\nConnecting to Access Point..."));
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SEC_WPA2)) {
failHang();
}
Serial.print(F("OK\r\nRequesting DHCP..."));
for(int t=millis(); !cc3000.checkDHCP() && ((millis() - t) < dhcpTimeout); delay(100))
{
Serial.println("Attempting");
}
if(!cc3000.checkDHCP()) {
failHang();
}
Serial.print(F("OK\r\nAttempting to resolve maker.ifttt.com..."));
ip = 0;
// find out maker.ifttt.com's ip address
while (ip == 0) {
if(!cc3000.getHostByName(BEEG, &ip)) {
Serial.println(F("Coudln't resolve maker.ifttt.com!"));
}
delay(500);
}
Serial.println(F("Resolved maker.ifttt.com"));
}
void loop() {
delay(1800000); //delay 1800000s == 30 minutes between GET requests.
// create a new sheet weekly
if (getCounter > 0 && (getCounter % 336 == 0)) {
sheetCounter += 1;
}
Serial.print(F("Requesting temperatures......"));
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(F("DONE"));
currentTemperature = sensors.getTempCByIndex(0); // poll sensor serial for temperature value in celsius
Serial.print(F("About to record temperature value: ")); Serial.println(currentTemperature);
http_request(IFTTT_KEY, TEMP_TRIGGER); // send the http request
delay(1000);
if (currentTemperature < 13 || currentTemperature > 26) {
http_request(IFTTT_KEY, ALARM_TRIGGER); // sends IFTTT request to send Dr. Savage a push notification if temperature gets too extreme
}
}
void failHang() {
Serial.print(F("FAILURE"));
while(1);
}
void http_request(char* key, char* trigger) {
Adafruit_CC3000_Client www;
char get_url[500];
sprintf(get_url, "/trigger/%s/with/key/%s?value1=%d&value2=%d", trigger, key, currentTemperature, sheetCounter); // copies url to the get_url
Serial.println("Opening TCP connection with ten second timeout.");
int t = millis();
do {
cc3000.connectTCP(ip, 80);
}
while ((!www.connected()) && ((millis() - t) < 10000)); // 10s timeout
// just sends the request with HTTP protocol
if (www.connected()) {
www.fastrprint(F("GET "));
www.fastrprint(get_url);
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(BEEG); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
Serial.println(F("Sent GET request."));
}
else {
Serial.println(F("Connection failed"));
return;
}
// closes the TCP connection to maker.ifttt.com and stops the device
Serial.println(F("\n\nClosing client stream"));
www.close();
www.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment