how to write data to SD and upload to database from wifi shield

HI.
I'm now using uno and wifi shield do a project which need to save data in SD and upload to database. However, it has a problem that it seems that these two function can not do at the same time. I separate these two function and run, they totally OK with its own function. When i combine them together, the SD card can not be opened and written. I finally found that when i used WifiClient in program, the SD card can not be write. It is too weird.

How could i solve this problem? This problem really confused me.

Thank you.

However, it has a problem that it seems that these two function can not do at the same time.

There is no physical reason for them not to work together. It then comes down to your code.

How could i solve this problem?

I believe that there is a hint above. If you need more than a subtle hint, POST YOUR CODE!

Does you SD card and Wi-Fi card use any common signal pins, like pins 13, 12, 11 & 10.
If so you need to have a separate enable pin for one of them.

Post a link to the two cards you are using.
As well as posting your code.

HI.

i define the SS pin which these two function might be used.

This is my code.

#include <Wire.h>
#include "SHT1x.h"
#include "GY30.h"
#include <SD.h>
#include <WiFi.h>

WiFiClient client;
char servername[] = "myserver";          // name address for Google (using DNS)

int  Status = WL_IDLE_STATUS;                // the Wifi radio's status
char FBssid[] = "SSID";                     // SSID of this network
char FBpass[] = "PASS";                 // passwordof this WPA network

int CS_pin = 4;

#define dataPin  5
#define clockPin 6

float temp_c, temp_f, humidity;
int lux;
long number= 1;              //Store the id# of our reading
String dataString;
char temper[10];
char humi[10];

void setup()
{
   Serial.begin(9600);
   pinMode(10, OUTPUT);
   SDsetup();
   ConnectToAP();
}

void loop()
{
  SensingData();
  delay(500);
  SDcardInit();
  sendDB ();
}

void ConnectToAP(){   
  Serial.println("Attempting to connect to network...");
  Status = WiFi.begin(SSID, PASS);
  
  if ( Status != WL_CONNECTED) { 
      Serial.println("Couldn't get a wifi connection");
      while(true);
  } 
  
  else {        
      printWifiStatus();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void SensingData(){
  SHT1x sht1x(dataPin, clockPin);
  GY30 gy(0x23);
  temp_c = sht1x.readTemperatureC();
  humidity = sht1x.readHumidity();
  lux = gy.readLux();
  Serial.print("Temperature: ");
  Serial.print(temp_c, 1);
  Serial.println("C");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  Serial.print("Illumination: ");
  Serial.print(lux);
  Serial.println(" lux");
}

void sendDB (){     
    if (client.connect(servername, 80)) 
    {     
       
       Serial.println("connected to database!");  
       client.print("GET /city/wang.php?data=");
       client.print(number);
       client.print("%2C");
       client.print(temp_c, 1);
       client.print("%2C");
       client.print(humidity, 1);
       client.print("%2C");
       client.print(lux);
       client.println("&Submit_Bottom=submit HTTP/1.1");
       client.println("Host: 140.112.94.126");
       client.println();
       client.println("Connection: close");
       client.println();
       Serial.println("Finish to database!");
       delay(5000);   
    } 
    else
    {
       Serial.println("connection failed");
       delay(3000);
    }
    client.stop();
}

void SDsetup(){
  if(!SD.begin(CS_pin))
  {
    Serial.println("Card Failed");
    return;
  }
  Serial.println("Card Ready");
  
  File logFile = SD.open("LOGG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , ,"); //Just a leading blank line, incase there was previous data
    logFile.println("number, Temp_C, Hum, Illu");
    logFile.close();
  }
  else
  {
    Serial.println("Couldn't open log file");
    return;
  }
}

void SDcardInit(){  
  //Open a file to write to
  //Only one file can be open at a time
  String tem = dtostrf(temp_c, 3, 1, temper);
  String temperature = (tem);
  String hum = dtostrf(humidity, 3, 1, humi);
  String humidity = (hum);
  String dataString = String(number) + ", " + temperature + ", " + humidity + ", " + String(lux);
  
  File logFile = SD.open("LOGG.csv", FILE_WRITE);
  if(logFile)
  {
    logFile.println(dataString);
    logFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't access file");
  }
    Serial.println("Finish SaveToSDcard");
}

Grumpy_Mike:
Does you SD card and Wi-Fi card use any common signal pins, like pins 13, 12, 11 & 10.
If so you need to have a separate enable pin for one of them.

Post a link to the two cards you are using.
As well as posting your code.

I can't see where you define or indeed use a separate enable pin for each device if they are on the same SPI bus.

SO, i add the pinMode(10, OUTPUT) is wrong way, isn't it? If it is not correct, how could improve my code to attach these two function can work normally?

Thanks.

You may not have enough RAM. You have lots of strings.

You should check memory use. Here is a link Arduino Playground - HomePage.

For *** sake

Grumpy_Mike:
Post a link to the two cards you are using.