Compare commits

..

7 commits

Author SHA1 Message Date
Michaël Ricart f80a2d799e fix parser error 2018-03-12 00:02:05 +01:00
Michaël Ricart 1d16899296 try/except when post 2018-03-09 22:45:50 +01:00
Michaël Ricart 15a27bc8ff enable method in get/post data 2018-03-09 00:09:59 +01:00
Michaël Ricart 4ca6158807 Merge branch 'master' of https://git.0w.tf/Milka64/uHodor 2018-03-08 23:39:52 +01:00
Michaël Ricart 38360f58e1 add gitignore, update config.ini and read config.ini in boot.py 2018-03-08 23:36:46 +01:00
Milka64 e774835f03 Merge branch '3-add-add_option-function' into 'master'
Resolve "add add_option function"

Closes #3

See merge request Milka64/uHodor!2
2018-03-08 20:15:06 +00:00
Michaël Ricart 71b6cc6db9 add add_option function 2018-03-08 21:04:43 +01:00
5 changed files with 87 additions and 11 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.pyc

View file

@ -1,19 +1,32 @@
def do_connect(): import uConfigParser
import gc
import webrepl
def do_connect(ssid, key):
import network import network
import gc
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
wlan.active(True) wlan.active(True)
if not wlan.isconnected(): if not wlan.isconnected():
print('connecting to network...') print('connecting to network...')
wlan.connect("MIPS", "importthis") wlan.connect(ssid, key)
while not wlan.isconnected(): while not wlan.isconnected():
pass pass
print('network config:', wlan.ifconfig()) print('network config:', wlan.ifconfig())
gc.collect()
do_connect()
import gc config_obj = uConfigParser.ConfigParser()
import webrepl config_obj.read('config.ini')
webrepl.start() 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() gc.collect()

View file

@ -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

View file

@ -1,13 +1,52 @@
from machine import Pin from machine import Pin
from urequests import get import urequests as requests
from time import sleep_ms from time import sleep_ms
from gc import collect 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 = Pin(2, Pin.IN)
pin_value = pin.value() pin_value = pin.value()
while True: while True:
current_value = pin.value() current_value = pin.value()
if pin_value != current_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 pin_value = current_value
collect() collect()

View file

@ -4,8 +4,8 @@ class ConfigParser:
def sections(self): def sections(self):
"""Return a list of section names, excluding [DEFAULT]""" """Return a list of section names, excluding [DEFAULT]"""
dict_to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"] to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"]
return dict_to_return return to_return
def add_section(self, section): def add_section(self, section):
"""Create a new section in the configuration.""" """Create a new section in the configuration."""
@ -18,6 +18,13 @@ class ConfigParser:
else: else:
return False 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): def options(self, section):
"""Return a list of option names for the given section name.""" """Return a list of option names for the given section name."""
if not section in self.config_dict: if not section in self.config_dict:
@ -56,7 +63,7 @@ class ConfigParser:
end_index = None end_index = None
else: else:
end_index = block.index(end_flag[0]) 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: if not values:
values = None values = None
elif len(values) == 1: elif len(values) == 1: