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] 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()