質問編集履歴
2
ソースコードの追加、質問文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,271 @@
|
|
1
|
-
|
1
|
+
サーバーとラズベリーパイはソケット通信で接続されています。データ送信側がサーバー、データ受信側をラズベリーパイとして、サーバー上のプログラムの中でif文でa=1ならラズベリーパイ上にデータ1を出力、a=2のときラズベリーパイ上にデータ2を出力、、といったことを行いたいのですが、
|
2
|
-
また、可能であればpythonによる記述例を教えていただけますでしょうか。
|
3
2
|
|
4
|
-
|
3
|
+
```python
|
4
|
+
# -*- Coding: utf-8 -*-
|
5
|
+
import serial
|
6
|
+
import time
|
7
|
+
import socket
|
8
|
+
import requests
|
9
|
+
import argparse
|
10
|
+
import json
|
11
|
+
import cv2
|
12
|
+
import subprocess
|
13
|
+
|
14
|
+
import pandas as pd
|
15
|
+
import numpy as np
|
16
|
+
from scipy import signal
|
17
|
+
import pywt
|
18
|
+
import threading
|
19
|
+
|
20
|
+
import picamera
|
21
|
+
import picamera.array
|
5
|
-
|
22
|
+
with picamera.PiCamera() as camera:
|
23
|
+
camera.resolution = (400,300)
|
24
|
+
camera.start_preview()
|
6
|
-
|
25
|
+
camera.capture('test.jpg')
|
26
|
+
|
27
|
+
import matplotlib
|
28
|
+
matplotlib.rcParams['backend']='TkAgg'
|
29
|
+
import matplotlib.pyplot as plt
|
30
|
+
from PIL import Image
|
31
|
+
from io import BytesIO
|
32
|
+
import errno
|
33
|
+
|
34
|
+
import collections as cl
|
35
|
+
import datetime
|
36
|
+
from timeout_decorator import timeout, TimeoutError
|
37
|
+
|
38
|
+
subscription_key = "key添付予定"
|
39
|
+
assert subscription_key
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
image_path = "/home/pi/test.jpg"
|
44
|
+
|
45
|
+
parser = argparse.ArgumentParser()
|
46
|
+
parser.add_argument('--ip_addr', help="Put the client's ip address here")
|
47
|
+
args = parser.parse_args()
|
48
|
+
|
49
|
+
|
50
|
+
connorclo=0
|
51
|
+
|
52
|
+
def main():
|
53
|
+
RR=[]
|
54
|
+
sin=[]
|
55
|
+
gpio_seri = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
|
56
|
+
dt=1
|
57
|
+
fs = 1.0/dt
|
58
|
+
|
59
|
+
cnt = 0
|
60
|
+
|
61
|
+
|
62
|
+
senddic={}
|
63
|
+
print("START")
|
64
|
+
t=1
|
65
|
+
senddic={}
|
66
|
+
print("START")
|
67
|
+
t=1
|
68
|
+
gpsla=gpsla1=0.0
|
69
|
+
gpslo=gpslo1=0.0
|
70
|
+
while True:
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
bio_data = gpio_seri.readline().rstrip().decode('unicode-escape')+","+str(gpsla)+","+str(gpsla)
|
75
|
+
|
76
|
+
|
77
|
+
bd=bio_data.split(",")
|
78
|
+
if len(bd)>17:
|
79
|
+
if bd[15] == '':
|
80
|
+
bd[15] = 60
|
81
|
+
elif bd[15].isdecimal() == False:
|
82
|
+
bd[15] = 60
|
83
|
+
else:
|
84
|
+
sin.append(int(bd[15]))
|
85
|
+
if int(bd[15])>0:
|
86
|
+
r = 60/float(bd[15])
|
87
|
+
RR.append(r)
|
88
|
+
|
89
|
+
|
90
|
+
cnt +=1
|
91
|
+
if cnt % 10 ==0:
|
92
|
+
print(cnt)
|
93
|
+
|
94
|
+
|
95
|
+
if(len(RR)>=1024):
|
96
|
+
#mae ha 5
|
97
|
+
dt_now=datetime.datetime.now().strftime('%Y/%m/%d_%H:%M:%S')
|
98
|
+
print(dt_now)
|
99
|
+
print("ond: "+bd[0]+" sitsudo: "+bd[1]+" taikiatsu: "+bd[2]+" kido: "+bd[3]+" dkenti: "+ bd[4])
|
100
|
+
print("kasokux: "+bd[5]+" ky: "+bd[6]+" kz: "+bd[7]+" kkx: "+bd[8]+" kky: "+bd[9]+" kkz: "+bd[10]+" tizix: "+bd[11]+" tiziy: "+bd[12]+" tiziz: "+bd[13])
|
101
|
+
print("taion: "+bd[14]+" sinpaku: "+bd[15]+" GSR: "+bd[16])
|
102
|
+
print("heikinshinpaku", sum(sin)/len(sin),"RRheikin",sum(RR)/len(RR),int(len(RR)))
|
103
|
+
t1 = time.time()
|
104
|
+
s = pd.Series(RR)
|
105
|
+
#freq2, pow_dend=signal.periodogram(s, fs)
|
106
|
+
vlf = 0.04
|
107
|
+
lf = 0.15
|
108
|
+
hf = 0.4
|
109
|
+
Fs = 250
|
110
|
+
dy=1.0/Fs
|
111
|
+
nq_f = fs/2.0
|
112
|
+
|
113
|
+
wavelet_type = 'cmor1.5-1.0'
|
114
|
+
wav = pywt.ContinuousWavelet(wavelet_type)
|
115
|
+
freqs = np.linspace(0.0001,nq_f,1024)
|
116
|
+
freqs_rate = freqs / fs
|
117
|
+
scales = 1 / freqs_rate
|
118
|
+
scales = scales[::-1]
|
119
|
+
frequencies_rate = pywt.scale2frequency(scale=scales, wavelet=wavelet_type)
|
120
|
+
|
121
|
+
frequencies = frequencies_rate / dt
|
122
|
+
|
123
|
+
cwtmatr, freqs_rate = pywt.cwt(s, scales, wavelet_type)
|
124
|
+
pow_den = (np.abs(cwtmatr))
|
125
|
+
|
126
|
+
pow_den = pow_den[200]
|
127
|
+
lf_freq_band = (freqs_rate >= vlf) & (freqs_rate <= lf)
|
128
|
+
hf_freq_band = (freqs_rate >= lf) & (freqs_rate <= hf)
|
129
|
+
LF1 = np.trapz(pow_den[lf_freq_band], x=None, dx=dy)
|
130
|
+
HF1 = np.trapz(pow_den[hf_freq_band], x=None, dx=dy)
|
131
|
+
|
132
|
+
pow_den = (np.abs(cwtmatr))
|
133
|
+
pow_den = pow_den[400]
|
134
|
+
#print(pow_den)
|
135
|
+
lf_freq_band = (freqs_rate >= vlf) & (freqs_rate <= lf)
|
136
|
+
hf_freq_band = (freqs_rate >= lf) & (freqs_rate <= hf)
|
137
|
+
LF2 = np.trapz(pow_den[lf_freq_band], x=None, dx=dy)
|
138
|
+
HF2 = np.trapz(pow_den[hf_freq_band], x=None, dx=dy)
|
139
|
+
|
140
|
+
pow_den = (np.abs(cwtmatr))
|
141
|
+
pow_den = pow_den[600]
|
142
|
+
lf_freq_band = (freqs_rate >= vlf) & (freqs_rate <= lf)
|
143
|
+
hf_freq_band = (freqs_rate >= lf) & (freqs_rate <= hf)
|
144
|
+
LF3 = np.trapz(pow_den[lf_freq_band], x=None, dx=dy)
|
145
|
+
HF3 = np.trapz(pow_den[hf_freq_band], x=None, dx=dy)
|
146
|
+
|
147
|
+
pow_den = (np.abs(cwtmatr))
|
148
|
+
pow_den = pow_den[800]
|
149
|
+
lf_freq_band = (freqs_rate >= vlf) & (freqs_rate <= lf)
|
150
|
+
hf_freq_band = (freqs_rate >= lf) & (freqs_rate <= hf)
|
151
|
+
LF4 = np.trapz(pow_den[lf_freq_band], x=None, dx=dy)
|
152
|
+
HF4 = np.trapz(pow_den[hf_freq_band], x=None, dx=dy)
|
153
|
+
|
154
|
+
pow_den = (np.abs(cwtmatr))
|
155
|
+
pow_den = pow_den[1000]
|
156
|
+
lf_freq_band = (freqs_rate >= vlf) & (freqs_rate <= lf)
|
157
|
+
hf_freq_band = (freqs_rate >= lf) & (freqs_rate <= hf)
|
158
|
+
LF5 = np.trapz(pow_den[lf_freq_band], x=None, dx=dy)
|
159
|
+
HF5 = np.trapz(pow_den[hf_freq_band], x=None, dx=dy)
|
160
|
+
|
161
|
+
LF_HF = (np.float64(LF1+LF2+LF3+LF4+LF5) / (HF1+HF2+HF3+HF4+HF5)) * 5
|
162
|
+
print(LF_HF)
|
163
|
+
t2 = time.time()
|
164
|
+
elapsed_time = t2-t1
|
165
|
+
#print(elapsed_time)
|
166
|
+
|
167
|
+
bio_data=bio_data+","+str(LF_HF)
|
168
|
+
with socket.socket(socket.AF_INET,socket.SOCK_STREAM)as s:
|
169
|
+
data = socket.recv(2)
|
170
|
+
if(data==1):
|
171
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
172
|
+
|
173
|
+
elif(data==2):
|
174
|
+
subprocess.call("aplay 20211110_075639.wav", shell=True)
|
175
|
+
|
176
|
+
elif(data==3):
|
177
|
+
subprocess.call("aplay 20211110_075718.wav", shell=True)
|
178
|
+
|
179
|
+
elif(data==4):
|
180
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
181
|
+
|
182
|
+
elif(data==5):
|
183
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
184
|
+
|
185
|
+
elif(data==6):
|
186
|
+
subprocess.call("aplay 20211110_075805.wav", shell=True)
|
187
|
+
|
188
|
+
else:
|
189
|
+
pass
|
190
|
+
|
191
|
+
with picamera.PiCamera() as camera:
|
192
|
+
camera.resolution = (400,300)
|
193
|
+
camera.capture('test.jpg')
|
194
|
+
image_data = open(image_path, "rb").read()
|
195
|
+
img = cv2.imread(image_path)
|
196
|
+
cv2.waitKey(1000)
|
197
|
+
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
|
198
|
+
'Content-Type': 'application/octet-stream'}
|
199
|
+
params = {'visualFeatures': 'Description'}
|
200
|
+
try:
|
201
|
+
response = requests.post(
|
202
|
+
analyze_url, headers=headers, params=params, data=image_data)
|
203
|
+
except requests.exceptions.ConnectionError:
|
204
|
+
continue
|
205
|
+
#print(datetime.datetime.now())
|
206
|
+
response.raise_for_status()
|
207
|
+
analysis = response.json()['description']['tags']
|
208
|
+
jsonana=json.dumps(analysis).replace("\"","")
|
209
|
+
if connorclo==1:
|
210
|
+
jsonana1=json.dumps(analysis)
|
211
|
+
gpsthread3 = threading.Thread(target=socket_data, args=(bio_data, jsonana1))
|
212
|
+
gpsthread3.daemon = True
|
213
|
+
gpsthread3.start()
|
214
|
+
|
215
|
+
dt_now=datetime.datetime.now().strftime('%Y/%m/%d_%H:%M:%S')
|
216
|
+
senddic["key"+str(t)] = {"nowtime":dt_now}
|
217
|
+
senddic["key"+str(t)].update({"bio_data":bio_data})
|
218
|
+
senddic["key"+str(t)].update({"camera":jsonana})
|
219
|
+
|
220
|
+
t+=1
|
221
|
+
print(connorclo)
|
222
|
+
print("b")
|
223
|
+
if(t==3):
|
224
|
+
json_data = json.dumps(senddic).replace("\\", "").replace("[","").replace("]","").replace(" ","")
|
225
|
+
|
226
|
+
file_path = "./sendata2.json"
|
227
|
+
with open(file_path, 'w') as f:
|
228
|
+
json.dump(json_data, f,ensure_ascii=False)
|
229
|
+
|
230
|
+
t=1
|
231
|
+
print("a")
|
232
|
+
sendic={}
|
233
|
+
print("wait")
|
234
|
+
|
235
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
236
|
+
break
|
237
|
+
cnt=0
|
238
|
+
del RR[:100]
|
239
|
+
cv2.destroyAllWindows()
|
240
|
+
|
241
|
+
if __name__ == '__main__':
|
242
|
+
main()
|
243
|
+
|
244
|
+
```
|
245
|
+
上記のプログラムを実行時に、次の箇所について
|
246
|
+
```python
|
247
|
+
with socket.socket(socket.AF_INET,socket.SOCK_STREAM)as s:
|
248
|
+
data = socket.recv(2)
|
249
|
+
if(data==1):
|
250
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
251
|
+
|
252
|
+
elif(data==2):
|
253
|
+
subprocess.call("aplay 20211110_075639.wav", shell=True)
|
254
|
+
|
255
|
+
elif(data==3):
|
256
|
+
subprocess.call("aplay 20211110_075718.wav", shell=True)
|
257
|
+
|
258
|
+
elif(data==4):
|
259
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
260
|
+
|
261
|
+
elif(data==5):
|
262
|
+
subprocess.call("aplay 20211110_075611.wav", shell=True)
|
263
|
+
|
264
|
+
elif(data==6):
|
265
|
+
subprocess.call("aplay 20211110_075805.wav", shell=True)
|
266
|
+
|
267
|
+
else:
|
268
|
+
pass
|
269
|
+
```
|
270
|
+
AttributeError: type object socket has no attribute 'recv'
|
271
|
+
というエラーが発生したため、どのようにプログラムを修正するべきか教えていただけないでしょうか。
|
1
内容の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,2 +1,6 @@
|
|
1
1
|
pythonを用いたラズベリーパイとサーバー間でのデータのやりとりにおいて、サーバー内にある複数のデータのうち、指定したデータをサーバーからラズベリーパイに送信することができる方法はありますか。
|
2
|
-
また、可能であればpythonによる記述例を教えていただけますでしょうか。
|
2
|
+
また、可能であればpythonによる記述例を教えていただけますでしょうか。
|
3
|
+
|
4
|
+
追記
|
5
|
+
サーバーとラズベリーパイはソケット通信で接続されています。サーバー上のプログラムの中でif文でa=1ならラズベリーパイ上に1という数値を出力、a=2のときラズベリーパイ上に2を出力、a=5のとき5という数値を出力するといったことを行いたいのですが、このときサーバーとラズベリーパイ上のプログラムはどのように記述すればよいか教えていただけますでしょうか。
|
6
|
+
また、データ送信側がサーバー、データ受信側がラズベリーパイとしています。
|