0

It's my first time working with SMBUS and I've read the docs of arduino and the Wire object. It states that I can send a string. To quote the docs: string: a string to send as a series of bytes. https://www.arduino.cc/reference/en/language/functions/communication/wire/write/

When I convert my int to a String type and pass it along to Wire.write, I can't read it with Python.

Slave address: 0x8

void setup() {
  Serial.begin(BaudRate); delay(10);
  Wire.begin(SLAVE_ADDRESS); 0x8
  Wire.onRequest(sendData); 
}

void loop() {
   sendData(1272); // right now hard-coded for testing purposes
}
// callback for sending data
void sendData(int data) {
    String dataString = String(data);
    int len = dataString.length() + 1;
    char charDataString[len];
    dataString.toCharArray(charDataString, len);
    for (int i = 0; i < len; i++) {
        Serial.println(charDataString[i]); -> SHOWS THE CORRECT VALUES
        Wire.write(charDataString[i]);
    }
    delay(1000);
}

My python code (how I read the data):


import smbus
import time
bus = smbus.SMBus(1)
address = 0x8
while True:
    data = ""
    for (i in range(0, 10))
        ##BOTH VARIANTS DO NOT WORK
        data = bus.read_i2c_block_data(address, 99, 3)
        data += chr(bus.read_byte(address));

    print(data)
    time.sleep(1)

I can't wrap my head around what I am doing wrong here.

6
  • I can't read it with Python is meanigless ... what is actually read by the python program?
    – jsotola
    Jun 14, 2022 at 21:56
  • Some random numeric values which make no sense to me. Jun 14, 2022 at 21:57
  • try converting the numbers to text using an online converter. Jun 14, 2022 at 22:04
  • @SteveRobillard on the raspberry pi (when I receive the data?) Jun 14, 2022 at 22:06
  • The Arduino sends it as bytes (as you state above) "string: a string to send as a series of bytes" so you need to convert the bytes back into a string. So if you send hello you might get "68 65 6c 6c 6f" as a hex representation or "104 101 108 108 111" as an ASCII representation. You can test this with an online converter type hex to text or ASCII to hex into google. Jun 14, 2022 at 22:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.