From 71b6cc6db9c9fe371ea43aa969ee74d8a1083170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Thu, 8 Mar 2018 21:04:43 +0100 Subject: [PATCH 1/5] add add_option function --- esp8266/uConfigParser.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/esp8266/uConfigParser.py b/esp8266/uConfigParser.py index e44a337..23ad026 100644 --- a/esp8266/uConfigParser.py +++ b/esp8266/uConfigParser.py @@ -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: From 38360f58e1eec150b6b7b16f504d24f56b1a9a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Thu, 8 Mar 2018 23:36:46 +0100 Subject: [PATCH 2/5] add gitignore, update config.ini and read config.ini in boot.py --- .gitignore | 1 + esp8266/boot.py | 25 +++++++++++++++++++------ esp8266/config.ini | 7 +++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .gitignore 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..779a593 100644 --- a/esp8266/config.ini +++ b/esp8266/config.ini @@ -0,0 +1,7 @@ +[DEFAULT] + +ssid = MIPS + +key = importthis + +webrepl_passwd = totocaca From 15a27bc8ff093ddda6deb9673780676189685d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Fri, 9 Mar 2018 00:09:59 +0100 Subject: [PATCH 3/5] enable method in get/post data --- esp8266/config.ini | 15 ++++++++++++--- esp8266/main.py | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/esp8266/config.ini b/esp8266/config.ini index 779a593..ff30337 100644 --- a/esp8266/config.ini +++ b/esp8266/config.ini @@ -1,7 +1,16 @@ [DEFAULT] -ssid = MIPS +ssid = HODOR -key = importthis +key = holdthedoor -webrepl_passwd = totocaca +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..4b1cf88 100644 --- a/esp8266/main.py +++ b/esp8266/main.py @@ -1,13 +1,31 @@ from machine import Pin -from urequests import get +from urequests import get, post 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) if not 'url' in option or not "method" in option} + if method == "POST": + post(url, **data) + elif method == "GET": + get(url, **data) pin_value = current_value collect() From 1d168992960cbcc64f12a97f42e0127df3a6a1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Fri, 9 Mar 2018 22:45:50 +0100 Subject: [PATCH 4/5] try/except when post --- esp8266/main.py | 24 ++++++++++++++++++------ esp8266/uConfigParser.py | 4 ++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/esp8266/main.py b/esp8266/main.py index 4b1cf88..e0aa7d6 100644 --- a/esp8266/main.py +++ b/esp8266/main.py @@ -1,5 +1,5 @@ from machine import Pin -from urequests import get, post +import urequests as requests from time import sleep_ms from gc import collect from uConfigParser import ConfigParser @@ -18,14 +18,26 @@ while True: 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) if not 'url' in option or not "method" in option} - if method == "POST": - post(url, **data) - elif method == "GET": - get(url, **data) + data = {option:config[section][option] for option in config_obj.options(section)} + del data['url'] + del data['method'] + try: + if method == "GET": + print('GET') + r = requests.get(url, data=data) + elif method == "POST": + print('POST') + r = requests.post(url, data=data) + except: + pass + else: + print("close") + r.close() + print("closed") pin_value = current_value collect() diff --git a/esp8266/uConfigParser.py b/esp8266/uConfigParser.py index 23ad026..c2cc80a 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.""" From f80a2d799ec6829c78f6ef69e5ae0d0f63bb0931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Mon, 12 Mar 2018 00:02:05 +0100 Subject: [PATCH 5/5] fix parser error --- esp8266/main.py | 23 ++++++++++++++++------- esp8266/uConfigParser.py | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/esp8266/main.py b/esp8266/main.py index e0aa7d6..eeb35a5 100644 --- a/esp8266/main.py +++ b/esp8266/main.py @@ -26,18 +26,27 @@ while True: 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') - r = requests.get(url, data=data) + if data : + r = requests.get(url, data=data) + else: + r = requests.get(url) elif method == "POST": print('POST') - r = requests.post(url, data=data) + if data: + r = requests.post(url, data=data) + else: + r = requests.post(url) except: - pass - else: - print("close") - r.close() - print("closed") + 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 c2cc80a..9f3e889 100644 --- a/esp8266/uConfigParser.py +++ b/esp8266/uConfigParser.py @@ -63,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: