개발자/Raspberry Pi3

건설 현장 안전 시스템 IoT 장비 - 여기서 일부 매듭

지구빵집 2019. 10. 3. 21:21
반응형

 

힘들게 일한다. 어려운 일도 아닌데, 시간만 보내고. 앞으로 그런일 없도록 하자.

 

소스코드와 사진

 

서버 Test 코드 실행은 $sudo python server.py

 

#-*- coding:utf-8 -*-
from socket import *

serverSock = socket(AF_INET, SOCK_STREAM)
serverSock.bind(('', 8080))
serverSock.listen(1)

connectionSock, addr = serverSock.accept()

print(str(addr), '에서 접속이 확인되었습니다.')

while True:
    data = connectionSock.recv(1024)
    print('받은 데이터 : ', data.decode('utf-8'))

#connectionSock.send('I am a server.'.encode('utf-8'))
#print('메시지를 보냈습니다.')

 

클라이언트 테스트코드 실행은 $sudo python testblescan.py

 

#-*- coding: utf-8 -*-
# test BLE Scanning software
# girin 9/30/2019

import RPi.GPIO as GPIO
import time

from socket import *

import blescan
import sys
import bluetooth._bluetooth as bluez

clientSock = socket(AF_INET, SOCK_STREAM)
clientSock.connect(('127.0.0.1', 8080))
print('연결 확인 됐습니다.')

dev_id = 1 #0
#pi@raspberrypi:~ $ hcitool dev
#Devices:
#        hci1    B8:27:EB:3F:20:99
#        hci0    00:15:83:6B:F6:6D


try:
	sock = bluez.hci_open_dev(dev_id)
	print ("ble thread started")

except:
	print ("error accessing bluetooth device...")
	sys.exit(1)

blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)

CollectedBeaconAddress = []
FindBeacon = []
StoredBeacon = []
NumofFindBeacon = len(FindBeacon)
NumofStoredBeacon = len(StoredBeacon)
print(NumofFindBeacon)
print(NumofStoredBeacon)

dub_list = []

before_Pir = 0;
before_Motion = 0

while True:
	returnedList = blescan.parse_events(sock, 20) #10, 100, 20

	#여기서 맥주소만 뽑아서 맥주소를 상대로
	del dub_list[:]
	for beacon in returnedList:
		origin_macaddress = beacon.split(',')[0]
		split_macaddress = origin_macaddress.split(':')
		mac_address= split_macaddress[0]+split_macaddress[1]+split_macaddress[2]+split_macaddress[3]+split_macaddress[4]+split_macaddress[5]
		dub_list.append(mac_address)
	#수집된거중 중복제거
	del CollectedBeaconAddress[:]
	CollectedBeaconAddress = list(set(dub_list))

	print (CollectedBeaconAddress)
	#print (StoredBeacon)

	print ("----------")
	for beacon in CollectedBeaconAddress:
		#print (beacon)
		#print(beacon.split(',')[0])
		#origin_macaddress = beacon.split(',')[0]
		#split_macaddress = origin_macaddress.split(':')
		#mac_address= split_macaddress[0]+split_macaddress[1]+split_macaddress[2]+split_macaddress[3]+split_macaddress[4]+split_macaddress[5]
		#리스트만들어 두고 추가 삭제를 자유롭게 보내는 것도 자유롭게
		#1.맥어드레스를 저장된 어드레스와 비교해서 저장하고 있다면 통과/2.없으면 집어넣고 발견됐다고 메세지 송신/리스트에 있는데 없으면 없다고 메시지 만들어 송신
		if beacon in StoredBeacon:
			#이미 있으므로 패스
			#print(StoredBeacon)
			pass
		else:
			#없으니 저장하고 전송하기
			print(beacon)
			StoredBeacon.append(beacon)
			clientSock.send('E5E5E5E5E5E5E5E5'+'0001'+'01'+ beacon + 'ffff')
			print('in 메시지를 전송했습니다.')

	#print (CollectedBeaconAddress)

	for outbeacon in StoredBeacon:
		if outbeacon in CollectedBeaconAddress:
			pass
		else:
			print(outbeacon)
			StoredBeacon.remove(outbeacon)
			clientSock.send('E5E5E5E5E5E5E5E5'+'0001'+'00'+ outbeacon + 'ffff')
			print('out 메시지를 전송했습니다.')

	#센서데이터 전송

 

작업 사진은 아직 완료 되지 않았다. 아흑!

 

 

 

 

 

반응형