Created
January 2, 2017 20:50
-
-
Save gbaeke/15596e3e2d185eb11720c965ab33e179 to your computer and use it in GitHub Desktop.
This file contains 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
// This #include statement was automatically added by the Particle IDE. | |
#include "Temboo/Temboo.h" | |
#include "TembooAccount.h" | |
int LED = D1; | |
int PR = A0; // photoresistor port | |
int BUTTON = D0; // button port | |
bool bright; | |
bool pressed; | |
int brightness = 0; // to store the current brightness | |
// we'll need this to contact Temboo | |
TCPClient client; | |
void setup() { | |
pinMode(LED, OUTPUT); | |
pinMode(PR, INPUT); | |
digitalWrite(LED, LOW); | |
//check environment and set bright accordingly | |
if(analogRead(PR)>=2000) bright=true; | |
else bright=false; | |
//set pressed state | |
if(digitalRead(BUTTON)==HIGH) pressed=true; | |
else pressed=false; | |
Particle.function("led", ledToggler); | |
Particle.variable("brightness", &brightness, INT); | |
} | |
void loop() { | |
brightness=analogRead(PR); | |
if(digitalRead(BUTTON)==HIGH && !pressed) { | |
Particle.publish("button pressed", NULL); | |
pressed=true; | |
runSendSMS("Brightness is " + String(brightness)); | |
runSendVoice("You have been sent the following message: brightness is " + String(brightness)); | |
} else if(digitalRead(BUTTON)==LOW && pressed) { | |
pressed=false; | |
} | |
if(brightness >= 2000 && !bright) { | |
Particle.publish("lights on", String(brightness), 60, PRIVATE); | |
bright=true; | |
} | |
else if(brightness < 2000 && bright) { | |
Particle.publish("lights out", String(brightness), 60, PRIVATE); | |
bright=false; | |
} | |
delay(1000); | |
} | |
int ledToggler(String command) { | |
if(command=="on") { | |
digitalWrite(LED, HIGH); | |
return 1; | |
} | |
else if(command=="off") { | |
digitalWrite(LED, LOW); | |
return 0; | |
} | |
else | |
{ | |
return -1; | |
} | |
} | |
void runSendSMS(String body) { | |
TembooChoreo SendSMSChoreo(client); | |
// Set Temboo account credentials | |
SendSMSChoreo.setAccountName(TEMBOO_ACCOUNT); | |
SendSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); | |
SendSMSChoreo.setAppKey(TEMBOO_APP_KEY); | |
// Set Choreo inputs | |
SendSMSChoreo.addInput("AuthToken", "twilio token"); | |
SendSMSChoreo.addInput("From", "from number"); | |
SendSMSChoreo.addInput("To", "to number"); | |
SendSMSChoreo.addInput("Body", body); | |
SendSMSChoreo.addInput("AccountSID", "accountsid"); | |
// Identify the Choreo to run | |
SendSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS"); | |
SendSMSChoreo.run(); | |
SendSMSChoreo.close(); | |
} | |
void runSendVoice(String body) { | |
TembooChoreo TextToSpeechChoreo(client); | |
// Set Temboo account credentials | |
TextToSpeechChoreo.setAccountName(TEMBOO_ACCOUNT); | |
TextToSpeechChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME); | |
TextToSpeechChoreo.setAppKey(TEMBOO_APP_KEY); | |
// Set Choreo inputs | |
TextToSpeechChoreo.addInput("APIKey", "nexmo api key"); | |
TextToSpeechChoreo.addInput("Text", body); | |
TextToSpeechChoreo.addInput("To", "to number"); | |
TextToSpeechChoreo.addInput("APISecret", "nexmo api secret"); | |
// Identify the Choreo to run | |
TextToSpeechChoreo.setChoreo("/Library/Nexmo/Voice/TextToSpeech"); | |
// Run the Choreo; when results are available, print them to serial | |
TextToSpeechChoreo.run(); | |
TextToSpeechChoreo.close(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment