-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_ap.py
119 lines (112 loc) · 2.79 KB
/
wifi_ap.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
# -*- coding: utf-8 -*-
import wifimodule
import socket
import time
import sys
import threading
HOST = '169.254.200.13'
PORT = 5005
BUFFER_SIZE = 8
count = 0
#set ip wifi reception
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
except socket.error as errmsg:
if s:
s.close()
print (errmsg)
sys.exit(1)
def receiveOnWifi():
conn, addr = s.accept()
try:
data = conn.recv(BUFFER_SIZE)
finally:
if data:
#return data
try:
conn.sendall(data)
finally:
conn.close()
return data
else:
conn.close()
return 'No data received'
while 1:
rx_data = receiveOnWifi()
print 'Received message :'+ rx_data
time.sleep(1)
##ssid = 'RPi_net'
##password = 'raspberry'
###connect to saved network with SSID 'Pi_net'
##
##wifi_conn = wifimodule.Connect(ssid, password)
##
##if wifi_conn == False:
## print 'wifi connection to ' + ssid + ' failed.'
##else:
## print 'wifi connected to '+ ssid + '.' #temporal
##
##if wifi_conn.ssid == 'RPi_net'
## IP = wifi_conn.address
## PORT = 5005
## BUFFER_SIZE = 8
##
## #set ip wifi reception
## try:
## s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## s.bind((HOST, PORT))
## s.listen(1)
## except socket.error as errmsg:
## if s:
## s.close()
## print (errmsg)
## sys.exit(1)
##
## def receiveOnWifi():
## conn, addr = s.accept()
## try:
## data = conn.recv(BUFFER_SIZE)
## finally:
## conn.close()
## return data
##
## #set up wifi transmission
## def sendOverWifi(msg):
## HOST = '169.254.214.14'
## PORT = 5005
##
## try:
## s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## s.connect((HOST, PORT))
## except socket.error as errmsg:
## if s:
## s.close()
## print (errmsg)
## sys.exit(1)
##
## s.send(msg)
## s.close()
##
## while 1:
## rx_data = receiveOnWifi()
## print 'Received message :'+ rx_data
##
## time.sleep(1)
##
## sendOverWifi('Bonjour')
##
##from wifi import Cell, Scheme
##
###connect to a network
##wnet_scheme = Scheme.find('wlan0', 'pi_net')
##
##if wnet_scheme == None:
## #search for network
## wnet = Cell.all('wlan0')[0]
## wnet_scheme = Scheme.for_cell('wlan0', 'pi_net', wnet, passkey)
## wnet_scheme.save()
## wnet_scheme.activate()
##else:
## wnet_scheme.activate()