I2C working off of IDE but not IoT Cloud

Hi everyone,

I have 2 probes from Atlas Scientific that I'm trying to get to work on IoT Cloud platform. One probe is for Temperature and Humidity, the other for CO2 reading. I've written the code that gets me all three readings through Serial communication no problem.

To get a reading on those probes, you send the char "r" as a command via wire.Write, and the probe will first respond with a response code followed by the reading. A response code of 1 is successful request (which I get on the IDE), 2 is syntax error, 255 is no data to send, and 254 is still processing, not ready.

Again this works perfectly with both probes together on the IDE, but on the the cloud I no longer get a proper response code. Instead of the 1, I get 190. I also frequently get a second junk byte value of 124, instead of the first digit of my humidity reading.

Here is the code with all the serial prints for trouble shooting. Below the code I've pasted the serial output.

#include "thingProperties.h"
#include <Wire.h>
#define humaddress 111
#define co2address 105
byte code = 0;                                  // to hold I2C response code.
byte in_char=0;                                 // to store inbound bytes
byte i =0;                                      //couter for storing inbound characters into hum and temp data
char humData[20];                               //we make a 20-byte character array to hold incoming humidity data from the sensor.
char co2Data[20];
int timeDelay = 1500;                                // delay time for sensors
char *hum;                                      // character pointer for parsing
char * tem;                                     // character pointer for parsing


void setup() {
  
  Serial.begin(9600);           // Initialize serial and wait for port to open:
  delay(300);                  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found

  initProperties();             // Defined in thingProperties.h

  
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);          // Connect to Arduino IoT Cloud
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  Serial.println("");
  Wire.beginTransmission(humaddress);                     //simple communication to Humidity sensor
  Wire.write("r");
  delay(300);
  Wire.endTransmission(humaddress);
  
  delay(timeDelay);
 
  Wire.requestFrom(humaddress, 22, 1);                              // Request 20 characters from I2C
  delay(900);
  Serial.print("Humidity code is: ");
  code = Wire.read();                                         // Read the first acknoladgment byte from the sensor
  delay(300);
  Serial.println(code);   
  
  while (Wire.available()){                                 // read bytes from sensor into in_char, store them int humData until null terminator
    in_char = Wire.read();
    Serial.println(in_char);
    delay(300);
    humData[i] = in_char;
    i +=1;
    
    if (in_char ==0){
      I=0;
      break;
    }
  }
  hum = strtok(humData, ",");
  tem = strtok(NULL, ",");
  
  temperature = atof(tem);
  humidity = atof(hum);
  
  Serial.print("Hum: ");
  Serial.println(humidity);

  Serial.print("Temp: ");
  Serial.println(temperature);
}


/*
  Since Hum is READ_WRITE variable, onHumChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onHumChange()  {
  // Add your code here to act upon Hum change
}

/*
  Since Temp is READ_WRITE variable, onTempChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTempChange()  {
  // Add your code here to act upon Temp change
}

Serial Output with timestamp:
17:57:25.637 -> Humidity code is: 190
17:57:25.934 -> 51
17:57:26.244 -> 53
17:57:26.550 -> 46
17:57:26.834 -> 52
17:57:27.149 -> 51
17:57:27.424 -> 44
17:57:27.751 -> 49
17:57:28.032 -> 57
17:57:28.346 -> 46
17:57:28.624 -> 54
17:57:28.933 -> 54
17:57:29.247 -> 0
17:57:29.549 -> Hum: 35.43
17:57:29.549 -> Temp: 19.66
17:57:29.585 ->
17:57:32.292 -> Humidity code is: 190
17:57:32.571 -> 124
17:57:32.887 -> 51
17:57:33.161 -> 53
17:57:33.472 -> 46
17:57:33.789 -> 52
17:57:34.061 -> 52
17:57:34.362 -> 44
17:57:34.680 -> 49
17:57:34.967 -> 57
17:57:35.284 -> 46
17:57:35.559 -> 54
17:57:35.866 -> 55
17:57:36.169 -> 0
17:57:36.481 -> Hum: 0.00
17:57:36.481 -> Temp: 19.67

Forgot to mention I’m using MKR wifi1010.

Hello @samers

i2c is also used by IoT Cloud to leverage the secure element on your board.

You can use it, but need to fix your code, please check I2C Usage

Thank you sir, and sorry for the late reply, I had given up and resorted to another solution that required buying additional Arduinos. The plan was to use a second Arduino as a "middle man" that would handle all the measurements from I2C connected devices, and transmitted to the IoT Cloud connected Arduino.

I'll look into your suggestion first however.