Ethernet+Sd card conflit? unstable data logging

Hi.

I have an arduino mega with ethernet shield, sd card, tft 3.2" screen and some sensors.

I’m using this code to test the php code i wrote to store data into a website database and log data to a txt file on the sd card at the same time.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

// Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192, 168, 0, 177);

int photocellPin = 0;  // Analog input pin on Arduino we connected the SIG pin from sensor
int photocellReading;  // Here we will place our reading
File myFile;
char server[] = "my web site here"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")

// Initialize the Ethernet server library
EthernetClient client;

void setup() {

  // Serial.begin starts the serial connection between computer and Arduino
  Serial.begin(9600);
  pinMode(53, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(4, HIGH);

  Serial.print(F("Starting SD..."));
  if (!SD.begin(4)) {
    Serial.println(F("failed"));
  }
  else {
    Serial.println(F("ok"));
  }

  // start the Ethernet connection
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    //  client.stop();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {

  photocellReading = analogRead(photocellPin); // Fill the sensorReading with the information from sensor
  // Connect to the server (your computer or web page)
  if (client.connect(server, 80)) {

    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: my web site here"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server
    Serial.println("posted something");
    Serial.println(photocellReading);
  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }


  myFile = SD.open("garbage.txt", FILE_WRITE);
  if (myFile) {
    // TFTPrint( "file created", 30, 300, 1, 2, TFT_BLACK, TFT_WHITE);
    myFile.println(millis());
    myFile.close();
    // if the file is available, write to it:
  }
  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
}

The problem is:

  • When I eliminate the sd card logging part from the program and the sd card from the shield, i get the connexion and the data is logged to database every 10s (which is the intervalle i set).
  • If i upload the code with sd part, sometimes i don’t get a connexion to the website (connexion failed), sometimes i get connexion (if only i reupload the code) but the data is not always stored on the database (despite getting that the data was sent to the database on serial monitor).

So, in addition of what i mentioned, i want to know how to properly use digitalWrite(4,LOW/HIGH) and digitalWrite(10,LOW/HIGH), should i use them whenever i need to use the sd card or ethernet (so digitalWrite(4,LOW);digitalWrite(10,HIGH); to write to sd card and digitalWrite(4,HIGH);digitalWrite(10,LOW); to use ethernet) or i need just to do so when initializing the sd card and the ethernet wiznet 5100? could it be a power issue (using my pc usb for now)

Thanks.

You aren't reading the response from the server.

  if (client.connect(server, 80) == 1) {

    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading);
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: my web site here");
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line

// this is new. Read from server until the server closes the connection.
    while(client.connected())
    {
       while(client.available())
       {
           Serial.write(client.read());
       }
    }
    
    client.stop();    // Closing connection to server
    Serial.println("posted something");
    Serial.println(photocellReading);
  }

SurferTim:
You aren't reading the response from the server.

Hi.

Thanks for your replay.

I tried that before (and retried it now) but i don't get any response from the server

"Starting SD...ok
connecting...
connected
posted something
64
posted something
63
posted something
64
posted something
64"

but i get some of the data stored on the database

Did you change the evaluation of the connect return value? Only '1' is success.

  if (client.connect(server, 80) == 1) {

SurferTim:
Did you change the evaluation of the connect return value? Only '1' is success.

  if (client.connect(server, 80) == 1) {

I tried this:

Serial.print(client.connect(server, 80));

I get -5

A negative return value is a DNS resolution fail of the domain name of the server..

SurferTim:
A negative return value is a DNS resolution fail of the domain name of the server..

Thanks for your answer.

I do not really understand what that does mean (i will google it soon :smiley: ) but isn't that strange that i get some of the data stored on the database?

If you use a domain name rather than an IP address for the server in your connect attempt, a negative return value indicates your DNS server was unable to resolve the domain name to an IP address.

Thanks for your clarification.

So should i use an IP adress to solve any further possible problem?

fjtheknight:
Thanks for your clarification.

So should i use an IP adress to solve any further possible problem?

Yes. It is also much faster establishing the connection with an IP address.

Hi again.

I tried to make a new website (same provider) with a new database.

I'm now getting a response from the server (earlier even if i try to connect to google i get connected but no response):

Starting SD...ok
connecting...
connected
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:07:03 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
45
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:07:17 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
44
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:07:32 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
45
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:07:47 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
44
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:08:03 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
45
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:08:16 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
43
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:08:32 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
45
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:08:46 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
44
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:09:00 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
46
0
HTTP/1.1 200 OK
Date: Mon, 02 Jan 2017 15:09:13 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff

posted something
44

When i accessed the old database, i saw a warning about php max input var, could that be a part of the problem?

Those are responses from the server. It is only the header tho. The Content-Length is showing '0'. It is probably your php code on the server.

Thanks again.

This is my php code:

<?php

 $username = "***********"; 
    $password = "************";  
    $servername = "************";
$database = "************";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn,$database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
    $sqli = "INSERT INTO sensor (value)  VALUES  ('".$_GET["value"]."')"   ; 

mysqli_query($conn,$sqli) or die ('Error SQL ! '.$sqli.'
'.mysqli_error());

?>

even if the server says that content length is 0, i’m getting the data stored on the database (well, about 3/4 of the attemps are sucessfull).

By the way, i reaploaded the same code (juste changed delay to 30000 to see if a longer delay would help treat all the data sent) but now i have “connexion failed”:

Starting SD...ok
connecting...
connected
client connection
1
--> connection failed

client connection
0
--> connection failed

client connection
0
--> connection failed

client connection
0
--> connection failed

The server's php page is failing to connect to your SQL database.

edit: I am presuming that is the problem. The output doesn't match any of your posted code. Maybe you should post the code that is returning those statements.

Hi.

I tried with a new website, and used an ip adress instead.

I used the WebClientRepeating example.

I get this response from the server:

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("0824ee76937b7d0e62d7533912afe0d2");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://mysitehere/write_data.php?value=55&i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>connecting...
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 03 Jan 2017 14:15:54 GMT
Content-Type: text/html
Content-Length: 854
Connection: close
Vary: Accept-Encoding
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache

but nothing on the database.

My php code works fine (if i type mysite/write_data.php?value=45 i get the value stored).

Thanks in advance.

This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support

SurferTim:

Yes i saw that line, but what can i do if i am using an arduino? any solution?thanks

Or am i not understanding the problem here? :smiley:

Ok i think i need to change my website to an other one that does not recquire javascript enabled :confused:

EDIT:
Changed the service provider and it's working. thanks