[SOLVED] Wire communiction Arduino Rp2040 connect to Arduino mega (JSON)

Im trying to send data between my rp2040 connect using the "Wire" library. I managed to create a code that will send data efficiently, and it did work. My Code that works:

RP2040 code:

#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(8); // address of the Mega
  Wire.write("Hello from RP2040!"); // send the data
  Wire.endTransmission(); // end the transmission
  delay(1000);
}

Arduino Mega Code:

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin(8); // address of the Mega
  Wire.onReceive(receiveEvent); // set the receive event
}

void loop() {
  delay(1000);
}

void receiveEvent(int numBytes) {
  while (Wire.available()) {
    char data = Wire.read(); // read the incoming data
    Serial.write(data); // print the data to the Serial Monitor
  }
}

This code works great, but I want to send over JSON data. So I modified the code for it to get JSON data:
Rp2040 code:

#include <Wire.h>
#include <ArduinoJson.h>

// Define I2C slave address
const uint8_t SLAVE_ADDRESS = 8;

void setup() {
  Serial.begin(9600);
  Wire.begin();   // Start I2C communication as Master
}

void loop() {
  // Create JSON document
  StaticJsonDocument<200> doc;

  // Set sensor type and value
  doc["sensor"] = "Temperature";
  doc["value"] = 25.0;

  // Serialize JSON document
  String jsonString;
  serializeJson(doc, jsonString);

  // Send JSON data over I2C
  Wire.beginTransmission(SLAVE_ADDRESS);
  Wire.write(jsonString.c_str(), jsonString.length());
  Wire.endTransmission();

  delay(1000);
}

Arduino Mega code:

#include <ArduinoJson.h>
#include <Wire.h>

void receiveEvent(int numBytes);

void setup() {
  Serial.begin(9600);
  Wire.begin(8); // address of the Mega
  Wire.onReceive(receiveEvent); // set the receive event
}

void loop() {
  delay(1000);
}

void receiveEvent(int numBytes) {
  StaticJsonDocument<200> doc;

  while (Wire.available()) {
    char data = Wire.read(); // read the incoming data
    Serial.write(data);
    doc["json"] = String(doc["json"].as<String>() + String(data));
  }

  // Deserialize the JSON document
  String jsonString = doc["json"].as<String>();

  DeserializationError error = deserializeJson(doc, jsonString);

  if (error) {
    Serial.print("Error: ");
    Serial.println(error.c_str());
  } else {
    // Print the sensor type and value to the Serial Monitor
    String sensorType = doc["sensor"].as<String>();
    float sensorValue = doc["value"].as<float>();

    Serial.print("Sensor Type: ");
    Serial.print(sensorType);
    Serial.print(", Value: ");
    Serial.println(sensorValue);
  }
}

But I have a problem The Arduino doesn't receive any information from the RP2040 code. I know the mega code works because I tried only to change this from:

Wire.beginTransmission(8);
    Wire.write(jsonString.c_str(), jsonString.length());

  Wire.endTransmission();

To this:

Wire.beginTransmission(8);
  Wire.write("Hello, Mega!");
  Wire.endTransmission();

And the Arduino did get the data. So there is a problem when using this code: Wire.write(jsonString.c_str(), jsonString.length());

Any way I can send maybe JSON differently or have a better way or a way to fix my code?
Thanks

What does...

Serial.println(jsonString.c_str());
Serial.println(jsonString.length());

... produce?

This produce:

{"sensor":"Temperature","value":25}
35

What happens if you replace...

With...

Wire.write("{\"sensor\":\"Temperature\",\"value\":25}", 35);

Ok the info is now sent with that code Wire.write(jsonString.c_str(), jsonString.length()). but the code do get the info from the json doesnt work

Edit: The limit is 32 bytes so i need to see how i can send data with two lines or more with this type: {"sensor":"Temp","value":25} and also a way to decode it

Solved the data is being sent ok, make sure to not use over 32 bytes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.