Last active
March 4, 2017 12:34
-
-
Save mattintosh4/55c06c6b50e1289716b1d87bf6448c86 to your computer and use it in GitHub Desktop.
sensors-py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
import Adafruit_DHT as DHT | |
import sqlite3 | |
import sys | |
import time | |
from datetime import datetime | |
DB_FILE = "/nfs/DHT11.sqlite3" | |
TABLE = "DHT11" | |
GPIO_PIN = 4 | |
now = datetime.now().strftime("%F %T") | |
for i in range(11): | |
humidity, temperature = DHT.read_retry(DHT.DHT11, GPIO_PIN) | |
if (20 <= humidity <= 90) and (0 <= temperature <= 50): | |
break | |
elif i == 10: | |
sys.exit(1) | |
sql = """INSERT INTO {table} VALUES("{date}",{temp},{humidity})""".format( | |
table = TABLE | |
, date = now | |
, temp = temperature | |
, humidity = humidity | |
) | |
con = sqlite3.connect(DB_FILE) | |
with con: | |
cur = con.cursor() | |
cur.execute("PRAGMA journal_mode = persist") | |
cur.execute("PRAGMA synchronous = off") | |
print sql | |
cur.execute(sql) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
import spidev | |
import sqlite3 | |
import time | |
from datetime import datetime | |
DB_FILE = "/nfs/temp.sqlite3" | |
def MCP3208(ch): | |
res = spi.xfer2([0x06 | ch >> 2, ch << 6, 0]) | |
val = (res[1] << 8 | res[2]) & 0xfff | |
return val | |
def convertVolt(data): | |
vol = 3.3 / 0xfff * data | |
return vol | |
def LM61(data, amp=1): | |
tmp = convertVolt(data) * 10 ** 3 / 10 / amp - 60 | |
return tmp | |
def LM35(data, amp=1): | |
tmp = convertVolt(data) * 10 ** 3 / 10 / amp | |
return tmp | |
def NJL7302L(data, amp=1): | |
lux = convertVolt(data) / 10 ** 3 * 10 ** 6 / 2 / amp | |
return lux | |
now = datetime.now().strftime("%F %T") | |
spi = spidev.SpiDev() | |
spi.open(0, 0) | |
ch0_data = MCP3208(0) | |
ch1_data = MCP3208(1) | |
ch2_data = MCP3208(2) | |
ch3_data = MCP3208(3) | |
ch4_data = MCP3208(4) | |
con = sqlite3.connect(DB_FILE) | |
with con: | |
cur = con.cursor() | |
cur.execute("PRAGMA journal_mode = persist") | |
cur.execute("PRAGMA synchronous = off") | |
sql = """\ | |
INSERT INTO LM61CIZx2 VALUES("{now:s}",{},{},{}); | |
INSERT INTO LM35DZ VALUES("{now:s}",{},{},{}); | |
INSERT INTO LM61BIZ VALUES("{now:s}",{},{},{}); | |
INSERT INTO LM61CIZ VALUES("{now:s}",{},{},{}); | |
INSERT INTO NJL7302L VALUES("{now:s}",{},{},{}); | |
""".format( | |
ch0_data, convertVolt(ch0_data), LM61(ch0_data, 2) | |
, ch1_data, convertVolt(ch1_data), LM35(ch1_data, 3) | |
, ch2_data, convertVolt(ch2_data), LM61(ch2_data) | |
, ch3_data, convertVolt(ch3_data), LM61(ch3_data) | |
, ch4_data, convertVolt(ch4_data), NJL7302L(ch4_data, 3) | |
, now = now | |
) | |
print sql | |
cur.executescript(sql) | |
spi.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import psutil | |
import sqlite3 | |
import time | |
import threading | |
from collections import deque | |
DB_FILE = "/srv/nfs4/ubuntu.sqlite3" | |
TABLE = "SYSMON" | |
def netIO(rate, interface="eth0"): | |
d = deque(maxlen=2) | |
d.append((time.time(),) + psutil.net_io_counters(pernic=True)[interface][0:2]) | |
while True: | |
time.sleep(1) | |
d.append((time.time(),) + psutil.net_io_counters(pernic=True)[interface][0:2]) | |
d_time, d_sent, d_recv = [y - x for x, y in zip(*d)] | |
rate.append([int(d_sent / d_time), int(d_recv / d_time)]) | |
con = sqlite3.connect(DB_FILE) | |
#cur = con.cursor() | |
#cur.execute("PRAGMA journal_mode = persist") | |
#cur.execute("PRAGMA synchronous = off") | |
transfer_rate = deque(maxlen=1) | |
t = threading.Thread(target=netIO, args=(transfer_rate,)) | |
t.daemon = True | |
t.start() | |
time.sleep(2) | |
con.execute("PRAGMA journal_mode = persist") | |
con.execute("PRAGMA synchronous = off") | |
con.execute("""\ | |
CREATE TABLE IF NOT EXISTS SYSMON( | |
TIME INTEGER NOT NULL UNIQUE | |
, CPU_PERCENT REAL | |
, VM_PERCENT REAL | |
, RX_BYTES INTEGER | |
, TX_BYTES INTEGER | |
, LOGIN_USERS INTEGER | |
, DISK_USAGE_ROOT REAL | |
) | |
""") | |
try: | |
while True: | |
query = """INSERT INTO {} VALUES({},{},{},{},{},{},{})""".format( | |
TABLE | |
, int(time.strftime("%s")) | |
, psutil.cpu_percent() | |
, psutil.virtual_memory().percent | |
, transfer_rate[0][0] | |
, transfer_rate[0][1] | |
, len(psutil.users()) | |
, psutil.disk_usage("/").percent | |
) | |
print query | |
con.execute(query) | |
con.commit() | |
time.sleep(1) | |
except KeyboardInterrupt: | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment