Skip to content

Instantly share code, notes, and snippets.

@CraftingGamerTom
Created December 23, 2018 13:16
Show Gist options
  • Save CraftingGamerTom/eace9eccd08da5b6b514a3a3c88e7690 to your computer and use it in GitHub Desktop.
Save CraftingGamerTom/eace9eccd08da5b6b514a3a3c88e7690 to your computer and use it in GitHub Desktop.
Data Collection Script for Aqua IoT - Mongo 1
# Download the helper library from https://www.twilio.com/docs/python/install
import datetime
import time
import dateutil.parser
import pymongo
import os
import glob
import time
from collections import OrderedDict
from twilio.rest import Client
from pymongo import MongoClient
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#SETTINGS
client = MongoClient('localhost', 27017)
lowTempLevel = 70
highTempLevel = 86
inDebugMode = False
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# FUNCTIONS
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
#return temp_c, temp_f
return temp_f # Only return F (for now)
# PROGRAM BELOW
while True:
print (client)
db = client.aqua
# Set Data
timeNow = datetime.datetime.utcnow()
ingestionTime = timeNow.isoformat()
device1Value = round(read_temp(),2)
device1MeasureDate = ingestionTime
newData = {
'id': '0001',
'value': device1Value,
'measureDate': device1MeasureDate,
'ingestionDate': ingestionTime
}
liveResult = db.liveData.find_and_modify({'id': '0001'}, newData)
persistentResult = db.persistentData.insert(newData)
print(liveResult)
print(persistentResult)
# -----
if((device1Value < lowTempLevel or device1Value > highTempLevel) and not inDebugMode):
notifiableTime = timeNow - datetime.timedelta(hours=2)
lastNotifiedDoc = db.notificationTracker.find({"id": "0001"}).limit(1)
lastNotified = dateutil.parser.parse(lastNotifiedDoc[0]['notificationTime'])
print('Temperature Warning! Current Temperature: ' + str(device1Value))
if(notifiableTime > lastNotified):
print('Notifying users!')
# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = Client(account_sid, auth_token)
# numbers_to_message = ['+10000000000X', '+10000000000X', '+10000000000X']
numbers_to_message = ['+10000000000X']
for number in numbers_to_message:
message = client.messages.create(
body = 'Temperature Warning! Current Temperature: ' + str(device1Value),
from_ = '+10000000000X',
to = number
)
print(message.sid)
# Set a value in DB to prevent notification spam
newNotification = {
"id":"0001",
"notificationTime": ingestionTime,
"users": numbers_to_message,
"debugMode": inDebugMode
}
db.notificationTracker.find_and_modify({'id': '0001'}, newNotification)
print("Temperature Warning!")
# loop
time.sleep(900)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment