Commit cc128c39 authored by chrissi^'s avatar chrissi^
Browse files

Added more documentation


Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent 86c4b9ea
......@@ -2,19 +2,31 @@ import network
import time
class Connector(object):
"""
Connects your device to a given WiFi and keeps track of the connection.
"""
def __init__(self, ssid, passphrase):
"""
Create a new WiFi-Connector.
If your WiFi is not encrypted leave the passphrase field empty.
@param ssid (string): SSID to connect to
@param passphrase (string): PSK of the WiFi
"""
def __init__(self, ssid, passphrase, ap_mode=False):
self._ssid = ssid
self._passphrase = passphrase
self._ap_mode = ap_mode
def do_connect(self):
"""
Keep track of the actual connection.
Call this function regularly, eg. in your main-loop.
"""
ap = network.WLAN(network.AP_IF) # create access-point interface
if self._ap_mode:
ap.active(True) # activate the interface
ap.config(essid='ID-IoT') # set the ESSID of the access point
else:
ap.active(False)
ap.active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
......
"""
This is a map of the labels written on the (cloned) NodeMCU-Devices to
the pin-numbers needed by machine.Pin().
"""
G0 = 0
TxD = 1
......
......@@ -2,11 +2,14 @@ import socket
import mac
def http_get(url):
"""Query an URL and return the result.
"""
Query an URL and return the result.
@param url: http://-url to query
@returns: (Status, Received Return)
"""
# DNS lookup
try:
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
......@@ -14,12 +17,18 @@ def http_get(url):
print("DNS lookup for {} failed.".format(host))
return (False, e)
# setup socket.
# TODO: It seems if this setup fails our micropython
# ran out of descriptors. In this case we should
# make sure to issue a machine.reset()
try:
s = socket.socket()
except Exception as e:
print("Socket setup failed")
return (False, e)
# Get Data via HTTP
try:
s.connect(addr)
print(('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
......@@ -42,7 +51,18 @@ def http_get(url):
return (False, e)
class HttpPut(object):
"""
HTTP-Uploader
"""
def __init__(self, url_format, secret):
"""
Creates a new HttpPut Uploader.
@param url_format (string): Formatstring of the URL. Will be used with
string.format() to add sensorName, value and mac
@param secret (string): Secret used to create the MAC
"""
self._url_format = url_format
self._secret = secret
......
......@@ -5,6 +5,9 @@ import scheduler
import http
import machine
# This is an example main-function for a measurement via a single
# DHT11-sensor connected to a pin.
# If you add more drivers you can - of couse - add other sensors.
s = scheduler.Scheduler()
......@@ -21,4 +24,5 @@ m1 = dht11.DhtMeasure("description-hum", "description-temp", machine.Pin(esp32pi
s.register(m1.measure, 30)
# start scheduler
# runs forever
s.run()
......@@ -9,14 +9,20 @@ class Scheduler:
"""
def __init__(self, raise_mode=False):
"""Setup a new scheduler"""
"""
Setup a new scheduler
@oaram raise_mode (Bool): Indicates if an exception raised in a tasks
should be re-raises or just printed out.
"""
self._callbacks = []
self._counter = []
self._idleCallbacks = []
self._raise_mode = raise_mode
def register(self, callback, timeout):
"""Register a new task.
"""
Register a new task.
@param callback: function to call.
@param timeout: timeout in seconds or None for an idle task
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment