Compare commits
7 commits
4-enable-c
...
master
Author | SHA1 | Date | |
---|---|---|---|
f80a2d799e | |||
1d16899296 | |||
15a27bc8ff | |||
4ca6158807 | |||
38360f58e1 | |||
e774835f03 | |||
71b6cc6db9 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.pyc
|
|
@ -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()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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()
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue