Browse code

add sms alert functionality

Gandolf authored on 12/09/2021 02:11:18
Showing 1 changed files
... ...
@@ -49,8 +49,19 @@ import json
49 49
 import ina260 # power sensor
50 50
 import tmp102 # temperature sensor
51 51
 
52
+# Import custom libraries
53
+import smsalert
54
+
52 55
     ### ENVIRONMENT ###
56
+
53 57
 _USER = os.environ['USER']
58
+_HOSTNAME = os.uname()[1]
59
+
60
+    ### SMS RECIPIENTS ###
61
+
62
+_SMS_CALLSIGN = '{your callsign}'
63
+_SMS_PASSCODE = '{your passcode}'
64
+_SMS_PHONE_NUMBER = '{your phone number}'
54 65
 
55 66
     ### SENSOR BUS ADDRESSES ###
56 67
 
... ...
@@ -89,11 +100,16 @@ _CHART_WIDTH = 600
89 100
 _CHART_HEIGHT = 150
90 101
 # chart average line color
91 102
 _AVERAGE_LINE_COLOR = '#006600'
103
+# low voltage alert threshold
104
+_DEFAULT_CRITICAL_LOW_VOLTAGE = 12.0
92 105
 
93 106
    ### GLOBAL VARIABLES ###
94 107
 
95
-# Container for sensor objects.
96
-dSensors = {}
108
+# Sensor instance objects.
109
+power1 = None
110
+battemp = None
111
+ambtemp = None
112
+sms = None
97 113
 
98 114
 # turns on or off extensive debugging messages
99 115
 debugMode = False
... ...
@@ -107,6 +123,8 @@ chartUpdateInterval = _CHART_UPDATE_INTERVAL
107 123
 failedUpdateCount = 0
108 124
 # sensor status
109 125
 deviceOnline = False
126
+# sms message sent status
127
+bSMSmsgSent = False
110 128
 
111 129
   ###  PRIVATE METHODS  ###
112 130
 
... ...
@@ -182,11 +200,11 @@ def getSensorData(dData):
182 200
     dData["time"] = getTimeStamp()
183 201
  
184 202
     try:
185
-        dData["current"] = dSensors['power'].getCurrent()
186
-        dData["voltage"] = dSensors['power'].getVoltage()
187
-        dData["power"] = dSensors['power'].getPower()
188
-        dData["battemp"] = dSensors['battemp'].getTempF()
189
-        dData["ambtemp"] = dSensors['ambtemp'].getTempF()
203
+        dData["current"] = power1.getCurrent()
204
+        dData["voltage"] = power1.getVoltage()
205
+        dData["power"] =   power1.getPower()
206
+        dData["battemp"] = battemp.getTempF()
207
+        dData["ambtemp"] = ambtemp.getTempF()
190 208
     except Exception as exError:
191 209
         print("%s sensor error: %s" % (getTimeStamp(), exError))
192 210
         return False
... ...
@@ -196,6 +214,34 @@ def getSensorData(dData):
196 214
     return True
197 215
 ## end def
198 216
 
217
+def convertData(dData):
218
+    """
219
+    Converts data items and verifies threshold crossings.
220
+    Parameters: dData - a dictionary object that contains the sensor data
221
+    Returns: True if successful, False otherwise
222
+    """
223
+    global bSMSmsgSent
224
+
225
+    if not bSMSmsgSent and dData["voltage"] <= _DEFAULT_CRITICAL_LOW_VOLTAGE:
226
+        # Format a text alert message.
227
+        message = "%s %s low voltage alert: %s volts" % \
228
+                  (getTimeStamp(), _HOSTNAME, dData["voltage"])
229
+        print(message)
230
+        # Send the text alert to recipient phone numbers.
231
+        sms.sendSMS(_SMS_PHONE_NUMBER, message)
232
+        bSMSmsgSent = True
233
+    elif bSMSmsgSent and dData["voltage"] > _DEFAULT_CRITICAL_LOW_VOLTAGE:
234
+        # Format a text alert message.
235
+        message = "%s %s voltage normal: %s volts" % \
236
+                  (getTimeStamp(), _HOSTNAME, dData["voltage"])
237
+        print(message)
238
+        # Send the text alert to recipient phone numbers.
239
+        sms.sendSMS(_SMS_PHONE_NUMBER, message)
240
+        bSMSmsgSent = False