Partial implementation of settings & sensors
This commit is contained in:
10
main.py
10
main.py
@ -1,10 +1,12 @@
|
||||
import machine
|
||||
import time
|
||||
|
||||
if machine.reset_cause() != machine.DEEPSLEEP_RESET:
|
||||
print("Reset detected, waiting 10 seconds to give you time to interrupt...")
|
||||
time.sleep(10)
|
||||
|
||||
import urequests
|
||||
|
||||
adc = machine.ADC(machine.Pin(32))
|
||||
adc.atten(machine.ADC.ATTN_11DB)
|
||||
|
||||
while (1):
|
||||
n = int(adc.read()/64)
|
||||
print("|" + "#"*n + " "*(64-n) + "|", end="\r")
|
||||
time.sleep_ms(100)
|
||||
53
sensor.py
Normal file
53
sensor.py
Normal file
@ -0,0 +1,53 @@
|
||||
from micropython import const
|
||||
import machine
|
||||
|
||||
SENSOR_ADC = const(0)
|
||||
SENSOR_I2C = const(1)
|
||||
SENSOR_SPI = const(2)
|
||||
|
||||
|
||||
class Sensor:
|
||||
def __init__(name, type, **kwargs):
|
||||
self.name = name
|
||||
self.type = type
|
||||
|
||||
# Different treatment depending on sensortype
|
||||
if type == SENSOR_ADC:
|
||||
#ADC is fairly simple
|
||||
#Only a ADC-Pin is required
|
||||
if "pin" not in kwargs.keys():
|
||||
raise ValueError("Sensortype requires a pin (ADC), but no 'pin'-argument was passed")
|
||||
self.pin = kwargs["pin"]
|
||||
|
||||
self.adc = machine.ADC(machine.Pin(self.pin))
|
||||
|
||||
# Optionally a attenuation parameter may be supplied
|
||||
# otherwise the maximum attenuation (i.e. maximum range) is used
|
||||
if "attenuation" in kwargs.keys():
|
||||
adc.atten(kwargs["attenuation"])
|
||||
else:
|
||||
adc.atten(machine.ADC.ATTN_11DB)
|
||||
|
||||
|
||||
if type == SENSOR_I2C:
|
||||
if "sda" not in kwargs.keys():
|
||||
raise ValueError("Sensortype requires pins (I2C), but no 'sda'-argument was passed")
|
||||
if "scl" not in kwargs.keys():
|
||||
raise ValueError("Sensortype requires pins (I2C), but no 'scl'-argument was passed")
|
||||
if "handler" not in kwargs.keys():
|
||||
raise ValueError("Sensortype requires handler-method (I2C), but no 'handler'-argument was passed")
|
||||
self.sda = kwargs["sda"]
|
||||
self.scl = kwargs["scl"]
|
||||
self.scl = kwargs["handler"]
|
||||
|
||||
|
||||
# SPI isn't used yet
|
||||
if type == SENSOR_SPI:
|
||||
raise NotImplementedError("SPI is not yet implemented")
|
||||
|
||||
|
||||
|
||||
# Read the sensor and return data ready for appending to the POST-request to influxdb
|
||||
def read():
|
||||
if type == SENSOR_ADC:
|
||||
return self.name + "=" + str(self.adc.read())
|
||||
11
settings.py
Normal file
11
settings.py
Normal file
@ -0,0 +1,11 @@
|
||||
node_name = "node_mpt_01"
|
||||
|
||||
wifi_ssid = "espnet"
|
||||
wifi_psk = "esp32net"
|
||||
|
||||
influx_token = "5TQvTkulmC6C_EIBE5objHDPROqdUjhCQbqA9pDL2V-D-MDMq8HXmC4azbapB-8O_8stypSGUCdmqpITHaosow=="
|
||||
influx_bucket = "sensornodes"
|
||||
influx_org = "none"
|
||||
influx_url = "https://influx.krumel.moe/api/v2/write?org={org}&bucket={bucket}&precision=s".format(org=influx_org, bucket=influx_bucket)
|
||||
influx_header = "Authoirzation: Token {}".format(influx_token)
|
||||
|
||||
125
urequests.py
Normal file
125
urequests.py
Normal file
@ -0,0 +1,125 @@
|
||||
#from: https://raw.githubusercontent.com/micropython/micropython-lib/master/urequests/urequests.py
|
||||
import usocket
|
||||
|
||||
class Response:
|
||||
|
||||
def __init__(self, f):
|
||||
self.raw = f
|
||||
self.encoding = "utf-8"
|
||||
self._cached = None
|
||||
|
||||
def close(self):
|
||||
if self.raw:
|
||||
self.raw.close()
|
||||
self.raw = None
|
||||
self._cached = None
|
||||
|
||||
@property
|
||||
def content(self):
|
||||
if self._cached is None:
|
||||
try:
|
||||
self._cached = self.raw.read()
|
||||
finally:
|
||||
self.raw.close()
|
||||
self.raw = None
|
||||
return self._cached
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return str(self.content, self.encoding)
|
||||
|
||||
def json(self):
|
||||
import ujson
|
||||
return ujson.loads(self.content)
|
||||
|
||||
|
||||
def request(method, url, data=None, json=None, headers={}, stream=None):
|
||||
try:
|
||||
proto, dummy, host, path = url.split("/", 3)
|
||||
except ValueError:
|
||||
proto, dummy, host = url.split("/", 2)
|
||||
path = ""
|
||||
if proto == "http:":
|
||||
port = 80
|
||||
elif proto == "https:":
|
||||
import ussl
|
||||
port = 443
|
||||
else:
|
||||
raise ValueError("Unsupported protocol: " + proto)
|
||||
|
||||
if ":" in host:
|
||||
host, port = host.split(":", 1)
|
||||
port = int(port)
|
||||
|
||||
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
|
||||
ai = ai[0]
|
||||
|
||||
s = usocket.socket(ai[0], ai[1], ai[2])
|
||||
try:
|
||||
s.connect(ai[-1])
|
||||
if proto == "https:":
|
||||
s = ussl.wrap_socket(s, server_hostname=host)
|
||||
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
|
||||
if not "Host" in headers:
|
||||
s.write(b"Host: %s\r\n" % host)
|
||||
# Iterate over keys to avoid tuple alloc
|
||||
for k in headers:
|
||||
s.write(k)
|
||||
s.write(b": ")
|
||||
s.write(headers[k])
|
||||
s.write(b"\r\n")
|
||||
if json is not None:
|
||||
assert data is None
|
||||
import ujson
|
||||
data = ujson.dumps(json)
|
||||
s.write(b"Content-Type: application/json\r\n")
|
||||
if data:
|
||||
s.write(b"Content-Length: %d\r\n" % len(data))
|
||||
s.write(b"\r\n")
|
||||
if data:
|
||||
s.write(data)
|
||||
|
||||
l = s.readline()
|
||||
#print(l)
|
||||
l = l.split(None, 2)
|
||||
status = int(l[1])
|
||||
reason = ""
|
||||
if len(l) > 2:
|
||||
reason = l[2].rstrip()
|
||||
while True:
|
||||
l = s.readline()
|
||||
if not l or l == b"\r\n":
|
||||
break
|
||||
#print(l)
|
||||
if l.startswith(b"Transfer-Encoding:"):
|
||||
if b"chunked" in l:
|
||||
raise ValueError("Unsupported " + l)
|
||||
elif l.startswith(b"Location:") and not 200 <= status <= 299:
|
||||
raise NotImplementedError("Redirects not yet supported")
|
||||
except OSError:
|
||||
s.close()
|
||||
raise
|
||||
|
||||
resp = Response(s)
|
||||
resp.status_code = status
|
||||
resp.reason = reason
|
||||
return resp
|
||||
|
||||
|
||||
def head(url, **kw):
|
||||
return request("HEAD", url, **kw)
|
||||
|
||||
def get(url, **kw):
|
||||
return request("GET", url, **kw)
|
||||
|
||||
def post(url, **kw):
|
||||
return request("POST", url, **kw)
|
||||
|
||||
def put(url, **kw):
|
||||
return request("PUT", url, **kw)
|
||||
|
||||
def patch(url, **kw):
|
||||
return request("PATCH", url, **kw)
|
||||
|
||||
def delete(url, **kw):
|
||||
return request("DELETE", url, **kw)
|
||||
Reference in New Issue
Block a user