import machine import time import network import urequests as requests import sys #import uasyncio as asyncio #from microdot_asyncio import Microdot, Request, Response # inputs from Twilio account_sid = 'ACc8decb157966b94d386cce4b33673df3' auth_token = '05b6d470c8bc9b51b7c35ca71fcc221c' recipient = '+14138843113' #'+14138843113' sender = '+18665063709' twiml = str('https://handler.twilio.com/twiml/EHd515b8f41758585a0c182d10678bedd1') button_pin = 1 fly_pin = 3 ssid = 'computer' password = 'slowasfuckboy' threshold = 20 def connect_to_wifi(ssid, password): # Just making our internet connection wlan = network.WLAN(network.STA_IF) wlan.active(True) # Wait for connect or fail max_wait = 10 while max_wait > 0: print(max_wait) try: print("trying") wlan.connect(ssid, password) print("tried") wlan.status() if wlan.status() < 0 or wlan.status() >= 3: print('connected') break else: wlan.status() except: max_wait = max_wait - 1 print("wlan status: ", wlan.status()) print('waiting for connection...') time.sleep(1) if max_wait == 10: sys.exit("Error message") max_wait = connect_to_wifi(ssid, password) if max_wait == 10: sys.exit("Error message") # =============================================================== define board commands ADC_res = 12 sens = 0.0035 conv_factor = 3.3*(pow(2,ADC_res))/sens # gives us Gauss / ADC conv_factor_1 = 3.5 pin_numbers = [button_pin, fly_pin] pin_objs = {x: machine.Pin(x, machine.Pin.IN, machine.Pin.PULL_UP) for x in pin_numbers} led = machine.Pin(9, machine.Pin.OUT) adc = machine.ADC(machine.Pin(fly_pin, machine.Pin.IN)) adc.atten(machine.ADC.ATTN_11DB) def call(): headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Connection':'close'} 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) response.close() def send_sms(message): headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Connection':'close'} 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) response.close() tare = 2130 #fly = adc.read() # take one reading to use as tare (switched to flat, because we need it to have the reading down!) while True: time.sleep(1) button = pin_objs[button_pin].value() fly = adc.read() gauss_fly = (fly - tare) * conv_factor_1 print(gauss_fly) print(button) # 1 == open, 0 == closed if (gauss_fly > threshold and gauss_fly < -threshold) and button == 1: print("intentionally undone: button undone, fly undone") elif (gauss_fly > threshold or gauss_fly < -threshold) and button == 1: print("not worried: button undone, fly closed") elif (gauss_fly > threshold or gauss_fly < -threshold) and button == 0: print("all closed up: button done, fly closed") elif (gauss_fly < threshold or gauss_fly < -threshold) and button == 0: print("warning fly down!: button done, fly undone") # wait 10 seconds time.sleep(3) button = pin_objs[button_pin].value() fly = adc.read() gauss_fly = (fly - tare)*conv_factor_1 # test again while (gauss_fly > threshold or gauss_fly < -threshold) and button == 0: button = pin_objs[button_pin].value() fly = adc.read() gauss_fly = (fly - tare)*conv_factor_1 print("attempting call") try: print("attempting call") print(gauss_fly) False time.sleep(3) call() except: True else: False time.sleep(0.01)