2017-03-05 7 views
0

그래서 저는 Python 스크립팅에 비교적 익숙하며, 라즈베리 파이와 스마트 장치 사이에서 블루투스를 통해 무선 랜을 구성해야합니다. 불행히도, 나는 제목에 나열된 오류로 계속 실행. 나는 누군가가 코드를 복사하고 실행할 수 있기를 바랬다. 왜 나는이 오류가 계속 발생 하는지를 깨닫는다. 모든 도움은 대단히 감사합니다!RPi 3 Python Script - NameError : name 'BluetoothSocket'이 정의되지 않았습니다.

#!/usr/bin/env python 

import os 

from bluetooth import * 

from wifi import Cell, Scheme 

import subprocess 

import time 




wpa_supplicant_conf = "/etc/wpa_supplicant/wpa_supplicant.conf" 

sudo_mode = "sudo " 


def wifi_connect(ssid, psk): 

    # write wifi config to file 
    f = open('wifi.conf', 'w') 
    f.write('country=GB\n') 
    f.write('ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n') 
    f.write('update_config=1\n') 
    f.write('\n') 
    f.write('network={\n') 
    f.write(' ssid="' + ssid + '"\n') 
    f.write(' psk="' + psk + '"\n') 
    f.write('}\n') 
    f.close() 

    cmd = 'mv wifi.conf ' + wpa_supplicant_conf 
    cmd_result = "" 
    cmd_result = os.system(cmd) 
    print cmd + " - " + str(cmd_result) 


    # restart wifi adapter 
    cmd = sudo_mode + 'ifdown wlan0' 
    cmd_result = os.system(cmd) 
    print cmd + " - " + str(cmd_result) 

    time.sleep(2) 

    cmd = sudo_mode + 'ifup wlan0' 
    cmd_result = os.system(cmd) 
    print cmd + " - " + str(cmd_result) 

    time.sleep(10) 

    cmd = 'iwconfig wlan0' 
    cmd_result = os.system(cmd) 
    print cmd + " - " + str(cmd_result) 

    cmd = 'ifconfig wlan0' 
    cmd_result = os.system(cmd) 
    print cmd + " - " + str(cmd_result) 

    p = subprocess.Popen(['ifconfig', 'wlan0'], stdout=subprocess.PIPE, 
          stderr=subprocess.PIPE) 

    out, err = p.communicate() 

    ip_address = "<Not Set>" 

    for l in out.split('\n'): 
     if l.strip().startswith("inet addr:"): 
      ip_address = l.strip().split(' ')[1].split(':')[1] 

    return ip_address 



def ssid_discovered(): 

    Cells = Cell.all('wlan0') 

    wifi_info = 'Found ssid : \n' 

    for current in range(len(Cells)): 
     wifi_info += Cells[current].ssid + "\n" 


    wifi_info+="!" 

    print wifi_info 
    return wifi_info 


def handle_client(client_sock) : 

    # get ssid 
    client_sock.send(ssid_discovered()) 
    print "Waiting for SSID..." 


    ssid = client_sock.recv(1024) 
    if ssid == '' : 
     return 

    print "ssid received" 
    print ssid 

    # get psk 
    client_sock.send("waiting-psk!") 
    print "Waiting for PSK..." 


    psk = client_sock.recv(1024) 
    if psk == '' : 
     return 

    print "psk received" 

    print psk 

    ip_address = wifi_connect(ssid, psk) 

    print "ip address: " + ip_address 

    client_sock.send("ip-addres:" + ip_address + "!") 

    return 



try: 
    while True: 

     server_sock=BluetoothSocket(RFCOMM) 
     server_sock,bind(("",PORT_ANY)) 
     server_sock.listen(1) 

     port = server_sock.getsockname()[1] 

     uuid = "815425a5-bfac-47bf-9321-c5ff980b5e11" 

     advertise_service(server_sock, "RaspberryPiServer", 
          service_id = uuid, 
          service_classes = [ uuid, SERIAL_PORT_CLASS ], 
          profiles = [ SERIAL_PORT_PROFILE ], 
          protocols = [ OBEX_UUID ] 
          ) 

     print("Waiting for connection on RFCOMM channel %d" % port) 

     client_sock, client_info = server_sock.accept() 
     print "Accepted connection from ", client_info 

     handle_client(client_sock) 

     client_sock.close() 
     server_sock.close() 

     # finished config 
     print 'Finished configuration\n' 


except (KeyboardInterrupt, SystemExit): 

    print '\nExiting\n' 

이 코드 출력

Traceback (most recent call last): File "test.py", line 128, in <module> 
    server_sock=BluetoothSocket(RFCOMM) 

NameError: name 'BluetoothSocket' is not defined 
+0

감사를 설치하려고합니다. 왜 그 오류를 생성하는지 어떤 아이디어? – syrj2112

+0

죄송합니다. 나는 블루투스 프로그래밍을 해 본 적이 없다. 하지만 표준 라이브러리에는 '블루투스'모듈이 없습니다. 당신은 아마도 당신의 질문을 편집하고 사용하고있는 라이브러리와 아마 당신의 OS를 알려 주어야 할 것입니다. 왜냐하면 블루투스 지원이 아마 OS에 맞을 것이기 때문입니다. – Chris

+0

블루투스 라이브러리를 설치할 수있는 Raspberry Pi 3가 포함 된 프로젝트입니다. Im은이 프로젝트와 관련된 모든 라이브러리를 확실히 모르지만 스크립트 상단에 통합되어 있습니다. – syrj2112

답변

0

당신은 우리에게 당신의 파이썬 버전을 표시 할 수 있습니까?

그냥 유형 :

$ python --version 

bluetooth lib 디렉토리가 존재하는 것으로 보인다. 하지만 BluetoothSocket : 그래서 제대로 포맷하는 방법을 몰랐어요이 상당히 새로운, 그 @ 크리스에 대한 bluez 및 파이썬 - bluez

$ sudo apt-get install bluez 
$ sudo apt-get install python-bluez