Bonjour à tous

D'habitude je code en C++ pour envoyer des données sur un passerelle avec la technoligie LoRaWAN.
Pour une petite application indispensable, j'utilise un Raspbery avec Python3.

Le code est relativement simple

Code python3 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
    """
    Example for using the RFM9x Radio with Raspberry Pi and LoRaWAN
     
    Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
    Author: Brent Rubell for Adafruit Industries
    """
    import threading
    import time
    import subprocess
    import busio
    from digitalio import DigitalInOut, Direction, Pull
    import board
    # Import thte SSD1306 module.
    import adafruit_ssd1306
    # Import Adafruit TinyLoRa
    from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa
 
    # Button A
    btnA = DigitalInOut(board.D5)
    btnA.direction = Direction.INPUT
    btnA.pull = Pull.UP
 
    # Button B
    btnB = DigitalInOut(board.D6)
    btnB.direction = Direction.INPUT
    btnB.pull = Pull.UP
 
    # Button C
    btnC = DigitalInOut(board.D12)
    btnC.direction = Direction.INPUT
    btnC.pull = Pull.UP
 
    # Create the I2C interface.
    i2c = busio.I2C(board.SCL, board.SDA)
 
    # 128x32 OLED Display
    display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)
    # Clear the display.
    display.fill(0)
    display.show()
    width = display.width
    height = display.height
 
    # TinyLoRa Configuration
    spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
    cs = DigitalInOut(board.CE1)
    irq = DigitalInOut(board.D22)
    # TTN Device Address, 4 Bytes, MSB
    devaddr = bytearray([0x00, 0x00, 0x00, 0x00])
    # TTN Network Key, 16 Bytes, MSB
    nwkey = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
    # TTN Application Key, 16 Bytess, MSB
    app = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
    # Initialize ThingsNetwork configuration
    ttn_config = TTN(devaddr, nwkey, app, country='US')
    # Initialize lora object
    lora = TinyLoRa(spi, cs, irq, ttn_config)
    # 2b array to store sensor data
    data_pkt = bytearray(2)
    # time to delay periodic packet sends (in seconds)
    data_pkt_delay = 5.0
 
 
    def send_pi_data_periodic():
        threading.Timer(data_pkt_delay, send_pi_data_periodic).start()
        print("Sending periodic data...")
        send_pi_data(CPU)
        print('CPU:', CPU)
 
    def send_pi_data(data):
        # Encode float as int
        data = int(data * 100)
        # Encode payload as bytes
        data_pkt[0] = (data >> 8) & 0xff
        data_pkt[1] = data & 0xff
        # Send data packet
        lora.send_data(data_pkt, len(data_pkt), lora.frame_counter)
        lora.frame_counter += 1
        display.fill(0)
        display.text('Sent Data to TTN!', 15, 15, 1)
        print('Data sent!')
        display.show()
        time.sleep(0.5)
 
    periodic = True
    while periodic:
        packet = None
        # draw a box to clear the image
        display.fill(0)
        display.text('RasPi LoRaWAN', 35, 0, 1)
 
        # read the raspberry pi cpu load
        cmd = "top -bn1 | grep load | awk '{printf \"%.1f\", $(NF-2)}'"
        CPU = subprocess.check_output(cmd, shell = True )
        CPU = float(CPU)
 
 
        display.fill(0)
        display.text('* Periodic Mode *', 15, 0, 1)
        display.show()
        time.sleep(0.5)
        if periodic== True
            send_pi_data_periodic()
            periodic=False 
 
        display.show()
        time.sleep(.1)

Toutes les 5 mionutes, il envoye la valeur du CPU.

Je dois encoyer trois valeru sous cette forme

ba:3,la:46.2014,lo:4.01324
Donc au niveau de la function end_pi_data_periodic(), j'ai commencé à essayer de faire ceci

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
    def send_pi_data_periodic():
        threading.Timer(data_pkt_delay, send_pi_data_periodic).start()
        print("Sending periodic data...")
        send_pi_data("ba:3")
ce qui me génère une erreur
Sending periodic data...
Traceback (most recent call last):
File "radio_lorawan.py", line 175, in <module>
send_pi_data_periodic()
File "radio_lorawan.py", line 79, in send_pi_data_periodic
send_pi_data("ba:3")
File "radio_lorawan.py", line 84, in send_pi_data
data = int(data * 100)
ValueError: invalid literal for int() with base 10: 'ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:3ba:
^CException ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Sauf erreur, ca ddoit etre une erreur de conversion.

Etant donnée que mon code en C++ envoye les donnée en uint_8
En python3 comment pourrais convertir
ba:3,la:46.2014,lo:4.01324
en uint_8 (ou int si ca fait la meme chose), pour envoyer ces valeurs?


Milles mercis pour vos lumières