#! /usr/bin/env python3
#-*- coding: utf-8 -*-

# python3 /home/scripts_python_tests_capteurs/UDP_server/udp_server_1.py
#----------------------------------------------------------------------

import socket

My_IP     	= "192.168.1.81"
localIP     = ""
localPort   = 31559
bufferSize  = 1024

msg_from_scanner = "AQCM05"

UDP_Sonde_Socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

UDP_Sonde_Socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)


# Bind to address and ip

UDP_Sonde_Socket.bind((localIP, localPort))

print("le serveur UDP de la sonde est actif...")

while(True):

	bytesAddressPair = UDP_Sonde_Socket.recvfrom(bufferSize)

	message = bytesAddressPair[0].decode('utf-8')
	address = bytesAddressPair[1]
	
	if message == msg_from_scanner :
	
		print( My_IP + " -> " + str(address) )
		bytesToSend = str.encode( My_IP )
		UDP_Sonde_Socket.sendto(bytesToSend, address)
	
