我正在研究一个项目,通过填充这些Web数据库来跟踪传感器值,使用Arduino Uno板从中提取值并使用以太网SD屏蔽通过以太网发送,使用W5100芯片 . 因为项目需要为Arduino板添加一个独立的功能以实现值跟踪,所以我必须在我的代码中添加数据 Logger 示例 . 问题是,即使我只包含SD库,程序也会崩溃 . 串口屏幕没有输出,没有初始化以太网或/和传感器,什么都没有 . 我使用digitalWire和pinMode稍微摆弄一下,在以太网传输或写卡的情况下选择合适的芯片,但没有用 . 到目前为止我的代码是这样的:

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <SFE_BMP180.h>
//#include <SD.h>

// You will need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 pressure;
#define ALTITUDE 20.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
// BMP180 variables
char status;
double inHb,TempC,T,P,p0,a,AltitudeMeters=ALTITUDE*3.28084;
long tempC;

// TEMT6000 
int temt6000Pin = 0;
int initTEMT6000 = 2;     // variable to compare the running time, used to enable by 5 seconds a reading for the TEMT6000 sensor
int ambientLight = 0; // Brightness for the CSS Ambient ligh box

// Analog Reflectance Sensor
// Define constants and variables
const int LED = 13;                   // sets the LED on pin 13
const int  STATE = 2;                 // sets pin 2 for sensor reading
int r_state = 0;                      // reset to zero the variable used to read the state of the OUT pin of the sensor

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(192,168,1,65); // WAMP

EthernetClient client;

long time=0;
boolean proximity=false;

void setup() {
  Serial.begin(9600);
/*
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  // SD card initialization
   Serial.print("Initializing SD card...");
   // make sure that the default chip select pin is set to
   // output, even if you don't use it:   
   // see if the card is present and can be initialized:
   if (!SD.begin(4)) 
   //if (!card.init(SPI_FULL_SPEED, 4))
   Serial.println("Card failed, or not present");  
   else Serial.println("card initialized.");   
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);
*/
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  Serial.print("Arduino's IP:");
  Serial.println(Ethernet.localIP());
  delay(500);
  Serial.print("Subnet Mask:");
  Serial.println(Ethernet.subnetMask());
  delay(500);
  Serial.print("Gateway IP:");
  Serial.println(Ethernet.gatewayIP());
  delay(500);
  Serial.print("DNS Server IP:");
  Serial.println(Ethernet.dnsServerIP());

  //Analog Reflectance Sensor Setup
  pinMode (LED, OUTPUT);               // sets pin 13 as digital output
  pinMode (STATE, INPUT);              // sets pin 2 as digital input 

  // BMP180 sensor setup
  Serial.begin(9600);
  Serial.println("REBOOT");

  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
}

void loop()
{
   digitalWrite(10,LOW);
  // make a string for assembling the data to log:
  String dataString = "";

  Serial.print("connecting...");

  if(client.connect(server,80))
  {
    Serial.println("connected");

    // Analog reflectance sensor main code
    r_state = digitalRead(STATE); // reads the status of the sensor          
    if(r_state == 0){              // if is there an obstacle (OUT = 0)
      digitalWrite (LED, HIGH);   // turn on the led
      proximity = true;
      dataString += "\n(";
      dataString += time;
      dataString += ") !Obstacle Detected!\n";
    }
    else{
      digitalWrite (LED, LOW);    // turn off the led
      proximity = false;
    }

    // Ambient light sensor (TMP 6000) main code
    ambientLight = analogRead(temt6000Pin);
    dataString += "(";
    dataString += time;
    dataString += ") Ambient light value = ";
    dataString += analogRead(temt6000Pin);

    // Barometric Pressure & Temperature sensor (BMP180) main code   

    // Loop here getting pressure readings every 10 seconds.

    // If you want sea-level-compensated pressure, as used in weather reports,
    // you will need to know the altitude at which your measurements are taken.
    // We're using a constant called ALTITUDE in this sketch:

    // Print out the measurement to SD card:
    dataString += "provided altitude: ";
    long altmeters = ALTITUDE;
    dataString += altmeters;
    dataString += " meters, ";
    long altfeet = ALTITUDE*3.28084;
    dataString += altfeet;
    dataString += " feet\n";
    // End of SD writing

    // Start a temperature measurement:
    // If request is successful, the number of ms to wait is returned.
    // If request is unsuccessful, 0 is returned.

    status = pressure.startTemperature();
    if (status != 0)
    {
      // Wait for the measurement to complete:
      delay(status);

      // Retrieve the completed temperature measurement:
      // Note that the measurement is stored in the variable T.
      // Function returns 1 if successful, 0 if failure.

      status = pressure.getTemperature(T);
      if (status != 0)
      {
        // Print out the measurement to SD card:
        dataString += "temperature: ";
        tempC = T;
        dataString += tempC;
        dataString += " deg C, ";
        long tempF = (9.0/5.0)*T;
        32.0;
        dataString += tempF;
        dataString += " deg F\n";
        TempC = (9.0/5.0)*T+32.0;
        // End to SD writing

        status = pressure.startPressure(3);
        if (status != 0)
        {
          // Wait for the measurement to complete:
          delay(status);

          status = pressure.getPressure(P,T);
          if (status != 0)
          {
            // Start of printing out the measurement to SD card:
            dataString += "absolute pressure: ";
            long presmb = P;
            dataString += presmb;
            dataString += " mb, ";
            long presinHg= P*0.0295333727;
            dataString += presinHg;
            dataString += " inHg\n";
            // The pressure sensor returns abolute pressure, which varies with altitude.
            // To remove the effects of altitude, use the sealevel function and your current altitude.
            // This number is commonly used in weather reports.
            // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
            // Result: p0 = sea-level compensated pressure in mb

            p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
            dataString += "relative (sea-level) pressure: ";
            long slmb = p0;
            dataString += slmb;
            dataString += " mb, ";
            long slinHg= p0*0.0295333727;
            dataString += slinHg;
            dataString += " inHg\n";

            // On the other hand, if you want to determine your altitude from the pressure reading,
            // use the altitude function along with a baseline pressure (sea-level or other).
            // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
            // Result: a = altitude in m.

            a = pressure.altitude(P,p0);
            dataString += "computed altitude: ";
            long alt = a;
            dataString += alt;
            dataString += " meters, ";
            long altfeet= a*3.28084;
            dataString += altfeet;
            dataString += " feet\n";
            // end of SD writing

            inHb=P*0.0295333727,2;

            p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)

            a = pressure.altitude(P,p0);
          }
          else Serial.println("error retrieving pressure measurement\n");
        }
        else Serial.println("error starting pressure measurement\n");
      }
      else Serial.println("error retrieving temperature measurement\n");
    }
    else Serial.println("error starting temperature measurement\n");

    // Send the GET HTTP method to populate the DataBase      
    client.print("GET /arduino/add_data.php?time=");
    client.print(time/1000);
    client.print("&barometric=");
    client.print(P);
    client.print("&proximity=");
    client.print(proximity);
    client.print("&light=");
    client.print(ambientLight);
    client.print("&humidity=000");
    //client.print(RH);
    client.print("&temperature=000");
    //client.print(tempC);
    client.println(" HTTP/1.0\r\n\r\n");
    client.println("GET /arduino/add_interval.php?interval=1 HTTP/1.0\r\n\r\n");
    Serial.println(dataString);
  } 
  else
  {
    Serial.println("failed");
  }

  while(client.connected())
  {
    while(client.available())
    {
      char c = client.read();
      Serial.write(c);
    }
    /*
    // open the file in the SD. note that only one file can be open at a time,
     // so you have to close this one before opening another.
     File dataFile = SD.open("Sensors.txt", FILE_WRITE);

     // if the file is available, write to it:
     digitalWrite(10,HIGH);
     digitalWrite(4,LOW);
     if (dataFile) {
     dataFile.println(dataString);
     dataFile.close();
     // print to the serial port too:
     Serial.println(dataString);
     }  
     // if the file isn't open, pop up an error:
     else {
     Serial.println("error opening Sensors.txt");
     }
     */
  }

  Serial.println();

  Serial.println("disconnecting.");
  client.stop();
  delay(1000);
}

在这种状态下,代码工作得很好,可以将数据发送到数据库 . 通过运行SD的各个例子,即使是这个,[Arduinio sd on Ethernet shield not working at all],Arduino板运行良好 . 这两者的混合是否会让我感到沮丧,现在可以这样:

  • TEMT6000环境光传感器(模拟引脚0上)

  • BMP180气压传感器(在I2C数据总线4和时钟5上)

  • MR003-007.1模拟反射传感器(数字引脚1)


Note: 即使使用Arduino板的外部电源,也没有区别,问题仍然存在 .


对不起我的长篇文章,并提前感谢您的帮助 .

Solution: 在Roger Rowland的指导下,整个问题是记忆力的稀缺 . 除了使用Strings之外,它们占用了大量的内存,File对象_2911296也是有罪的 . 干杯罗杰!