import machine import time import network import urequests as requests # inputs from Twilio account_sid = 'XX' #twilio provided auth_token = 'XX' #twilio provided recipient_num = 'XX' sender_num = 'XX' #designated number from twilio twiml = str('https://handler.twilio.com/twiml/EHd515b8f41758585a0c182d10678bedd1') ssid = 'XX' #wifi name password = 'XX' #wifi pass # Just making our internet connection wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) # Wait for connect or fail max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print('waiting for connection...') time.sleep(1) # Handle connection error if wlan.status() != 3: raise RuntimeError('network connection failed') else: print('connected') status = wlan.ifconfig() # =============================================================== define board commands pin_numbers = [2, 3] pin_objs = {x: machine.Pin(x, machine.Pin.IN, machine.Pin.PULL_UP) for x in pin_numbers} led = machine.Pin("LED", machine.Pin.OUT) def call_number(recipient, sender, twiml, auth_token, account_sid): headers = {'Content-Type': 'application/x-www-form-urlencoded'} print("Attempting to Call User") data = "To={}&From={}&Url={}".format(recipient,sender, twiml) url = "https://api.twilio.com/2010-04-01/Accounts/{}/Calls.json".format(account_sid) response = requests.post(url, data=data, auth=(account_sid,auth_token), headers=headers) print(response) def send_sms(recipient, sender, message, auth_token, account_sid): headers = {'Content-Type': 'application/x-www-form-urlencoded'} data = "To={}&From={}&Body={}".format(recipient,sender,message) url = "https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json".format(account_sid) response = requests.post(url, data=data, auth=(account_sid,auth_token), headers=headers) print(response) while True: fly = pin_objs[2].value() button = pin_objs[3].value() # 1 == open, 0 == closed if fly == 1 and button == 1: print("everything unconnected") led.value(0) elif fly == 0 and button == 1: print("likely intentionally down") led.value(0) elif fly == 1 and button == 0: print("likely unintentionally down") time.sleep(5) fly = pin_objs[2].value() button = pin_objs[3].value() if fly == 1 and button == 0: led.value(1) message = "your fly is down!" send_sms(recipient_num, sender_num, message, auth_token, account_sid) call_number(recipient_num, sender_num, twiml, auth_token, account_sid) else: print("all closed up!") print("") time.sleep(0.1)