Giter Club home page Giter Club logo

arduino-i2c-json-raspberry's Introduction

Arduino I2C Raspberry Pi

Communication example between arduino and RPi with JSON, avoiding the 32 byte limitation of I2C. RPi acts as master, arduino as slave, and the communication is made through several request:

  1. RPi asks for data length to arduino.
  2. Arduino answers with the JSON length.
  3. RPi gets the data in 32 byte requests.

RPi script is written in python, and uses the smbus2 library to abstract from communication. Arduino sketch is based on this fantastic post. Software dependencies:

Arduino sketch:

Raspberry pi script:

I2C

Use i2cdetect -y 1 to search the adress device of your arduino. Maybe you will have to enable before the I2C kernel module with raspi-config.

Code example

Arduino

void receiveEvent(int nBytes) {
    while (Wire.available()) {
        request = (char) Wire.read();
    }
}

void requestEvent() {

    if (request == ASK_FOR_LENGTH) {
        Wire.write(json.length());
        requestIndex = 0;
    }

    if (request == ASK_FOR_DATA) {
        if (requestIndex < (json.length() / 32)) {
            Wire.write(json.c_str() + requestIndex * I2C_LENGTH_LIMIT, I2C_LENGTH_LIMIT);
            requestIndex++;
        }
        else {
            Wire.write(json.c_str() + requestIndex * I2C_LENGTH_LIMIT, json.length() % I2C_LENGTH_LIMIT);
            requestIndex = 0;
        }
    }
}

Raspberry Pi

with SMBus(1) as bus:

    time.sleep(SLEEP_TIME)

    # Ask for length response
    write = i2c_msg.write(I2C_SLAVE_ADDR, [ASK_FOR_LENGTH])
    bus.i2c_rdwr(write)

    time.sleep(SLEEP_TIME)

    # Answer
    read = i2c_msg.read(I2C_SLAVE_ADDR, 1)
    bus.i2c_rdwr(read)
    responseLength = list(read)[0]

    time.sleep(SLEEP_TIME)

    # Ask for data reponse
    write = i2c_msg.write(I2C_SLAVE_ADDR, [ASK_FOR_DATA])
    bus.i2c_rdwr(write)

    time.sleep(SLEEP_TIME)

    response = str()

    # Answer: Iterate over I2C_LENGTH_LIMIT bytes blocks, plus last [0,I2C_LENGTH_LIMIT] block
    for responseIndex in range(0, (responseLength // I2C_LENGTH_LIMIT) + 1, 1):
        read = i2c_msg.read( \
            I2C_SLAVE_ADDR, \
            I2C_LENGTH_LIMIT if (responseIndex != (responseLength // I2C_LENGTH_LIMIT)) else (responseLength % I2C_LENGTH_LIMIT))
        bus.i2c_rdwr(read)
        response +=  "".join([chr(i) for i in list(read)])

    parsedJson = json.loads(response)
    print("Length: {0}".format(responseLength))
    print("Response: {0}".format(parsedJson))
    print(json.dumps(parsedJson, indent = 4))

Wiring diagram

Wiring diagram

arduino-i2c-json-raspberry's People

Contributors

edumardo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.