German BLE2 TI Sensor Tag

pucki

Active Member
Licensed User
Longtime User
er hat or und and vertauscht ;)

Du solltest die Uralte Technik der Logikchips und deren Funktionsweise lernen. ;)

Ich hab noch einen Philipps CE 1602 Computerlernbaukasten aus den 70er wo man genau das lernt. Das Ding ist so selten das selbst Infos im Netz Mangelware sind ;)

Gruß

Pucki
 

pucki

Active Member
Licensed User
Longtime User
Das freut mich wirklich. Ich mags nämlich wenn ein Projekt begonnen und auch durchgeführt wird.

Besonders wenn es sich um ein Kombi-Projekt handelt.

Gruß

Pucki
 

DonManfred

Expert
Licensed User
Longtime User

Wolli013

Well-Known Member
Licensed User
Longtime User
Vielen Dank nochmal an euch die mir Tipps, Tricks und Code gegeben haben, ohne Euch wäre das wohl nichts geworden!
Als nächstes wollte ich den Code noch in B4I einbringen, hoffe das klappt auch so gut wie mit B4A.
So, hier jetzt nochmal der komplette Code wo es richtig gut läuft.


B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public connected As Boolean = False
    Public ConnectedName As String
    Private ConnectedServices As List
    Dim BlueATemperatur,BlueALuftfeuchtigkeit As Double = 0
    Dim Twert,Fwert As Float
    Dim Zaehler As Int
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Create
    manager.Initialize("manager")
End Sub

Public Sub Scan
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on")
    Else
        manager.Scan(Null) 'alle Geräte scannen
    End If
End Sub
 
Public Sub ReadData
    For Each s As String In ConnectedServices
     manager.ReadData(s)
    Next
End Sub

Public Sub Disconnect
    manager.Disconnect
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log("Name: " & Name)
 Log("Mac Adresse: " & Id)
  Log("RSSI: " & RSSI)
   Log("Zusatzdaten: " & AdvertisingData)
    ConnectedName = Name
     manager.StopScan
      manager.Connect(Id)
End Sub
 
Public Sub writeData1
'Temperatur und Feuchtresensor
    manager.SetNotify("f000aa20-0451-4000-b000-000000000000","f000aa21-0451-4000-b000-000000000000",True)
    Log("Set Notify")
    Sleep(2000)
    
    Dim valoriByte(1) As Byte
    valoriByte(0)=0x01
    manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa22-0451-4000-b000-000000000000",valoriByte)
    Log("Set WriteData1")   
    Sleep(2000)

'    Dim valoriByte(1) As Byte 'Period
'    valoriByte(0)=0x64
'    manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa23-0451-4000-b000-000000000000",valoriByte)
'    Log("Set WriteData2")   
'    Sleep(2000)   
'Period AA23* R/W  2.55 sec (0xFF). Default 1 second (0x64).
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)

        Private tem,hum As Int
        Private temp1,temp2,hum1 As Float
        Dim ArrayBytes() As Byte = Characteristics.Get("f000aa21-0451-4000-b000-000000000000")
 
        tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff))
        hum = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(3), 8), 0x0000ff00), Bit.And(ArrayBytes(2), 0x000000ff))
        
        temp1 = (tem/65536)*165-40 ' = Grad
        temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
        hum1 = (hum/65536)*100 ' = Relative Luftfeuchtigkeit
 
         Log("Temperatur-Grad = " & NumberFormat(temp1, 1, 2))
          Log("Temperatur-Fahrenheit = " & NumberFormat(temp2, 1, 2))
           Log("Relative-Feuchte = " & NumberFormat(hum1, 1, 2))
          
'Mittelwert aller 10 Ergebnisse.
 Zaehler = Zaehler + 1
 Twert = Twert + temp1
  Fwert = Fwert + hum1
If Zaehler = 10 Then
 temp1 = Twert / 10
  hum1 = Fwert / 10
   If hum1 > 100 Then hum1 = 100
    BlueATemperatur = NumberFormat(temp1, 1, 2) 'Übergabedaten
    BlueALuftfeuchtigkeit = NumberFormat(hum1, 1, 2) 'Übergabedaten
    Zaehler = 0
   Twert = 0
  Fwert = 0
End If

End Sub

Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    Sleep(2000)   
    writeData1
End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False
End Sub

Sub Manager_RssiAvailable (Success As Boolean, RSSI As Double)
   Log(Success)
   Log(RSSI)
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Ich versuche es jetzt gerade in B4I zu implementieren aber bekomme es nicht hin!
Weis einer wo da der große Unterschied ist das es mit meinem Code aus B4A nicht geht?
Ich kann den Batteristatus auslesen also Verbindung und alles da,
aber bekomme einfach keine Temp- und Feuchtewerte.
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Hier mal den Code den ich in B4A verwende.


B4X:
'Bluetoothverbing herstellen---------------------------------------------------------

Public Sub Scan
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on")
    Else
        manager.Scan(Null) 'alle Geräte scannen
    End If
End Sub
 
Public Sub ReadData
    Log("Read")
    For Each s As String In ConnectedServices
     manager.ReadData(s)
    Next
    Sleep(8000)
End Sub

Public Sub Disconnect
    manager.Disconnect
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
 
Log("Name: " & Name)
 Log("Mac Adresse: " & Id)
  Log("RSSI: " & RSSI)
   Log("Zusatzdaten: " & AdvertisingData)
 
    ConnectedName = Name
     manager.StopScan
      manager.Connect(Id)
 
End Sub
 
Public Sub writeData1
 
 'Enable Temperatur und Feuchtresensor
    manager.SetNotify("F000AA20-0451-4000-B000-000000000000","F000AA21-0451-4000-B000-000000000000",True)
    Log("Set Notify")
'    Sleep(500)
 
    Dim valoriByte(1) As Byte
    valoriByte(0)=0x01
    manager.WriteData("F000AA20-0451-4000-B000-000000000000","F000AA22-0451-4000-B000-000000000000",valoriByte)
    Log("Set WriteData1")   
'    Sleep(500)

    Dim valoriByte(1) As Byte 'Period
    valoriByte(0)=0xFF
    manager.WriteData("F000AA20-0451-4000-B000-000000000000","F000AA23-0451-4000-B000-000000000000",valoriByte)
    Log("Set WriteData2")   
    Sleep(500)   
    'Period AA23* R/W  2.55 sec (0xFF). Default 1 second (0x64).
 
End Sub

Sub Manager_DataAvailable (Service As String, Characteristics As Map)
Log("Manager_DataAvailable")
 
    Select Service
        Case "F000AA20-0451-4000-B000-000000000000"
            TempFeuchte(Characteristics)       
    End Select

      For Each id As String In Characteristics.Keys 
        Log($"** Reading Sens: ${id} **"$) 'alle Sensoren
        
        If id = "Battery Level" Then 'Batterie
            Dim ArrayBytesB() As Byte = Characteristics.Get(id)
            Log("Batterie: "&ArrayBytesB(0) & " %")
        End If
    Next
 
End Sub
 
Sub TempFeuchte(Characteristics As Map)
    
    writeData1
    
    For Each id As String In Characteristics.Keys   
 
        If id = "F000AA21-0451-4000-B000-000000000000" Then   
                
        Log("Startet")
        
        Private tem As Int
        Private hum As Int
        Private temp1,temp2,hum1 As Float
    
        Dim ArrayBytes() As Byte = Characteristics.Get("F000AA21-0451-4000-B000-000000000000")
 
         tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff))
         hum = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(3), 8), 0x0000ff00), Bit.And(ArrayBytes(2), 0x000000ff))
 
        temp1 = (tem/65536)*165-40
 
        temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
        Log("Temperatur-Grad = " & NumberFormat(temp1, 1, 2))
        Log("Temperatur-Fahrenheit = " & NumberFormat(temp2, 1, 2))
 
        hum1 = (hum/65536)*100   
 
        Log("Feuchte = " & NumberFormat(hum1, 1, 2))
 
         BlueATemperatur = NumberFormat(temp1, 1, 2) 'Übergabedaten
         BlueALuftfeuchtigkeit = NumberFormat(hum1, 1, 2) 'Übergabedaten
        
        End If
Next
End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False
End Sub

Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    Sleep(3000)   
    ReadData
End Sub

Sub Manager_RssiAvailable (Success As Boolean, RSSI As Double)
   Log(Success)
   Log(RSSI)
End Sub
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Problem gelöst!
Beispiel:
manager.WriteDataWithResponse("F000AA20-0451-4000-B000-000000000000","F000AA22-0451-4000-B000-000000000000",valoriByte)

anstatt:
manager.WriteData("f000aa20-0451-4000-b000-000000000000","f000aa22-0451-4000-b000-000000000000",valoriByte)

Alle Buchstaben müssen GROSS geschrieben werden und WriteData muss WriteDataWithResponse heissen dann läuft es! :)
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Hier jetzt der komplette Code für B4I
Warscheinlich nicht perfekt, aber es läuft schon mal.

B4X:
'Bluetoothverbing herstellen---------------------------------------------------------
Public Sub Scan
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on")
    Else
        manager.Scan(Null) 'alle Geräte scannen
    End If
End Sub
 
Public Sub ReadData
    For Each s As String In ConnectedServices
     manager.ReadData(s)
    Next
    Sleep(8000)
    writeData1
End Sub

Public Sub Disconnect
    manager.Disconnect
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
Log("Name: " & Name)
 Log("Mac Adresse: " & Id)
  Log("RSSI: " & RSSI)
   Log("Zusatzdaten: " & AdvertisingData)
    ConnectedName = Name
     manager.StopScan
      manager.Connect(Id)
End Sub
 
Public Sub writeData1
'Temperatur und Feuchtresensor
    manager.SetNotify("F000AA20-0451-4000-B000-000000000000","F000AA21-0451-4000-B000-000000000000",True)
    Log("Set Notify")
    Sleep(2000)
    
    Dim valoriByte(1) As Byte
    valoriByte(0)=0x01
    manager.WriteDataWithResponse("F000AA20-0451-4000-B000-000000000000","F000AA22-0451-4000-B000-000000000000",valoriByte)
    Log("Set WriteData1")   
    Sleep(2000)

'    Dim valoriByte(1) As Byte 'Period
'    valoriByte(0)=0x64
'    manager.WriteDataWithResponse("F000AA20-0451-4000-B000-000000000000","F000AA23-0451-4000-B000-000000000000",valoriByte)
'    Log("Set WriteData2")   
'    Sleep(2000)   
'Period AA23* R/W  2.55 sec (0xFF). Default 1 second (0x64).
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)

For Each id As String In Characteristics.Keys
        
        If id = "Battery Level" Then 'Batterie
            Dim ArrayBytesB() As Byte = Characteristics.Get(id)
            Log("Batterie: "&ArrayBytesB(0) & " %")
        End If
            
 If id = "F000AA21-0451-4000-B000-000000000000" Then
 
        Private tem,hum As Int
        Private temp1,temp2,hum1 As Float
        Dim ArrayBytes() As Byte = Characteristics.Get("F000AA21-0451-4000-B000-000000000000")
 
        tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff))
        hum = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(3), 8), 0x0000ff00), Bit.And(ArrayBytes(2), 0x000000ff))
        
        temp1 = (tem/65536)*165-40 ' = Grad
        temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
        hum1 = (hum/65536)*100 ' = Relative Luftfeuchtigkeit
 
         Log("Temperatur-Grad = " & NumberFormat(temp1, 1, 2))
          Log("Temperatur-Fahrenheit = " & NumberFormat(temp2, 1, 2))
           Log("Relative-Feuchte = " & NumberFormat(hum1, 1, 2))
          
'Mittelwert aller 10 Ergebnisse.
 Zaehler = Zaehler + 1
 Twert = Twert + temp1
  Fwert = Fwert + hum1
If Zaehler = 10 Then

 temp1 = Twert / 10
  hum1 = Fwert / 10
   If hum1 > 100 Then hum1 = 100
    BlueATemperatur = NumberFormat(temp1, 1, 2) 'Übergabedaten
    BlueALuftfeuchtigkeit = NumberFormat(hum1, 1, 2) 'Übergabedaten
    Zaehler = 0
   Twert = 0
  Fwert = 0
End If
 
 End If
Next
End Sub

Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    Sleep(2000)   
    ReadData
End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False
End Sub

Sub Manager_RssiAvailable (Success As Boolean, RSSI As Double)
   Log(Success)
   Log(RSSI)
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub 
'Bluetoothverbing herstellen---------------------------------------------------------
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Könnte mir vielleicht nochmal einer unter die Arme greifen?
Ich versuche jetzt mit diesem Code noch den Luftdruck zu messen, bekomme es aber nicht hin und weis nicht genau wo ich da ansetzen muss.

Beschreibung zum auselesen:

Barometric Pressure Sensor
The pressure sensor used on the SensorTag is BMP280 from Bosch Sensortec. The Barometric Pressure Sensor is implemented with the following source code files:
Application: sensortag_bar.c
Driver: SensorBmp280.c
Profile: barometerservice.c

Humidity Sensor
Type UUID Access Size (bytes) Description
Data AA41* R/N 6 Temp[0:7], Temp[8:15], Temp[16:23], Press[0:7], Press[8:15], Press[16:23]
Notification 2902 R/W 2 Write 0x0001 to enable notifications, 0x0000 to disable.
Configuration AA42* R/W 1 Write 0x01 to enable data collection, 0x00 to disable.
Period AA44* R/W 1 Resolution 10 ms. Range 100 ms (0x0A) to 2.55 sec (0xFF). Default 1 second (0x64).
Configuration
When an enable command (0x01) is written to the configuration characteristic, the sensor starts to perform measurements each second and the data is stored in the data characteristic. When the disable command (0x00) is issued, the sensor's measurement cycle is stopped, the data are cleared and no longer reported.

Data
The data from the pressure sensor consists of two 24-bit unsigned integers: the temperature in bytes 0-2, the pressure in bytes 3-5. The conversion and adjustment calculations is done in firmware, so the application in effect only has to divide the incoming values by 100. Conversion to temperature and pressure is done as shown below. The temperature unit is degrees Celsius, the pressure in hectopascal (hPa).

float sensorBmp280Convert(uint32_t rawValue)
{
return rawValue / 100.0f;
}


Hier das Code snipped:
B4X:
 'Luftdruck messen
        Private dr1,dr2 As Int
        Private druck1  As Float
        Dim ArrayBytesD() As Byte = Characteristics.Get("f000aa41-0451-4000-b000-000000000000")
        
        dr1 = ArrayBytesD(0) + ArrayBytesD(1) + ArrayBytesD(2)
        dr1 = Bit.And(0x0000ffff, dr1) / 100
        dr2 = ArrayBytesD(3) + ArrayBytesD(4) + ArrayBytesD(5)
        dr2 = Bit.And(0x0000ffff, dr2) / 100
    
        Log("dr1 " & dr1)
        Log("dr2 " & dr2)
 

MaFu

Well-Known Member
Licensed User
Longtime User
Was für Werte hast Du denn in den einzelnen Arrayfeldern?

Ich würds mal so probieren:
B4X:
dr1 = Bit.Or(Bit.ShiftLeft(ArrayBytes(0), 16), Bit.ShiftLeft(ArrayBytes(1), 8), ArrayBytes(2))
dr1 = Bit.And(0x00ffffff, dr1) / 100
dr2 = Bit.Or(Bit.ShiftLeft(ArrayBytes(3), 16), Bit.ShiftLeft(ArrayBytes(4), 8), ArrayBytes(5))
dr2 = Bit.And(0x00ffffff, dr2) / 100
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Danke für deine Mithilfe aber leider wird von B4A gesagt, zu viele Parameter!
dr1 = Bit.Or(Bit.ShiftLeft(ArrayBytes(0), 16), Bit.ShiftLeft(ArrayBytes(1), 8), ArrayBytes(2))
 

MaFu

Well-Known Member
Licensed User
Longtime User
Sorry, so sollte es stimmen:
B4X:
dr1 = Bit.Or(Bit.Or(Bit.ShiftLeft(ArrayBytes(0), 16), Bit.ShiftLeft(ArrayBytes(1), 8)), ArrayBytes(2))
dr1 = Bit.And(0x00ffffff, dr1) / 100
dr2 = Bit.Or(Bit.Or(Bit.ShiftLeft(ArrayBytes(3), 16), Bit.ShiftLeft(ArrayBytes(4), 8)), ArrayBytes(5))
dr2 = Bit.And(0x00ffffff, dr2) / 100
Kann passieren, wenn man mit vielen Programmiersprachen parallel arbeitet :D
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Super läuft schon mal aber die Werte scheinen nicht zu passen.
Beim Luftdruck mus um 1000 rauskommen

Hier die Logdaten

Connected: CC2650 SensorTag
dr1 91770
dr2 167444
dr1 91115
dr2 167444
dr1 91770
dr2 167444
dr1 91770
dr2 167444
 

MaFu

Well-Known Member
Licensed User
Longtime User
Welche Werte stehen den in den Arrayfeldern?

Ev. muss die Reihenfolge umgedreht werden:
B4X:
dr1 = Bit.Or(Bit.Or(Bit.ShiftLeft(ArrayBytes(2), 16), Bit.ShiftLeft(ArrayBytes(1), 8)), ArrayBytes(0))
dr1 = Bit.And(0x00ffffff, dr1) / 100
dr2 = Bit.Or(Bit.Or(Bit.ShiftLeft(ArrayBytes(5), 16), Bit.ShiftLeft(ArrayBytes(4), 8)), ArrayBytes(3))
dr2 = Bit.And(0x00ffffff, dr2) / 100
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Hier die Array felder

Temperatur-Grad = 22.14 'Richtige Temperatur

ArrayBytesD(0) = -102
ArrayBytesD(1) = 8
ArrayBytesD(2) = 0
ArrayBytesD(3) = -122
ArrayBytesD(4) = -127
ArrayBytesD(5) = 1
Druck-Temperatur-------------- 167771
Luftdruck------------------------ 167770


Temperatur-Grad = 22.15 'Richtige Temperatur

ArrayBytesD(0) = -102
ArrayBytesD(1) = 8
ArrayBytesD(2) = 0
ArrayBytesD(3) = -121
ArrayBytesD(4) = -127
ArrayBytesD(5) = 1
Druck-Temperatur-------------- 167771
Luftdruck------------------------ 167770
 

MaFu

Well-Known Member
Licensed User
Longtime User
Wie ich vermutete, andersherum :D
Und dann noch ausmaskieren wegen der Minuswerte :eek:
B4X:
dr1 = Bit.Or(Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(2), 16), 0x00ff0000), Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00)), Bit.And(ArrayBytes(0), 0x000000ff))
dr2 = Bit.Or(Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(5), 16), 0x00ff0000), Bit.And(Bit.ShiftLeft(ArrayBytes(4), 8), 0x0000ff00)), Bit.And(ArrayBytes(3), 0x000000ff))
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Perfekt!!!
Du bis mein Held des Tages!
Vielen Dank nochmal ohne deine Hilfe hätte ich das nie rausbekommen.
 

Wolli013

Well-Known Member
Licensed User
Longtime User
Ich habe schon wieder ein bit Problem bei minus Werten und komme nicht weiter es in B4A zu überstezen.
Hier mal der Java Code
'-127 ... 127, signed Temperature (MSB is sign, next 7 bits are decimal value)

int temperatureSign = (rawData[4] >> 7) & 1;
int temperatureBase = (rawData[4] & 0x7F);
float temperatureFraction = ((float) rawData[5]) / 100f;
temperature = ((float) temperatureBase) + temperatureFraction;
if (temperatureSign == 1) {
temperature *= -1;
}

Vielleicht weis ja einer weiter und kann mir diesbezüglich helfen.
 
Last edited:
Top