简体   繁体   中英

Parsing data from I2C

I'm sending data from my Raspberry Pi to an Arduino using I2C. I'm using the wire library and based my code from the slavereceiver and slavesender examples. I'm trying to parse the following data coming in from the Raspberry Pi:

13:0&8:0

So, 13 would be my pin and 0 would be the intended value. The next set of data is pin 8, with an intended value of 0 as well.

I can't seem to get anything to work that I've tried, so for now I'm just printing the data to the serial monitor. Here is my code:

#include <Wire.h>
int motion = 6;
int relay1 = 8;
int relay2 = 9;
int onBoardLED = 13;

void setup()
{
  Wire.begin(3);                
  Wire.onRequest(requestEvent);
  Wire.onReceive(receiveEvent);
  pinMode(motion, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(onBoardLED, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
  digitalWrite(onBoardLED, HIGH);
}

void receiveEvent(int howMany)
{
    while (Wire.available()){
        char json = Wire.read();
        Serial.print(json);
    }
}
void requestEvent()
{
    char myStuff[80];
    int motionState = digitalRead(motion);
    sprintf(myStuff, "{\"motion\": %i}", motionState);
    Wire.write(myStuff); 
}

The serial monitor shows the data, but I can't for the life of me parse it to get my values and my pins.

Any help would be great appreciated!

Update : So, here is my new code based on paulsm4 suggestions, but when I try to print the length of the inData, it comes back as 0:

void receiveEvent(int howMany)
{
    char inData[80];
    byte index = 0;
    while (Wire.available()){
      char aChar = Wire.read();
      if(aChar == '\n')
      {
         // End of record detected. Time to parse
         index = 0;
         inData[index] = NULL;
      }
      else
      {
         inData[index] = aChar;
         index++;
         inData[index] = '\0'; // Keep the string NULL terminated
      }
    }
    Serial.println(strlen(inData));
    char* command = strtok(inData, "&");
    while (command != 0) {
        // Split the command in two values
        char* separator = strchr(command, ':');
        if (separator != 0) {
            // Actually split the string in 2: replace ':' with 0
            *separator = 0;
            int servoId = atoi(command);
            ++separator;
            int position = atoi(separator);

            // Do something with servoId and position
            Serial.print(servoId);
        }
        // Find the next command in input string
        command = strtok(NULL, "&");
    }
}

Here is a good example of the kind of thing you need to "parse" your input (different I/O device than I2C; sample principle):

http://forum.arduino.cc/index.php?topic=48925.0

char inData[80];
byte index = 0;

void loop() {
   while(Serial.available() > 0) {
      char aChar = Serial.read();
      if(aChar == '\n') {
         // End of record detected. Parse this line
         ...
         // When you're done parsing, clear the array and reset the index    
         inData[index] = NULL;
         index = 0;
      }
      else  {
         inData[index] = aChar;
         index++;
         inData[index] = '\0'; // Keep the string NULL terminated
      }
   }
}

One option for "parsing" might be to use strtok() . Here is a good example:

https://arduino.stackexchange.com/questions/1013/how-do-i-split-an-incoming-string

char* command = strtok(inData, "&");
while (command != 0) {
    // Split the command in two values
    char* separator = strchr(command, ':');
    if (separator != 0) {
        // Actually split the string in 2: replace ':' with 0
        *separator = 0;
        int servoId = atoi(command);
        ++separator;
        int position = atoi(separator);

        // Do something with servoId and position
    }
    // Find the next command in input string
    command = strtok(NULL, "&");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM