""" Created on Thu Jan 11 15:22:56 2018 @author: Robin Cavalieri IMA5SC - PFE 2018 P32 - Détection automatique de DoS sur réseau LoRa Main - prédictions, affichage, vérification du trafic """ ############################################################################### #LIBRAIRIES UTILES ############################################################################### import time; import serial; import numpy as np; # Réseau de neurones from keras.models import Sequential from keras.layers import Dense from keras.models import model_from_json ############################################################################### ############################################################################### #VARIABLES GLOBALES INITIALISATION ############################################################################### ping_mesured=0; rssi_mesured=0; result=0; puissance_read = 0; temp_read = 0; trame_read = []; file_name = 'C:/Users/Utilisateur/PFE/python/datasets/data_to_pred.csv'; ############################################################################### ############################################################################## #DEFINITION DE LA LECTURE DE PORT ############################################################################## ser = serial.Serial( #Pour Linux #port='/dev/ttyACM0', #Pour Windows port='COM4', #Vitesse de communication baudrate = 9600, #Parité parity=serial.PARITY_NONE, #Bit de stop stopbits=serial.STOPBITS_ONE, #Taille du message bytesize=serial.EIGHTBITS, #Out timeout=1 ) counter=0; ############################################################################## ############################################################################### #FONCTION DE PREDICTION APPELEE DANS LE MAIN ############################################################################### def Predictions(data_eval): ############################################################################### #ARCHITECTURE DU RESEAU DE NEURONES par le biais de la librairie KERAS #ANN ############################################################################### #Création du classifieur classifier = Sequential(); #Couche d'entrée donc avec 2 entrées, PING et DELTA_RSSI classifier.add(Dense(32, activation='relu', input_dim=2)) #Couche de sortie classifier.add(Dense(1, activation='sigmoid')) ############################################################################### #COMPILATION ############################################################################### # optimizer : algorithme choisi pour trouver le model (le plus puissant) # loss : Si deux valeurs en sortie (binairie outcome) : binary_crossentropy classifier.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy']); ############################################################################### #LOAD DU TRAINING SET ########################################################################### json_file=open('C:/Users/Utilisateur/PFE/python/Training/training.json','r'); loaded_model_json=json_file.read(); json_file.close(); loaded_model=model_from_json(loaded_model_json); loaded_model.load_weights('C:/Users/Utilisateur/PFE/python/Training/training.h5'); ########################################################################### #REALISER DES PREDICTIONS ########################################################################### #Normalisation des données #sc = StandardScaler(); predictions = classifier.predict(data_eval); return predictions; ############################################################################### ############################################################################### #AFFICHAGE ############################################################################### def Recorder(): global temp; global temp_read; global ping_mesured; global puissance_read; global trame_read; global cpt_trames; global cpt_trames_temp; while(1): #RECUPERATION DES DONNEES x = ser.readline(); if(0x7C in x): #Lecture de la trame trame_read = x; #Récupération du compteur de trames cpt_trames = (trame_read[15]-48)*100 + (trame_read[16]-48)*10 + (trame_read[17]-48); if(0x7C in x and cpt_trames == 1): temp = time.time(); if(0x7C in x and cpt_trames > 1): t2 = time.time(); ping_mesured = (t2-temp)*1000; #Récupération de la température temp_read = (trame_read[12]-48)*10 + trame_read[13]-48; #Récupération de la puissance x = ser.readline(); puissance_read = (x[1]-48)*10 + x[2]-48; #T2 temp = t2; #SET INPUTS PREDICTION data_to_pred = np.array((ping_mesured, puissance_read),dtype = np.int64); data_to_pred = data_to_pred.reshape((1,2)); #PREDICTION TEMPS REEL pred = Predictions(data_to_pred); if(cpt_trames_temp+1 == cpt_trames): #AFFICHAGE print("\n###############################\n"); print("TRAME : "+ "".join(map(chr, trame_read))+"\n"); print("TRAME NUM. : "+str(cpt_trames)+"\n"); print("TEMPERATURE : "+str(temp_read)+" °C\n"); print("PUISSANCE : "+str(puissance_read)+" dBm\n"); print("PING : "+str(ping_mesured)+" ms\n"); np.round(pred,0); if(pred==1): print("TRAFIC : DANGER\n"); elif(pred==0): print("TRAFIC : RAS\n"); print("###############################\n"); cpt_trames_temp = cpt_trames; ############################################################################### ############################################################################### #MAIN ############################################################################### if __name__ == '__main__': Recorder(); ###############################################################################