I am trying to connect a BCG sensor (Murata Sensor) to a wifi network and get data from it. I have configured the sensor in suppose a wifi named "Wifi1" and then I am trying to access the data from the sensor on my Mac which is also connected on Wifi1. The python script first tries finding the node using ssdpSearch function and then executes the later code. Here is the entire code :
import socket
import time
import datetime
REPEAT = 10
def ssdpSearch():
print("Starting SSDP Search. 10 seconds.")
UDP_IP = '<broadcast>'
UDP_PORT = 2000
UDP_MESSAGE = '{"type":"SCS-DISCOVER","hostname":"Host-SCS"}'
networks = socket.gethostbyname_ex(socket.gethostname())[2] # Find all networks (i.e, wifi, wired)
sockets = []
for net in networks: # Connect to all networks
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # Allow broadcast
sock.bind((net, UDP_PORT)) # Connect
sock.settimeout(1.0) # Set timeout (if no answer when reading)
sockets.append(sock) # Save "sock" to sockets
timeStart = time.time()
devices = []
print('Found devices:')
time.sleep(0.1)
while time.time() - timeStart < REPEAT:
for sock in sockets:
try:
sock.sendto(UDP_MESSAGE.encode(), (UDP_IP, UDP_PORT))
data, addr = sock.recvfrom(1024)
data = data.decode()
data = data[1:].split(',')
if data[0] == '"type":"SCS-NOTIFY"': # Only accept correct responses
oldDevice = 0
# print(data)
for dev in devices:
if dev[0] == data[1]:
oldDevice = 1
if not oldDevice:
devices.append([data[1],data[2]]) # Save found devices
print('\t' + data[1] + ' ' + data[2])
except:
1
time.sleep(0.2)
if not len(devices):
print('\t No devices found.')
print('')
for sock in sockets:
sock.close()
def readLine(s):
# Function to read status from BCG data
line = s.recv(1024).decode()
return line
def main():
print('BCG Data Logger \n Logs either raw data or BCG algorithm data to a file \ depending on configured mode.')
# Open file
IP = input('Insert IP address (empty for SSDP): ')
while len(IP) == 0:
ssdpSearch()
IP = input('Insert IP address (empty for SSDP): ')
PORT = 8080
filename = 'logged_data_' + str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) + '.txt'
fid = open(filename,'w')
print('')
print('Starting to read data. Press \"ctrl+c\" to quit.')
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.connect((IP, PORT))
while True: #
data = readLine(s)
print(data,)
fid.write(data)
except (KeyboardInterrupt, SystemExit):
print('Exiting program.')
fid.close()
break
except (socket.timeout):
print('Timed out, reconnecting.')
except socket.error as msg:
print(msg)
print('Trying to reconnect.')
if __name__ == '__main__':
main()
In the output it is first asking me the IP address which I have no clue which IP address is it asking. We may leave it blank if it is SSDP so if I leave it blank it gives following error:
>Insert IP address (empty for SSDP):
>Starting SSDP Search. 10 seconds.
>Traceback (most recent call last):
>File "DataCollection.py", line 91, in <module>
>main()
>File "DataCollection.py", line 64, in main
>ssdpSearch()
>File "DataCollection.py", line 17, in ssdpSearch
>networks = socket.gethostbyname_ex(socket.gethostname())[2] # Find all networks (i.e, wifi, wired)
>socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Can anyone help me out to understand this.
Thank you in advance.