Pages: 1 2 [3]   Go Down
Author Topic: Arduino + official ethernet shield + SD -> all working together.  (Read 13161 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are right, inserting the sd into the slot is stopping Server ethernet...

Does anyone knows why about this fact ?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just spent a painful 20 hours banging on an arduino ethernet shield with an SD card trying to get them to work together.  This thread, ladyada's tutorial - all resulting in arduino crashing when switching between ethernet and SD - nothing worked.

Reason: I was using the Arduino IDE 1.0.1 - everything works when I went back to 1.0

Symptoms - garbage spat out into serial, setup() restarting, not being able to open the files, etc.

I want my weekend back  smiley-eek-blue
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mine works fine together using IDE v1.0.1. I use this setup with dhcp for the w5100:
Code:
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

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

  Serial.print("Starting ethernet..");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println("ok");
  // Ethernet.begin() returns with its SPI enabled, so disable it
  digitalWrite(10,HIGH);
}

void loop() {
}
What does the serial monitor say? ok or failed?
I can't give you your weekend back.  smiley-sad

edit: Added missing double quote. My bad.  smiley-red
« Last Edit: July 03, 2012, 05:34:19 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you my friend.I had many problems but your setup process worked first time. smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is this thread still alive? hope someone can help me. I try to implement this code for my gps data logger, when i use the code with analog input as in this thread i can successfully insert the data both to SD card and mysql database. but when i use my gps logger sketch its only write the data to SD card but not to myql database, here is my skecth :

Code:
#include <TinyGPS.h>
#include <SD.h>
#include <stdlib.h>
#include <SPI.h>
#include <Ethernet.h>

TinyGPS gps;
static char dtostrfbuffer[20];
int CS = 4;
int ETH = 10;
int LED = 13;

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0D, 0xD2, 0xC2 };
IPAddress ip(192,168,0, 3);
IPAddress subnet(255,255,255,0);
IPAddress gateway(192,168,0,1);
IPAddress server(192,168,0,5);
EthernetClient client;

boolean SDavailable;
//Define String
String SD_date_time = "invalid";
String SD_lat = "invalid";
String SD_lon = "invalid";
String SD_speed = "invalid";
String SD_heading = "invalid";


static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec, int SD_val);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{
  //Serial interfaces
  Serial.begin(9600);
  Serial3.begin(4800);

  //set pinmode
  pinMode(ETH, OUTPUT);
  pinMode(CS, OUTPUT);

  //start ethernet
  digitalWrite(ETH, LOW); //ethernet on
  digitalWrite(CS, HIGH); //SD card off
  Ethernet.begin(mac, ip, gateway, subnet);


  //Connect to the SD Card
  Serial.println("Starting SD card...");
  digitalWrite(ETH, HIGH);
  digitalWrite(CS, LOW);
  if(!SD.begin(CS))
  {
    Serial.println("Card Failure");
    //return;
    SDavailable = false;
  }
  else
  {
    Serial.println("Card initialized");
    SDavailable = true;
  }
}


void loop()
{
  String dataString ="";

  bool newdata = false;
  unsigned long start = millis();

  // Ambil data setiap 2 detik
  while (millis() - start < 2000)
  {
    if (feedgps())
      newdata = true;
  }

  gpsdump(gps);

  //Tulis data ke sd card
  if (SDavailable !=false) {
    digitalWrite(CS, LOW);
    digitalWrite(ETH, HIGH);

    dataString = "CRM001, " + SD_date_time + "," + SD_lat + "," + SD_lon + "," + SD_speed + "," + SD_heading;
    if(SD_date_time != "invalid")
      digitalWrite(LED, HIGH);
    else
      digitalWrite(LED, LOW);

    //open file log.txt
    File dataFile = SD.open("LOG.txt", FILE_WRITE);
    if (dataFile)
    {
      Serial.println(dataString);
      dataFile.println(dataString);
    }

    else
    {
      Serial.println("\nCouldn't open the log file!");
    }
    dataFile.close();
  }
  //id++;
  // delay(5000);
  digitalWrite(ETH, LOW);
  digitalWrite(CS, HIGH);
  if(client.connect(server, 80))
  {       
    //Serial.println("connected...");
    //Serial.println("ARDUINO: forming HTTP request message");
    client.print("GET /arduino/insert.php?dataString=");
    client.print(dataString);
    client.println(" HTTP/1.1");
    client.println("Host: localhost");
    client.println("Connection: close");
    client.println();

    Serial.println("ARDUINO: HTTP message sent");
    //delay(2000);

    client.stop();
    //client.flush();
  }
  else
  {
    Serial.println("connection failure");
  }
}

static void gpsdump(TinyGPS &gps)
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;

  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5, 1); //LATITUDE
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5, 2); //LONGITUDE
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  print_date(gps); //DATE AND TIME
  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2, 0);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2, 4);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2, 3);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);

}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len-1] = ' ';
  //Serial.print(sz);
  feedgps();
}


static void print_float(float val, float invalid, int len, int prec, int SD_val)
{
  char sz[32];
  if (val == invalid)
  {
    strcpy(sz, "*******");
    sz[len] = 0;
    if (len > 0)
      sz[len-1] = ' ';
    for (int i=7; i<len; ++i)
      sz[i] = ' ';
    //Serial.print(sz);
    if(SD_val == 1) SD_lat = sz;
    else if(SD_val == 2) SD_lon = sz;
    else if(SD_val== 3) SD_speed = sz;
    else if(SD_val== 4) SD_heading = sz;
  }
  else
  {
    //Serial.print(val, prec);
    if (SD_val == 1) SD_lat = dtostrf(val,10,5,dtostrfbuffer);
    else if (SD_val == 2) SD_lon = dtostrf(val,10,5,dtostrfbuffer);
    else if (SD_val == 3) SD_speed = dtostrf(val,10,2,dtostrfbuffer);
    else if (SD_val == 4) SD_heading = dtostrf(val,10,2,dtostrfbuffer);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1);
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(" ");
  }
  feedgps();
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
  {
    //Serial.print("*******    *******    ");
    SD_date_time = "invalid";
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d-%02d-%02d %02d:%02d:%02d   ",
    year, month, day, hour, minute, second);
    //Serial.print(sz);
    SD_date_time = sz;
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  feedgps();
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    //Serial.print(i<slen ? str[i] : ' ');
    feedgps();
}

static bool feedgps()
{
  while (Serial3.available())
  {
    if (gps.encode(Serial3.read()))
      return true;
  }
  return false;
}





Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using an Uno, do not use D13 LED. That is one of the SPI data lines.

Without reading  and displaying the server response, it is hard to tell what the problem is. I modified your GET request to send the correct request, then read the server response.
Code:
  if(client.connect(server, 80))
  {       
    //Serial.println("connected...");
    //Serial.println("ARDUINO: forming HTTP request message");
    client.print("GET /arduino/insert.php?dataString=");
    client.print(dataString);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close\r\n");

    // read server response   
    while(client.connected())
    {
      while(client.available())
      {
        inChar = client.read();
        Serial.write(inChar);
        // set connectLoop to zero if a packet arrives
        connectLoop = 0;
      }

      connectLoop++;

      // if more than 10000 milliseconds since the last packet
      if(connectLoop > 10000)
      {
        // then close the connection from this end.
        Serial.println();
        Serial.println(F("Timeout"));
        client.stop();
      }
      // this is a delay for the connectLoop timing
      delay(1);
    }

    Serial.println();
    Serial.println(F("disconnecting."));
    // close client end
    client.stop();
  }
  else
  {
    Serial.println("connection failure");
  }
What does the server return?

If you are using an Uno, it is possible you could be running out of SRAM.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My apologize i forgot, i use Mega 2560 and ethernet shield with built in sd card.

here is my server response
Code:
Starting SD card...
Card initialized
HTTP/1.1 200 OK
Date: Fri, 24 May 2013 12:15:01 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By: PHP/5.2.8
Content-Length: 92
Connection: close
Content-Type: text/html

array(1) {
  ["dataString"]=>
  string(7) "CRM001,"
}
Array
(
    [dataString] => CRM001,
)

disconnecting.


and here is the data logged in my LOG.txt
CRM001, 2013-05-14 15:27:23   ,  -6.89235, 107.56865,      1.31,    275.34

looks like only the first column sent to my server.

below is insert.php code in my server
Code:
<?
var_dump($_GET);
//exit;
$val = @$_GET['dataString']; 
if(empty($_GET))
    echo "No GET variables";
else
    print_r($_GET);

$con = mysql_connect("localhost", "root", "");
if(!$con)
      die('Could not connect: ' .mysql_error());
mysql_select_db("arduino", $con);

$result = mysql_query("INSERT INTO arduino(vid,date,lat,lon,speed,heading) VALUES ($val)");
mysql_close($con);
?>
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is that a space following "CRM001,"? That would be an illegal character in the GET request.
Code:
dataString = "CRM001, " + SD_date_time + "," + SD_lat + "," + SD_lon + "," + SD_speed + "," + SD_heading;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for your help, yes it is a space following "CRM001,"
Now i remove the space from CRM001 and i got the following server response
Code:
HTTP/1.1 200 OK
Date: Fri, 24 May 2013 12:50:47 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By: PHP/5.2.8
Content-Length: 113
Connection: close
Content-Type: text/html

array(1) {
  ["dataString"]=>
  string(17) "CRM001,2013-05-24"
}
Array
(
    [dataString] => CRM001,2013-05-24
)

disconnecting.
CRM001,2013-05-24 12:50:47,  -6.89233, 107.56870,      0.26,    137.12

I know it is because the date time format is use a space between day and hour. how can i send this date time format? or i have to use another format and format it later.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
CRM001,2013-05-24 12:50:47,  -6.89233, 107.56870,      0.26,    137.12
OK. You eliminated the first space, but what about the rest of those? Like between the date and time? And before latitude? And before longitude? And before speed? And before heading?

edit: Since you are already using the String data type, then you should be able to use the replace() function to replace all occurrences of " " with "+".
http://arduino.cc/en/Reference/StringReplace
« Last Edit: May 24, 2013, 08:12:41 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know there's still a lot of thing to learn cause i'm just started to learn arduino and i have no programming background. another thing... english is not my native language so sometime i need more time to understand something that even its so simple or easy.

I'm thinking to use NMEA 0183 format and process the data later using database processing program such as php. I'm still googling to find the answer cause in this project i need to send the historical data to the database since the ethernet connection is not always available.

thank you
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 150
Posts: 6132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not a problem. English is my native language, and I still have trouble with it.  smiley

Did you see my edit above about replacing the spaces in your string with a plus(+)? As long as those are not tabs in the string, it should work.

edit: On the php end, use urldecode to change the "+" back to " ".
http://php.net/manual/en/function.urldecode.php
« Last Edit: May 24, 2013, 08:21:27 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now i already get the correct server response
Code:
HTTP/1.1 200 OK
Date: Fri, 24 May 2013 14:24:55 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By: PHP/5.2.8
Content-Length: 193
Connection: close
Content-Type: text/html

array(1) {
  ["dataString"]=>
  string(57) "CRM001,2013-05-24 14:24:55,-6.89232,107.56871,0.63,147.05"
}
Array
(
    [dataString] => CRM001,2013-05-24 14:24:55,-6.89232,107.56871,0.63,147.05
)

disconnecting.
CRM001,2013-05-24+14:24:55,-6.89232,107.56871,0.63,147.05

since inserting a string to mysql must use quotes, i try to directly add quotes to CRM001 and date but i can't get it working. How to add a quotes to a string in the sketch or is it better to add the quotes in the php before insert to database?

thank you very  much and forgive me for a lot of asking
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I try to modify the php script but i still get get the data send to my database
Code:
<?
var_dump($_GET);
//exit;
$val = @$_GET['DBString']; 
extract($_GET);
$dbdata = "'".implode("','", explode(",", $DBString))."'";
echo $dbdata;
if(empty($_GET))
    echo "No GET variables";
else
   print_r($_GET);
//Connect to database
$con = mysql_connect("localhost", "root", "");
if(!$con)
      die('Could not connect: ' .mysql_error());
mysql_select_db("arduino", $con);

$result = mysql_query("INSERT INTO arduino(vid,date,lat,lon,speed,heading) VALUES ($dbdata)");
if(!$result) {
echo "Error".mysql_error();
}
mysql_close($con);
?>

Here is the server response
Code:
HTTP/1.1 200 OK
Date: Sat, 25 May 2013 12:41:16 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By: PHP/5.2.8
Content-Length: 247
Connection: close
Content-Type: text/html

array(1) {
  ["dataString"]=>
  string(57) "CRM001,2013-05-25 12:41:16,-6.89240,107.56863,0.37,121.77"
}
''Array
(
    [dataString] => CRM001,2013-05-25 12:41:16,-6.89240,107.56863,0.37,121.77
)
ErrorColumn count doesn't match value count at row 1
disconnecting.
When i try to put the data directly in the web address i.e :http://localhost/arduino/insert.php?DBString=CRM001,2013-05-25%2012:38:31,-6.89240,107.56863,1.24,152.30 i can successfully send the DBString data to my database.
Logged

Pages: 1 2 [3]   Go Up
Jump to: