send data from b 3buetooth to python script raspberry pi

Solutions on MaxInterview for send data from b 3buetooth to python script raspberry pi by the best coders in the world

showing results for - "send data from b 3buetooth to python script raspberry pi"
Niko
16 Oct 2019
1#!/usr/bin/env python
2#Run.py
3import os
4from bluetooth import *
5from wifi import Cell, Scheme
6import subprocess
7import time
8wpa_supplicant_conf = "/etc/wpa_supplicant/wpa_supplicant.conf"
9sudo_mode = "sudo "
10def wifi_connect(ssid, psk):
11    # write wifi config to file
12    cmd = 'wpa_passphrase {ssid} {psk} | sudo tee -a {conf} > /dev/null'.format(
13            ssid=str(ssid).replace('!', '\!'),
14            psk=str(psk).replace('!', '\!'),
15            conf=wpa_supplicant_conf
16        )
17    cmd_result = ""
18    cmd_result = os.system(cmd)
19    print cmd + " - " + str(cmd_result)
20    # reconfigure wifi
21    cmd = sudo_mode + 'wpa_cli -i wlan0 reconfigure'
22    cmd_result = os.system(cmd)
23    print cmd + " - " + str(cmd_result)
24    time.sleep(10)
25    cmd = 'iwconfig wlan0'
26    cmd_result = os.system(cmd)
27    print cmd + " - " + str(cmd_result)
28    cmd = 'ifconfig wlan0'
29    cmd_result = os.system(cmd)
30    print cmd + " - " + str(cmd_result)
31    p = subprocess.Popen(['hostname', '-I'], stdout=subprocess.PIPE,
32                            stderr=subprocess.PIPE)
33    out, err = p.communicate()
34    if out:
35        ip_address = out
36    else:
37        ip_address = "<Not Set>"
38    return ip_address
39def ssid_discovered():
40    Cells = Cell.all('wlan0')
41    wifi_info = 'Found ssid : \n'
42    for current in range(len(Cells)):
43        wifi_info +=  Cells[current].ssid + "\n"
44    wifi_info+="!"
45    print wifi_info
46    return wifi_info
47def handle_client(client_sock) :
48    # get ssid
49    client_sock.send(ssid_discovered())
50    print "Waiting for SSID..."
51    ssid = client_sock.recv(1024)
52    if ssid == '' :
53        return
54    print "ssid received"
55    print ssid
56    # get psk
57    client_sock.send("waiting-psk!")
58    print "Waiting for PSK..."
59    psk = client_sock.recv(1024)
60    if psk == '' :
61        return
62    print "psk received"
63    print psk
64    ip_address = wifi_connect(ssid, psk)
65    print "ip address: " + ip_address
66    client_sock.send("ip-address:" + ip_address + "!")
67    return
68try:
69    while True:
70        server_sock=BluetoothSocket( RFCOMM )
71        server_sock.bind(("",PORT_ANY))
72        server_sock.listen(1)
73        port = server_sock.getsockname()[1]
74        uuid = "815425a5-bfac-47bf-9321-c5ff980b5e11"
75        advertise_service( server_sock, "RPi Wifi config",
76                           service_id = uuid,
77                           service_classes = [ uuid, SERIAL_PORT_CLASS ],
78                           profiles = [ SERIAL_PORT_PROFILE ])
79        print "Waiting for connection on RFCOMM channel %d" % port
80        client_sock, client_info = server_sock.accept()
81        print "Accepted connection from ", client_info
82        handle_client(client_sock)
83        client_sock.close()
84        server_sock.close()
85        # finished config
86        print 'Finished configuration\n'
87except (KeyboardInterrupt, SystemExit):
88    print '\nExiting\n'
89
similar questions