Skip to content

Commit

Permalink
Broadcast script
Browse files Browse the repository at this point in the history
  • Loading branch information
dakriy committed Feb 11, 2019
1 parent 331f690 commit 3b7fb95
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
70 changes: 70 additions & 0 deletions broadcast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from socket import *
import time
import netifaces as ni
from struct import *
import select

ip = ni.ifaddresses('eth0')[ni.AF_INET][0]['broadcast']
me = ni.ifaddresses('eth0')[ni.AF_INET][0]['addr']

broadcastPort = 42070
connPort = 42069

connected = False
connAddr = ''

pingTime = 0
sentTime = 0

with socket(AF_INET, SOCK_DGRAM) as conn:
conn.bind(('', connPort))
conn.setblocking(0)
with socket(AF_INET, SOCK_DGRAM) as cs:
cs.setblocking(0)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while not connected:

if time.time() - sentTime > 5:
cs.sendto(('magic|' + gethostname() + '|end').encode(), (ip, broadcastPort))
sentTime = time.time()

inputready,outputready,other = select.select([conn], [], [], 0)
for s in inputready:
if s == conn:
while True:
try:
data, addr = conn.recvfrom(65535)
data = data.decode().strip()
if data == 'connectRequest':
print('sending reply')
conn.sendto(('connect').encode(), addr)
if data == 'connected':
print('connected to ' + str(addr))
connAddr = addr
connected = True
pingTime = time.time()
except BlockingIOError:
break
while connected:
if time.time() - pingTime > 15:
connected = False
inputready,outputready,other = select.select([conn], [], [], 0)
for s in inputready:
if s == conn:
print('here')
while True:
try:
data, addr = conn.recvfrom(65535)
type = unpack('B', data)[0]
if type == 0:
print('recieved ping packet')
d = pack('B', type)
conn.sendto(d, connAddr)
pingTime = time.time()
print('ping!')
except BlockingIOError:
break



2 changes: 1 addition & 1 deletion runinScripts/get-garbage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Print readings
while True:
next = int(time.time())
if next == current+10*60: #use the decimal to indicate how many minutes you want
if next > current+10*60: #use the decimal to indicate how many minutes you want
f = open("/home/pi/Measurements/"+time.ctime()+".txt", 'w')
current = next
if sensor.read():
Expand Down
2 changes: 1 addition & 1 deletion runinScripts/record-video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def exit_gracefully(self, signum, frame):
self.kill_now = True

start = time.time()
framerate = 25
framerate = 10
fourcc = "DIVX"
cap = cv2.VideoCapture(0)
file = "/home/pi/Measurements/Video/zucc " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start)) + ".avi"
Expand Down

0 comments on commit 3b7fb95

Please sign in to comment.