Why client.connect or client.write wont work after a particular function

Hi, I am dong an application on arduino with C. It allows I2C grab four sensors temperature data, which i called scan_temp as function name. After scanning, Ethernet shield will begin, send, and connect to server ip to send those data. But it wont work. I dont know the reason. i tried to move Ethernet.begin(), client.connect() and connect.write() before scanning sensors, it works. As long as i put it after scaning, it weirdly hangs there. Does anyone have that problem before? Please let me know. Thanks in advance.

Post your sketch and links to all libraries used unless they are distributed with the Arduino IDE. Also provide links to the datasheets of the used sensors and a wiring diagram.

It’s not a general problem because I have several application that does Ethernet connections after reading I2C sensors, and they work reliably. So the error is in your code (or your hardware).

Connection diagram of sensors (plz see link):

The library i added is from the link below:
Do you know if there is a time out issue between function of scan_Sensor() and send-Data()?
Here is my code:

#include <Arduino.h>
#include <SoftI2CMaster.h>
#include <SPI.h>
#include <Ethernet.h>

#define DEVICE1 0x55<<1
#define DEVICE2 0x50<<1
//#define DEVICE3 0x48<<1
//#define DEVICE4 0x46<<1
#define DEVICE5 0x42<<1 //device 5
#define DEVICE6 0x40<<1 //device 6
//#define DEVICE7 0x44<<1
//#define DEVICE8 0x38<<1

#define BAD_T 32767
#define SAMPLES 22 //# of samples for each sensor
#define DEV_NBR 2
#define SLICE_MAX_NBR 16
#define SERVER_PORT 4242

const float temp_Factor=0.02;
const float temp_Error=0.01;
const float temp_K = 273.15;

float obj_Temp[SAMPLES*DEV_NBR]; //temperature data
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac = { 0x90, 0xA2, 0xDA, 0x0F, 0x3F, 0x6D };
IPAddress ip(192, 168, 0, 7);
//byte rasp_server_ip = {192, 168, 1, 42};
//Test ip on router
byte rasp_server_ip = {192, 168, 0, 6};

// Initialize the Ethernet client library
// with the IP address and port of the server
EthernetClient client;

void setup(){
int objTemp[DEV_NBR];
boolean start_init_bit = true;
while ( start_init_bit == false);
Init_dev(); //initial I2C and begin ethernet
//get Status n
boolean start_pi_bit = true;
//Sensor scan Starts;

if(start_pi_bit == true)
scan_Sensor(objTemp); //sensor start scanning
sendData(); // send data to pi through ethernet
Serial.println(“Error of scanning sensors.”);


void Init_dev()
Serial.println(“Initializing I2C.”);

//read temperature from sensor register
int read_Temp(int dev){
int data_low = 0;
int data_high = 0;
int pec = 0;

// read
data_low = i2c_read(false); //Read 1 byte and then send ack
data_high = i2c_read(false); //Read 1 byte and then send ack
pec = i2c_read(true);
//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
//double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point

// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (((data_high & 0x007F) << 8) + data_low);

return tempData;
//scan multiple sensors and store data into defined location
void scan_Sensor(int *objT)
int sample_counter =0;
boolean Error = false;

for (sample_counter =0; sample_counter <SAMPLES; sample_counter++)
objT[0] = read_Temp(DEVICE5);
objT[1] = read_Temp(DEVICE6);
// objT[2] = read_Temp(DEVICE5);
// objT[3] = read_Temp(DEVICE6);

// delay(950);

// while(Error==true)
// {
// Serial.println(“Halt”);
// obj_Temp[sample_counter+44]=objT[2]*temp_Factor-temp_Error-temp_K;

boolean Error_sensor_check(int objT)
if ( (objT[0] == BAD_T) || (objT[1] == BAD_T)|| (objT[2] == BAD_T)||(objT[3] == BAD_T))
Serial.print(“Bad sensor”);
return true;
//else if //check accurate threshold
return false;
void sendData()
// Ethernet.begin(mac,ip);
// Serial.println(“Initializing Ethernet.”);
if (client.connect(rasp_server_ip, SERVER_PORT)) {
} else {
Serial.println(“connection failed”);
void display_value()
for(int sensor_nbr=0; sensor_nbr<4; sensor_nbr++)
for(int j=0; j<SAMPLES; j++)
Serial.print(" “);
void loop(){


I realized that it worked if i only read and store two sensors. As long as i read four of them together, Ethernet wouldn't work. I am really new with programming, but i think it might has something with memory over write? Could someone give me suggestion? Thanks.

Are you changing DEV_NBR to 4 when you read 4 sensors?

#define DEV_NBR 2

    // objTemp is an array of 2
    int objTemp[DEV_NBR];   

        // You are calling this with a pointer to an array of 2
        scan_Sensor(objTemp); //sensor start scanning

void scan_Sensor(int *objT)
     // down a few lines, you are assigning like it is an array of 4
     objT[0] = read_Temp(DEVICE5);
     objT[1] = read_Temp(DEVICE6);
    // objT[2] = read_Temp(DEVICE5);
   //  objT[3] = read_Temp(DEVICE6);