title: RaspberryPi to Arduino -send and receive string
body: <p>This is the code I am currently using to send and receive int values from a RaspberryPi to an Arduino using i2C. It works fine for values 0-255, but because of the 1 byte limit, anything larger fails.<br>
  To circumvent this, I'd like to send and receive string values instead, and then convert back to int if necessary.<br>
  What changes would I need to make in the following?<br>
  Here is my RPi Python code</p>
  
  <pre><code>import smbus
  import time
  # for RPI version 1, use "bus = smbus.SMBus(0)"
  bus = smbus.SMBus(1)
  
  # This is the address we setup in the Arduino Program
  address = 0x04
  
  def writeNumber(value):
      bus.write_byte(address, value)
      # bus.write_byte_data(address, 0, value)
      return -1
  
  def readNumber():
      number = bus.read_byte(address)
      # number = bus.read_byte_data(address, 1)
      return number
  
  while True:
      try:
          var = int(raw_input("Enter 1 - 9: "))
      except ValueError:
          print "Could you at least give me an actual number?"
          continue
  
      writeNumber(var)
      print "RPI: Hi Arduino, I sent you ", var
      # sleep one second
      #time.sleep(1)
  
      number = readNumber()
      print "Arduino: Hey RPI, I received a digit ", number
      print
  </code></pre>
  
  <p>And here is my Arduino code </p>
  
  <pre><code>#include &lt;Wire.h&gt;
  
  #define SLAVE_ADDRESS 0x04
  int number = 0;
  int state = 0;
  
  void setup() {
      pinMode(13, OUTPUT);
      Serial.begin(9600);         // start serial for output
      // initialize i2c as slave
      Wire.begin(SLAVE_ADDRESS);
  
      // define callbacks for i2c communication
      Wire.onReceive(receiveData);
      Wire.onRequest(sendData);
  
      Serial.println("Ready!");
  }
  
  void loop() {
      delay(100);
  }
  
  // callback for received data
  void receiveData(int byteCount){
  
      while(Wire.available()) {
          number = Wire.read();
          if (Wire.available() &gt; 1)  // at least 2 bytes
          {
            number = Wire.read() * 256 + Wire.read();
          }
          Serial.print("data received: ");
          Serial.println(number);
          //sendData();
          if (number == 1){
  
              if (state == 0){
                  digitalWrite(13, HIGH); // set the LED on
                  state = 1;
              }
              else{
                  digitalWrite(13, LOW); // set the LED off
                  state = 0;
              }
           }
       }
  }
  
  // callback for sending data
  void sendData(){
      Wire.write(number);
  }
  </code></pre>