Serial and I2C Bug

Hi Guys,

I'm using a Mega ADK for this project if that means anything...

The project involving GPS via Serial3 port and a RTC which I's accessing the temperature sensor via I2C. The code works fine if I comment out the GPS or temperature sensor but when I combine the two I get rubbish temperature data.

Sometimes it give a true reading and other times just goes up to a values of 300+ deg C! I noticed I get rubbish temperature data whenever the GPS is transmitting data which is about 1 Hz. I know this because the temperature data is OK when the GPS TX LED is off.

Does anyone have any ideas on how I can stop this from happening?

I've been toying with the idea of using Serial.begin() and Serial.end() before and after I ask for the GPS data but don't know if it will work for sure, and was wondering is anyone has any better ideas because I want to step the GPS data collection 4 Hz, and was think using those function would slow things down a bit.

I can't post the code because it is over 9500 characters long and this site doesn't support anything over that.

adamatcooranbong:
I can't post the code because it is over 9500 characters long and this site doesn't support anything over that.

http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_attachments

Code attached...

XivelyTemperatureSensorWithGPS.ino (8.01 KB)

String jsonBodyData()
{
  char decimalToStringBuffer[20];
  String body;
  body += "{\n";
  body += "\"version\":\"1.0.0\",\n";
  body += "\"datastreams\" : [ {\n";
  body += "\"id\" : \"";
  body += "Temperature";
  body += "\",\n";
  body += "\"current_value\" : \"";
...

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software


Also see: http://www.gammon.com.au/concat


  Wire1.beginTransmission(DS3232_I2C_ADDRESS);
  Wire1.write(0x11); // set DS3232 register pointer to 00h
  Wire1.endTransmission();

Where is Wire1 declared?

Hi Nick,

I agree with your comment regarding Strings however; it works for now but was planning on changing to char's after I get it all going and then I'll start refining to code. I must say I love your website - its a big help!

I use IDE's 1.0.5 mostly and 1.5.2 Beta for my DUE board work so as far as I know all the String bugs have been ironed out.

Yeah, you got me with Wire1 part... I accidentally posted my DUE version of the code as that board has two I2C buses and that's how you access it - similar API for accessing the the other hardware serial buses on the MEGA e.g. Serial1, Serial2... Wire1 is declared in the Arduino Wire/I2C default library.

Mind you I can't get any I2C device to communicated on the DUE. Have you done any work with this board?

No I haven't, yet.

 Wire1.requestFrom(DS3232_I2C_ADDRESS, 8); // request 7 bytes of data from DS3232 starting from register 00h
  MSB=Wire1.read();
  LSB=Wire1.read() >> 6;
  // send it to the serial monitor
  degreesC = MSB + 0.25 * LSB;
  return degreesC;

You are requesting 8 bytes (despite the comment) and reading 2. Not sure how well this will work. You should also check the returned number, eg.

 if ( Wire.requestFrom(DS3232_I2C_ADDRESS, 8) != 8)
    {
   // error
    }
  else
   {
   // read them
   }

Do you have pull-up resistors on the I2C lines?

Yeah, good find...

Oversight on my behalf... You can look at your own code all day and never find anything wrong with it until somebody points out the obvious.

It doesn't change the values I get though when I change the number of byte to read to 2. The temperature data I get from the I2C temperature sensor is good but only have issues when I hook up the GPS to the Serial. If I unwire either the GPS or RTC the code works fine, albeit missing either GPS or temperature data.

For your information I'm using RTC module from Freetronics to pull the temperature data out. It has inbuilt pull-ups - if you bridge the tabs with a bit of solder - which i have done.

I'll give your suggestion a go.