homecontrol-dht: baby steps
This commit is contained in:
commit
8c14db6565
6 changed files with 120 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.idea
|
||||
Pipfile.lock
|
||||
/credentials.py
|
14
Pipfile
Normal file
14
Pipfile
Normal file
|
@ -0,0 +1,14 @@
|
|||
[[source]]
|
||||
name = "pypi"
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[packages]
|
||||
pyserial = "<4.0,>=3.3"
|
||||
docopt = ">=0.6.2,<0.7"
|
||||
adafruit-ampy = "<1.1,>=1.0.5"
|
||||
|
||||
[requires]
|
||||
python_version = "3.7"
|
1
boot.py
Normal file
1
boot.py
Normal file
|
@ -0,0 +1 @@
|
|||
# not doing anything
|
52
main.py
Normal file
52
main.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import dht
|
||||
import machine
|
||||
import utime as time
|
||||
import urequests as requests
|
||||
import uuid
|
||||
import ujson
|
||||
import wifi
|
||||
|
||||
CONFIG = "config.json"
|
||||
|
||||
class Node:
|
||||
def __init__(self):
|
||||
self.config = {}
|
||||
try:
|
||||
config_file = open(CONFIG, "r")
|
||||
self.config = ujson.load(config_file)
|
||||
except Exception as ex:
|
||||
print("Exception\n\ttype: %s\n\targs: %s" % (type(ex).__name__, ex.args))
|
||||
|
||||
if not self.config.get("nodeid"):
|
||||
self.config["nodeid"] = uuid.uuid4().hex
|
||||
with open(CONFIG, "w") as config_file:
|
||||
ujson.dump(self.config, config_file)
|
||||
|
||||
self.d = dht.DHT22(machine.Pin(2))
|
||||
|
||||
def update(self):
|
||||
self.d.measure()
|
||||
t = float(self.d.temperature())
|
||||
h = float(self.d.humidity())
|
||||
print("T: %f, H: %f" % (t, h))
|
||||
self.send_value("temperature", t)
|
||||
self.send_value("humidity", h)
|
||||
|
||||
def send_value(self, vtype, value):
|
||||
url = "http://192.168.11.21:5000/node/add_value"
|
||||
data = {"nodeid": self.config["nodeid"], "type": vtype, "value": value}
|
||||
try:
|
||||
r = requests.post(url, json=data)
|
||||
# remember to close
|
||||
r.close()
|
||||
except Exception as ex:
|
||||
print("Exception\n\tdata: %s\n\ttype: %s\n\targs: %s" % (data, type(ex).__name__, ex.args))
|
||||
|
||||
if __name__ == "__main__":
|
||||
w = wifi.wifi()
|
||||
w.connect()
|
||||
node = Node()
|
||||
|
||||
while True:
|
||||
node.update()
|
||||
time.sleep(5)
|
28
uuid.py
Normal file
28
uuid.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import uos
|
||||
import ubinascii
|
||||
|
||||
|
||||
class UUID:
|
||||
def __init__(self, bytes):
|
||||
if len(bytes) != 16:
|
||||
raise ValueError('bytes arg must be 16 bytes long')
|
||||
self._bytes = bytes
|
||||
|
||||
@property
|
||||
def hex(self):
|
||||
return ubinascii.hexlify(self._bytes).decode()
|
||||
|
||||
def __str__(self):
|
||||
h = self.hex
|
||||
return '-'.join((h[0:8], h[8:12], h[12:16], h[16:20], h[20:32]))
|
||||
|
||||
def __repr__(self):
|
||||
return "<UUID: %s>" % str(self)
|
||||
|
||||
|
||||
def uuid4():
|
||||
"""Generates a random UUID compliant to RFC 4122 pg.14"""
|
||||
random = bytearray(uos.urandom(16))
|
||||
random[6] = (random[6] & 0x0F) | 0x40
|
||||
random[8] = (random[8] & 0x3F) | 0x80
|
||||
return UUID(bytes=random)
|
22
wifi.py
Normal file
22
wifi.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
import network
|
||||
from credentials import *
|
||||
# NET_SSID="xxx"
|
||||
# NET_PASS="yyy"
|
||||
|
||||
class wifi:
|
||||
def __init__(self):
|
||||
self.if_ap = network.WLAN(network.AP_IF)
|
||||
self.if_sta = network.WLAN(network.STA_IF)
|
||||
|
||||
def status(self):
|
||||
return self.if_sta.isconnected()
|
||||
|
||||
def connect(self):
|
||||
self.if_ap.active(False)
|
||||
self.if_sta.active(True)
|
||||
if not self.if_sta.isconnected():
|
||||
print("connect: reconnecting...")
|
||||
self.if_sta.connect(NET_SSID, NET_PASS)
|
||||
while not self.if_sta.isconnected():
|
||||
pass
|
||||
print("network config: ", self.if_sta.ifconfig())
|
Loading…
Reference in a new issue