Need help with reading from i2c bus

OP #7752291
Lesenswert?

Andreas B. schrieb:

https://letmegooglethat.com/?q=python+i2c

General you should learn the programming language before what you want to use. This forum is not a programming service.

This is my try but the result this wrong

1
#!/usr/bin/python3
2
import smbus
3
import requests
4
import time
5

6
bus = smbus.SMBus(1)
7
address = 0x0F
8
x = bus.read_i2c_block_data(address, 0xD0,3)
9
voltagein = x[2] <<8 | (x[1])
10
#print (x[1])
11
#print (x[2])
12

13
voltagein_string = "%i" % voltagein
14
print  (voltagein_string)
15
bus.close()
16
del x
17
del bus
OP #7752326
Lesenswert?

Matthias S. schrieb:

Martin L. schrieb:

voltagein = x[2] <<8 | (x[1])

Try:

1
> voltagein  = (x[0] * 256) + x[1]
2
>

This prints 2 times the same value

1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,2)
4
print ("x")
5
print (x)
6
#voltagein = (x[1]<<8) | (x[0])
7
voltagein  = (x[0] * 256) + x[1]
8
voltagein_string = "%i" % voltagein
9
print ("voltage in")
10
print (voltagein)
11
bus.close()
12
del x
13
del bus
14

15
time.sleep(2)
16
bus = smbus.SMBus(1)
17
address = 0x0F
18
x = bus.read_i2c_block_data(address, 0xD1,2)
19
print ("x")
20
print (x)
21
#powerextern = x[0] | (x[1]<<8)
22
powerextern = (x[0] * 256) + x[1]
23
pwerextern_string = "%i" % powerextern
24
print ("powerstring")
25
print (pwerextern_string)
26
bus.close()
27
del x
28
del bus
1
x
2
[208, 137]
3
voltage in
4
53385
5
x
6
[209, 132]
7
powerstring
8
53636
Beitrag #7752374 wurde von einem Moderator gelöscht.
OP #7752397
Lesenswert?

Martin L. schrieb:

Hmmm schrieb:

Martin L. schrieb:

x [208, 137]

Martin L. schrieb:

x [209, 132]

Seems like the first byte is always the command. What does your debug output look like after reading 3 bytes?

1
> [sudo] password for openhabian:
2
> x
3
> [208, 126, 20]
4
> voltage in
5
> 53374
6
> x
7
> [209, 159, 20]
8
> powerstring
9
> 53663
10
> 
11
>
1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,9)
4
print ("x")
5
print (x)
6
voltagein = (x[2]<<8) | (x[1])
7
#voltagein  = (x[1] * 256) + x[2]
8
voltagein_string = "%i" % voltagein
9
print ("voltage in")
10
print (voltagein)
11
bus.close()
12
del x
13
del bus
14

15
time.sleep(2)
16
bus = smbus.SMBus(1)
17
address = 0x0F
18
x = bus.read_i2c_block_data(address, 0xD1,9)
19
print ("x")
20
print (x)
21
powerextern = (x[3]<<8) | x[2]
22
#powerextern = (x[0] * 256) + x[1]
23
pwerextern_string = "%i" % powerextern
24
print ("powerstring")
25
print (pwerextern_string)
26
bus.close()
27
del x
28
del bus
1
x
2
[208, 110, 20, 1, 105, 20, 0, 0, 0]
3
voltage in
4
5230
5
x
6
[209, 143, 20, 1, 105, 20, 0, 0, 0]
7
powerstring
8
276
OP #7752398
Lesenswert?

Hmmm schrieb:

Martin L. schrieb:

[208, 126, 20]

powerextern = (x[2] * 256) + x[1]

Das sollte rauskommen

1
********************************
2
*                              *
3
* S.USV solutions              *
4
* www.s-usv.com                *
5
*                              *
6
* Model: Advanced              *
7
* Firmware Version: 2.61       *
8
* Software Version: 2.40       *
9
* Hardware Version: 2.2        *
10
*                              *
11
* Mail notification: Disabled  *
12
*                              *
13
* Timed Boot: Disabled         *
14
* Boot time: 00:00:00          *
15
*                              *
16
* Timed Shutdown: Disabled     *
17
* Shutdown time: 00:00:00      *
18
*                              *
19
* Fri Oct 11 18:18:54 2024     *
20
*                              *
21
********************************
22
*                              *
23
* Powering Source: Primary     *
24
* Charging circuit: OFFLINE    *
25
* Charging current: 300 mA     *
26
*                              *
27
* Voltage in: 5.23 V           *
28
* Battery capacity: 100.00%    *
29
* Battery voltage: 4.20V       *
30
* Power Battery: 000.00 mA     *
31
* Power Extern: 1259.00 mA      *
32
*                              *
33
* Shutdown timer: -1           *
34
* Autostart:  enabled          *
35
* Sleep timer: 1               *
36
*                              *
37
********************************
OP #7752408
Lesenswert?

Hmmm schrieb:

Martin L. schrieb:

voltage in 5230

5230 mV = 5.230 V

The device seems to send the command byte followed by the low byte and the high byte of the value, so just use (x[2] * 256) + x[1] in both cases.

there is no difference between the commands

1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,3)
4
print ("x")
5
print (x)x
6
[208, 132, 20]
7
voltage in
8
5252
9
x
10
[209, 126, 20]
11
powerstring
12
5246
13

14
voltagein = (x[2]<<8) | (x[1])
15
#voltagein  = (x[1] * 256) + x[2]
16
voltagein_string = "%i" % voltagein
17
print ("voltage in")
18
print (voltagein)
19
bus.close()
20
del x
21
del bus
22

23
time.sleep(2)
24
bus = smbus.SMBus(1)
25
address = 0x0F
26
x = bus.read_i2c_block_data(address, 0xD1,3)
27
print ("x")
28
print (x)
29
#powerextern = (x[3]<<8) | x[2]
30
powerextern = (x[2] * 256) + x[1]
31
#powerextern = (x[0] * 256) + x[1]
32
pwerextern_string = "%i" % powerextern
33
print ("powerstring")
34
print (pwerextern_string)
35
bus.close()
36
del x
37
del bus
1
x
2
[208, 132, 20]
3
voltage in
4
5252
5
x
6
[209, 126, 20]
7
powerstring
8
5246

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren