diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/esp8266/boot.py b/esp8266/boot.py index abe5771..f5a6c1a 100644 --- a/esp8266/boot.py +++ b/esp8266/boot.py @@ -1,19 +1,32 @@ -def do_connect(): +import uConfigParser +import gc +import webrepl + +def do_connect(ssid, key): import network + import gc wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') - wlan.connect("MIPS", "importthis") + wlan.connect(ssid, key) while not wlan.isconnected(): pass print('network config:', wlan.ifconfig()) + gc.collect() -do_connect() -import gc -import webrepl -webrepl.start() +config_obj = uConfigParser.ConfigParser() +config_obj.read('config.ini') +config = config_obj.config_dict +if config_obj.has_option('DEFAULT','ssid') and config_obj.has_option('DEFAULT','key'): + do_connect(config['DEFAULT']['ssid'], config['DEFAULT']['key']) +else: + do_connect() +if config_obj.has_option('DEFAULT','webrepl_passwd'): + webrepl.start(password=config['DEFAULT']['webrepl_passwd']) +else: + webrepl.start() gc.collect() diff --git a/esp8266/config.ini b/esp8266/config.ini index e69de29..ff30337 100644 --- a/esp8266/config.ini +++ b/esp8266/config.ini @@ -0,0 +1,16 @@ +[DEFAULT] + +ssid = HODOR + +key = holdthedoor + +webrepl_passwd = HODOR + +[DOOR_WS] + +url = http://hold.the.door/hodor + +method = POST + +param1 = HODOR +param2 = HoldTheDoor diff --git a/esp8266/main.py b/esp8266/main.py index aefe102..eeb35a5 100644 --- a/esp8266/main.py +++ b/esp8266/main.py @@ -1,13 +1,52 @@ from machine import Pin -from urequests import get +import urequests as requests from time import sleep_ms from gc import collect +from uConfigParser import ConfigParser + +config_obj = ConfigParser() +config_obj.read('config.ini') +config = config_obj.config_dict pin = Pin(2, Pin.IN) pin_value = pin.value() while True: current_value = pin.value() if pin_value != current_value: - get('http://192.168.44.174:6543/value/%s' % current_value) + for section in config_obj.sections(): + if config_obj.has_option(section, 'url'): + try: + url = config[section]['url'] + method = config[section]['method'] + + except: + print("error when read section %s in config file" % section) + else: + continue + data = {option:config[section][option] for option in config_obj.options(section)} + del data['url'] + del data['method'] + print(data) + try: + if method == "GET": + print('GET') + if data : + r = requests.get(url, data=data) + else: + r = requests.get(url) + elif method == "POST": + print('POST') + if data: + r = requests.post(url, data=data) + else: + r = requests.post(url) + except: + print(url) + requests.get(url) + print("UNKNOWN ERROR") + continue + print("close") + r.close() + print("closed") pin_value = current_value collect() diff --git a/esp8266/uConfigParser.py b/esp8266/uConfigParser.py index e44a337..9f3e889 100644 --- a/esp8266/uConfigParser.py +++ b/esp8266/uConfigParser.py @@ -4,8 +4,8 @@ class ConfigParser: def sections(self): """Return a list of section names, excluding [DEFAULT]""" - dict_to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"] - return dict_to_return + to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"] + return to_return def add_section(self, section): """Create a new section in the configuration.""" @@ -18,6 +18,13 @@ class ConfigParser: else: return False + def add_option(self, section, option): + """Create a new option in the configuration.""" + if self.has_section(section) and not option in self.config_dict[section]: + self.config_dict[section][option] = None + else: + raise + def options(self, section): """Return a list of option names for the given section name.""" if not section in self.config_dict: @@ -56,7 +63,7 @@ class ConfigParser: end_index = None else: end_index = block.index(end_flag[0]) - values = [value.split('=')[-1].strip() for value in block[start_index:end_index] if value] + values = [value.split('=',1)[-1].strip() for value in block[start_index:end_index] if value] if not values: values = None elif len(values) == 1: