Neurolingua commited on
Commit
fb71fe0
·
verified ·
1 Parent(s): 285a08f

Update firestore_twilio_listener.py

Browse files
Files changed (1) hide show
  1. firestore_twilio_listener.py +83 -60
firestore_twilio_listener.py CHANGED
@@ -1,60 +1,83 @@
1
- from twilio.rest import Client
2
- import firebase_admin
3
- from firebase_admin import credentials, firestore
4
-
5
- # Twilio Configuration
6
- ACCOUNT_SID = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
7
- AUTH_TOKEN = '6d8b2559ffbbdbc5d06542e545220aaa'
8
- FROM_PHONE = '+12708175529'
9
- TO_PHONE = '+916382792828'
10
-
11
- # Firebase Configuration
12
- cred = credentials.Certificate("E:\static\snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json")
13
- firebase_admin.initialize_app(cred)
14
- db = firestore.client()
15
-
16
- # Function to send SMS using Twilio
17
- def send_msg(field1):
18
- # if field;1
19
- client = Client(ACCOUNT_SID, AUTH_TOKEN)
20
- try:
21
- message = client.messages.create(
22
- from_=FROM_PHONE,
23
- body=f"""E.coli Level: {field1}
24
- Status: safe
25
- Suggestions:
26
- You may heat your water to eradicate the bacteria.""",
27
- to=TO_PHONE
28
- )
29
- print(f"Message sent successfully! SID: {message.sid}")
30
- except Exception as e:
31
- print(f"Failed to send message: {e}")
32
-
33
- # Firestore Listener for new data in the 'thingspeak_data' collection
34
- def on_snapshot(doc_snapshot, changes, read_time):
35
- for change in changes:
36
- if change.type.name == 'ADDED': # Detects new documents added to the collection
37
- new_data = change.document.to_dict()
38
- field1 = new_data.get('field1', 'N/A') # Replace 'field1' with your actual field key
39
- print(f"New data detected: {new_data}")
40
- send_msg(field1) # Trigger the Twilio SMS function
41
-
42
- # Initialize Firestore Listener
43
- def start_firestore_listener():
44
- thingspeak_ref = db.collection('thingspeak_data')
45
- thingspeak_watch = thingspeak_ref.on_snapshot(on_snapshot)
46
- print("Listening for new data in Firestore...")
47
-
48
- if __name__ == "__main__":
49
- try:
50
- # Start the listener
51
- start_firestore_listener()
52
-
53
- # Keep the script running to listen for changes
54
- import time
55
- while True:
56
- time.sleep(10)
57
- except KeyboardInterrupt:
58
- print("\nListener stopped.")
59
- except Exception as e:
60
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from twilio.rest import Client
2
+ import firebase_admin
3
+ from firebase_admin import credentials, firestore
4
+
5
+ # Twilio Configuration
6
+ ACCOUNT_SID = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
7
+ AUTH_TOKEN = '6d8b2559ffbbdbc5d06542e545220aaa'
8
+ FROM_PHONE = '+12708175529'
9
+ TO_PHONE = '+916382792828'
10
+ import numpy as np
11
+ import joblib
12
+
13
+ # Load the saved Gradient Boosting model
14
+ loaded_model = joblib.load('dlmodel.pkl')
15
+
16
+ def predict_conc(pdiff):
17
+ if pdiff>3.19:
18
+ return 0
19
+ pdiff_value = pdiff # Replace 1.5 with your input value for "Average Potential Difference"
20
+ new_data = np.array([[pdiff_value]])
21
+ prediction = loaded_model.predict(new_data)
22
+ concentration = 10**prediction[0] # Reverse log transformation
23
+ return f"{concentration:.4f}"
24
+
25
+
26
+ # Firebase Configuration
27
+ cred = credentials.Certificate("snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json")
28
+ firebase_admin.initialize_app(cred)
29
+ db = firestore.client()
30
+
31
+ # Function to send SMS using Twilio
32
+ def send_msg(field1,suggestion):
33
+ # if field;1
34
+
35
+ client = Client(ACCOUNT_SID, AUTH_TOKEN)
36
+ try:
37
+ message = client.messages.create(
38
+ from_=FROM_PHONE,
39
+ body=f"""Test Results:\nE.coli Level: {field1} CFU/mL.
40
+ Status: safe
41
+ Suggestions:\n
42
+ {suggestion}""",
43
+ to=TO_PHONE
44
+ )
45
+ print(f"Message sent successfully! SID: {message.sid}")
46
+ except Exception as e:
47
+ print(f"Failed to send message: {e}")
48
+
49
+ # Firestore Listener for new data in the 'thingspeak_data' collection
50
+ def on_snapshot(doc_snapshot, changes, read_time):
51
+ for change in changes:
52
+ if change.type.name == 'ADDED': # Detects new documents added to the collection
53
+ new_data = change.document.to_dict()
54
+ field1 = new_data.get('field1', 'N/A') # Replace 'field1' with your actual field key
55
+ print(f"New data detected: {new_data}")
56
+ concentration=predict_conc(field1)
57
+ if concentration==0:
58
+ send_msg(concentration,"The water is safe for use!!")
59
+
60
+ else:
61
+ send_msg(concentration,"You may boil the water to 50°C to eradicate the bacteria.")
62
+
63
+ # Trigger the Twilio SMS function
64
+
65
+ # Initialize Firestore Listener
66
+ def start_firestore_listener():
67
+ thingspeak_ref = db.collection('thingspeak_data')
68
+ thingspeak_watch = thingspeak_ref.on_snapshot(on_snapshot)
69
+ print("Listening for new data in Firestore...")
70
+
71
+ if __name__ == "__main__":
72
+ try:
73
+ # Start the listener
74
+ start_firestore_listener()
75
+
76
+ # Keep the script running to listen for changes
77
+ import time
78
+ while True:
79
+ time.sleep(10)
80
+ except KeyboardInterrupt:
81
+ print("\nListener stopped.")
82
+ except Exception as e:
83
+ print(f"An error occurred: {e}")