BWP-SS19-01/Fein Design/BLEundLogging: Unterschied zwischen den Versionen

Aus Verteilte Systeme - Wiki
Zur Navigation springen Zur Suche springen
Zeile 68: Zeile 68:
 
# now, lets read this file
 
# now, lets read this file
 
fIn = open(textFile + ".txt", "r")
 
fIn = open(textFile + ".txt", "r")
  +
#fOut = open("/var/www//html/itse/log"+textFileNew, "a+")
 
fOut = open(textFileNew, "a+")
 
fOut = open(textFileNew, "a+")
 
if fIn.mode == 'r' and fOut.mode == "a+":
 
if fIn.mode == 'r' and fOut.mode == "a+":
 
print "OK.. lets read\n"
 
print "OK.. lets read\n"
 
contents = fIn.readline()
 
contents = fIn.readline()
  +
timestamp = datetime.datetime.now()
 
  +
print "DATUM: ",timestamp,"\n"
 
while contents:
 
while contents:
 
if "255" in contents:
 
if "255" in contents:
Zeile 78: Zeile 80:
 
#print "MSG: ",msg
 
#print "MSG: ",msg
 
 
if "ff" in msg[:2] and "a0" in msg[2:4]:
+
if "f1" in msg[:2] and "a0" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
fOut.write("SUCCESS: ")
 
  +
toWrite = str(timestamp)+" ENERGIEMANAGEMENT SUCCESS: "
if "ff" in msg[:2] and "a1" in msg[2:4]:
 
fOut.write("WARNING: ")
+
fOut.write(toWrite)
if "ff" in msg[:2] and "a2" in msg[2:4]:
+
if "f1" in msg[:2] and "a1" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
fOut.write("ERROR: ")
 
  +
toWrite = str(timestamp)+" ENERGIEMANAGEMENT WARNING: "
if "ff" in msg[:2] and "a3" in msg[2:4]:
 
fOut.write("FATAL ERROR: ")
+
fOut.write(toWrite)
if "ee" in msg[:2] and "ee" in msg[2:4]:
+
if "f1" in msg[:2] and "a2" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
fOut.write("MSG: ")
 
  +
toWrite = str(timestamp)+" ENERGIEMANAGEMENT ERROR: "
if "ee" in msg[:2] and "ef" in msg[2:4]:
 
fOut.write("MSG: ")
+
fOut.write(toWrite)
if "aa" in msg[:2] and "ff" in msg[2:4]:
+
if "f1" in msg[:2] and "a3" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
fOut.write("AKKU: ")
 
  +
toWrite = str(timestamp)+" ENERGIEMANAGEMENT FATAL_ERROR: "
 
fOut.write(toWrite)
 
if "f1" in msg[:2] and "a4" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" ENERGIEMANAGEMENT AKKU: "
  +
fOut.write(toWrite)
  +
  +
if "f2" in msg[:2] and "a0" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" ODOMETRY SUCCESS: "
 
fOut.write(toWrite)
 
if "f2" in msg[:2] and "a1" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" ODOMETRY WARNING: "
 
fOut.write(toWrite)
  +
if "f2" in msg[:2] and "a2" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" ODOMETRY ERROR: "
 
fOut.write(toWrite)
 
if "f2" in msg[:2] and "a3" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" ODOMETRY FATAL_ERROR: "
  +
fOut.write(toWrite)
  +
  +
if "f3" in msg[:2] and "a0" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" MAINCONTROL SUCCESS: "
  +
fOut.write(toWrite)
  +
if "f3" in msg[:2] and "a1" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" MAINCONTROL WARNING: "
  +
fOut.write(toWrite)
  +
if "f3" in msg[:2] and "a2" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" MAINCONTROL ERROR: "
  +
fOut.write(toWrite)
  +
if "f3" in msg[:2] and "a3" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" MAINCONTROL FATAL_ERROR: "
  +
fOut.write(toWrite)
  +
  +
if "ff" in msg[:2] and "ff" in msg[2:4]:
  +
timestamp = datetime.datetime.now()
  +
toWrite = str(timestamp)+" UNDEFINED NACHRICHT: "
  +
fOut.write(toWrite)
   
 
newMsg = msg[4:]
 
newMsg = msg[4:]

Version vom 30. Juli 2019, 10:12 Uhr

Mittels der Funktion void BLE_SendData(void * Data, int Art_des_Inhaltes) kann jedes Modul eine Nachricht absenden.
Intern arbeitet die Funktion mit einem Switch-case, um je nach Inhalt die ersten 4 Bits in der Bluetooth-Nachricht entsprechend zu setzen.


Protokoll-Flags
- "AA FF" - Übertragung des aktuellen Akkustands mittels Float.

- "FE A0" - Übertragung eines SUCCESS
- "FE A1" - Übertragung eines WARNINGs (Stufe 1)
- "FE A2" - Übertragung eines ERRORs (Stufe 2)
- "FE A3" - Übertragung eines FATAL_ERRORs (Stufe 3)

- "EE EE" - Nachricht "Sleepmodus aktiv"
- "EE EF" - Nachricht "Sleepmodus inaktiv"

Weitere Protokoll-Flags kommen im Laufe der weiteren Entwicklung dazu.
Außerdem wird in der weiteren Entwicklung ein Timestamp und von welchem Modul die Nachricht versendet wurde hinzukommen, sodass das Filtern der Daten einfacher und eindeutiger gestaltet werden kann.
Beispielsweise wird der Log-Eintrag wie folgt aussehen:
2018-12-25 09:27:53 Energiemanagement AKKU: 33.6


HIER void BLE_SendData(void * Data, int Art_des_Inhaltes) Code einfügen.


Auf der anderen Seite wird das gesendete Signal empfangen. Dies passiert mittels einem BLE-USB-Stick, einem Laptop und einem Python-Script.
Das Python-Script nutzt die 'bluepy'-Bibliothek, welches [| hier] verfügbar ist.

Das Script empfängt zum Einen die Nachricht und zum Anderen werden die übermittelten Hex-Zeichen in ASCII-Zeichen umgewandelt, damit es für den Mensch besser lesbar ist.
Hier werden die ersten 4 Bits berücksichtigt und je nach Flag die Nachricht entsprechende dekodiert.

# only runs with sudo
# sudo ble.py filename (without .txt)
import bluepy
from bluepy.btle import Scanner, DefaultDelegate
import sys
import datetime
import struct
textFile = ""
class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)
        global textFile
        textFile = sys.argv[1]

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print "Discovered device", dev.addr
        elif isNewData:
            print "Received new data from", dev.addr

scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
f = open(textFile + ".txt", "w+")
for dev in devices:
    print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
    #print "Test %s" % (dev.getValueText())
    for (adtype, desc, value) in dev.getScanData():
        #print " %s = %s = %s" % (adtype, desc, value)
	#print " %s = %s" % (adtype, value)
        f.write("%s = %s\n" % (adtype, value))
f.close()

# lets copy the file to keep the full information
textFileNew = textFile + "New" + ".txt"

setHex = "0x"
# now, lets read this file
fIn = open(textFile + ".txt", "r")
#fOut = open("/var/www//html/itse/log"+textFileNew, "a+")
fOut = open(textFileNew, "a+")
if fIn.mode == 'r' and fOut.mode == "a+":
	print "OK.. lets read\n"
	contents = fIn.readline()
	timestamp = datetime.datetime.now()
	print "DATUM: ",timestamp,"\n"
	while contents:
		if "255" in contents:
			msg = contents[6:]
			#print "MSG: ",msg
			
			if "f1" in msg[:2] and "a0" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ENERGIEMANAGEMENT SUCCESS: "
				fOut.write(toWrite)
			if "f1" in msg[:2] and "a1" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ENERGIEMANAGEMENT WARNING: "
				fOut.write(toWrite)
			if "f1" in msg[:2] and "a2" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ENERGIEMANAGEMENT ERROR: "
				fOut.write(toWrite)
			if "f1" in msg[:2] and "a3" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ENERGIEMANAGEMENT FATAL_ERROR: "
				fOut.write(toWrite)
			if "f1" in msg[:2] and "a4" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ENERGIEMANAGEMENT AKKU: "
				fOut.write(toWrite)			

			if "f2" in msg[:2] and "a0" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ODOMETRY SUCCESS: "
				fOut.write(toWrite)
			if "f2" in msg[:2] and "a1" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ODOMETRY WARNING: "
				fOut.write(toWrite)
			if "f2" in msg[:2] and "a2" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ODOMETRY ERROR: "
				fOut.write(toWrite)
			if "f2" in msg[:2] and "a3" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" ODOMETRY FATAL_ERROR: "
				fOut.write(toWrite)

			if "f3" in msg[:2] and "a0" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" MAINCONTROL SUCCESS: "
				fOut.write(toWrite)
			if "f3" in msg[:2] and "a1" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" MAINCONTROL WARNING: "
				fOut.write(toWrite)
			if "f3" in msg[:2] and "a2" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" MAINCONTROL ERROR: "
				fOut.write(toWrite)
			if "f3" in msg[:2] and "a3" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" MAINCONTROL FATAL_ERROR: "
				fOut.write(toWrite)

			if "ff" in msg[:2] and "ff" in msg[2:4]:
				timestamp = datetime.datetime.now()
				toWrite = str(timestamp)+" UNDEFINED NACHRICHT: "
				fOut.write(toWrite)

			newMsg = msg[4:]
			# the first appear of the "00" string shows the end of the message!
			findMsg = newMsg.find("00")
		
			for i in range(0, findMsg, 2):
				toConvert = newMsg[i:i+2]
				zahl = int(setHex+toConvert,16)
				print zahl,'=>',chr(zahl)
		       		fOut.write(chr(zahl))
			fOut.write("\n")
		contents = fIn.readline()
fIn.close()
fOut.close()