Skip to content

Instantly share code, notes, and snippets.

@dhood
Created August 26, 2017 18:33
Show Gist options
  • Save dhood/b8741ac4f17c0f94a763669cc254aea3 to your computer and use it in GitHub Desktop.
Save dhood/b8741ac4f17c0f94a763669cc254aea3 to your computer and use it in GitHub Desktop.
push_ifttt_event
import requests
import time
import serial
special_link = "https://maker.ifttt.com/trigger/kevin/with/key/o2jagJdkxIUnPkL4mosDT"
time_of_last_event = 0;
def triggerEvent(switchID):
print('calling trigger event')
# How long has it been since the switch was last pressed?
global time_of_last_event
time_now = time.time()
time_since_last_event = time_now - time_of_last_event
# Only do something if it enough time has passed that we know it's not noise
if time_since_last_event > 0.5:
print('pressed!')
payload = {}
payload["value1"] = str(switchID)
# Tell IFTTT about the event
requests.post(special_link, data=payload)
time_of_last_event = time_now
else:
pass
def main():
ser = serial.Serial('/dev/cu.usbmodem1411', 9600) # Establish the connection on a specific port
time.sleep(3)
pressed = False
while True: # Loop forever
time.sleep(.1) # Delay for one tenth of a second so we are not talking all the time
ser.write(b'request') # Send message to arduino to request values
arduino_says = ser.readline().strip() # Read the newest output from the Arduino
print(arduino_says)
if(arduino_says == b"on"):
# Only run triggerEvent if this is the first time it's been pressed
if pressed is False:
triggerEvent(1)
else:
pass
pressed = True
elif(arduino_says == b"off"):
pressed = False
else:
print('received something else')
if __name__ == '__main__':
main()
/*
Arduino sketch to send switch values over serial when requested.
*/
// ------ Configuration settings
int inputPin = 2; // input pin used for the switch
// ------
int ledPin = 13; // use the onboard LED pin for debugging purposes
int inByte = 0; // incoming serial byte
// This function is run when the arduino turns on (only once)
void setup()
{
pinMode(inputPin, INPUT); // initialise input pin mode
Serial.begin(9600); // initialise the serial connection at 9600 baud
pinMode(ledPin, OUTPUT); // set the LED's pin as an 'output' pin
while (!Serial) {
; // wait for the connection to initalise
}
}
// This function is run continuously
void loop()
{
int buttonState = 0;
// only send results when requested
if (Serial.available() > 0) { // listen for a command over serial
// get incoming byte:
inByte = Serial.read();
buttonState = digitalRead(inputPin);
int switchValue = digitalRead(inputPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("on");
}
else {
Serial.println("off");
digitalWrite(ledPin, LOW);
}
// toggle onboard LED to show something received
int oldLedValue = digitalRead(ledPin);
int newLedValue = !oldLedValue;
//digitalWrite(ledPin, newLedValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment