-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_main.py
190 lines (148 loc) · 5.64 KB
/
pi_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from RPLCD.gpio import CharLCD
from RPi import GPIO
import time
#----
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
#----
########-------------------------
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#--------------FILE--------------
fil = open('ou1.txt','w')
#fi.write("basb")
#--------------------------------
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
gps_data = "0,0"
#gps_data = "23.12345,78.12345"
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0)
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
##########-------------------------
lcd = CharLCD(pin_rs=26, pin_e=24, pins_data=[22, 18, 16, 12],
numbering_mode=GPIO.BOARD,
cols=20, rows=4, dotsize=8,
charmap='A02',
auto_linebreaks=True)
#--------button----------
import RPi.GPIO as GPIO
import time
#GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO17
GPIO.setup(13, GPIO.OUT) #LED to GPIO27
#------------------------
####-----------------------SERVER------------------------
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
import socket
import time
SERVER_ADDRESS = (HOST, PORT) = '', 8088
REQUEST_QUEUE_SIZE = 5
####------------------
file_out = open("encrypted_data.bin", "a")
recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = open("aeskey.txt","r").read()
###---------------------
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(SERVER_ADDRESS)
listen_socket.listen(REQUEST_QUEUE_SIZE)
print('Serving HTTP on port {port} ...'.format(port=PORT))
i=0
####---------------------SERVER-END------------------------
#fi.write("honda")
try:
while True:
button_state = GPIO.input(11)
butt_flag = "0"
if button_state == False:
GPIO.output(13, True)
#print('Button Pressed...')
butt_flag = "1"
#time.sleep(0.01)
else:
GPIO.output(13, False)
butt_flag = "0"
temp_val = str(read_temp())
bpm_val = str(ser.readline())
dispText = "temp="+temp_val+"|"+"BPM="+bpm_val+"|"+"pos="+gps_data+"|"+"btn="+butt_flag
print dispText
fil.write(dispText+"\n")
#fi.write("heeeloo")
lcd.clear()
lcd.clear()
lcd.write_string(dispText)
send_text ="time_stamp="+str(time.time())+"|"+"mod_id=sm_0001|"+dispText
###-----------SERVER---------------------
# Enable Serial Communication
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key
###### ENCRYPTING
data = (send_text).encode("utf-8")
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
cipher = ciphertext + "join" + tag + "nonce" + cipher_aes.nonce + "key" + enc_session_key
#cipher = [ x for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
file_out.write(cipher)
port.write('AT'+'\r\n')
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write('ATE0'+'\r\n') # Disable the Echo
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications
#rcv = port.read(10)
#print rcv
#time.sleep(1)
# Sending a message to a particular Number
port.write('AT+CMGS="9953798329"'+'\r\n')
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write(send_text+'\r\n') # Message
#rcv = port.read(10)
#print rcv
port.write("\x1A") # Enable to send SMS
#for i in range(10):
# rcv = port.read(10)
# print rcv
i=i+1
###-----------SERVER--END------------------
time.sleep(5.0000)
except KeyboardInterrupt:
pass
finally:
lcd.clear()
lcd.write_string(u'script stopped')
time.sleep(1)
lcd.clear()
GPIO.cleanup()