简体   繁体   English

如何在arduino中从SD卡创建变量

[英]how to create variable from sd card in arduino

I have saved a number onto a sd card in a file called CONFIG.BAT, it is a telephone number and is in the format of "+441234567890", in the serial monitor I can print it out using 我已经将一个数字保存到名为CONFIG.BAT的文件中的sd卡中,它是一个电话号码,格式为“ +441234567890”,在串行监视器中,我可以使用

  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

Serial.println("CONFIG.DAT:");

// read from the file until there's nothing else in it:
while (myFile.available()) {

      Serial.write(myFile.read());

}

And I get this responce 我得到这个回应

Initializing SD card...initialization done.
CONFIG.DAT:
+441234567890

What I want to do is put this number into a variable "telNo", I have tried to use 我想做的就是将此数字放入变量“ telNo”中,我尝试使用

telNo = (myFile.read());

The response I get is 我得到的答复是

Initializing SD card...initialization done.
CONFIG.DAT: 
10

I'm not sure what this "10" signifies. 我不确定这个“ 10”代表什么。

I have also tried "concat" 我也尝试过“ concat”

while (myFile.available()) {

        number.concat(myFile.read());      

}

The response I get is the Decimal code for the number with some numbers on the end I don't understand 我得到的响应是数字的十进制代码,最后还有一些我不明白的数字

Initializing SD card...initialization done.
CONFIG.DAT: 
48555653515353555354561310

The last four numbers (1310) I don't understand 我不明白最后四个数字(1310)

What I'm looking to achieve is "telNo = the telephone number in the CONFIG.DAT file, so if any one could help I would appreciate it 我想要实现的是“ telNo = CONFIG.DAT文件中的电话号码,所以如果有人可以帮助我将不胜感激

Here is my full Sketch 这是我的完整草图

#include <SoftwareSerial.h>
//#include <SD.h>
#define SD_CS_PIN SS
#include <SPI.h>
#include "SdFat.h"

SdFat SD;

File myFile;

//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8

//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7

//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);


//const int ledPin3 = 11;//Define the Bluetooth led pin
const int ledPin2 = 2;//Define the interupt pin to signify bluetooth connect or disconnect

String readString = "";
String telNoString = "";
String number = "";

volatile int ledonState = 0;
int lastLedonState = 0;


const int thresholdvalue=680;//The threshold to turn the led on


void setup() {
  pinMode (ledPin2, INPUT);//set input for interupt pin
  //pinMode (ledPin3, OUTPUT);//set output for bluetooth pin

  attachInterrupt(0, pin_ISR, CHANGE);

  Serial.begin(9600);

  while(!Serial);

  //Being serial communication witj Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);

  SysCall::yield();

  Serial.println("Setup Complete!");

}


void pin_ISR() {
  ledonState = digitalRead(ledPin2);

if (ledonState != lastLedonState) {

  if (ledonState == HIGH) {
        //digitalWrite(ledPin3, HIGH);//turn led on
            Serial.println("HC-05 is now connected");
            //Serial.println();
      }else{
         //digitalWrite(ledPin3, LOW);//turn led off
             Serial.println("HC-05 is now Disconnected");
             //Serial.println();
  }
lastLedonState = ledonState;
}
}


void sound_detect(){

  int sensorValue = analogRead(A0);//use A0 to read the electrical signal

  if(sensorValue > thresholdvalue) {

//  digitalWrite(ledPin1,HIGH);//if the value read from A0 is larger than 400,then light the LED
//  delay(10);
//  digitalWrite(ledPin1,LOW);

 Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");  

  if (!SD.exists("CONFIG.DAT")) {

    Serial.println("No Number Exists! Please go to Setup Device to add Number for Alert");

  }else{

  // open the file for reading:
  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

            number.concat(myFile.read());      
          //number = (myFile.read());
          //Serial.write(myFile.read());

    }

      //Serial.println(myFile);    

    // close the file:
    myFile.close();

  } else {

    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");

  }

  }


//    //Set SMS format to ASCII
//  serialSIM800.write("AT+CMGF=1\r\n");
//  delay(1000);
// 
//  //Send new SMS command and message number
//  serialSIM800.write("AT+CMGS=\"+44"+number+"\"\r\n");
//  delay(1000);
//   
//  //Send SMS content
//  serialSIM800.write("TEST SMS NOISE DETECT");
//  delay(1000);
//   
//  //Send Ctrl+Z / ESC to denote SMS message is complete
//  serialSIM800.write((char)26);
//  delay(1000);
//     
//  Serial.println("SMS Sent!");
Serial.println("this is the phone number: ");
Serial.print(number);

}

}



void insertNo(){

        while(Serial.available()==0) { // Wait for User to Input Data  
  }

telNoString = Serial.readString();


  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.println("initialization done.");

  if (SD.exists("CONFIG.DAT")) {

  SD.remove("CONFIG.DAT");
  }

  if (!SD.exists("CONFIG.DAT")) {

  myFile = SD.open("CONFIG.DAT", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to CONFIG.DAT...");

    myFile.println(telNoString);

    // close the file:
    myFile.close();

    Serial.println("done.");

  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");
  } 

  // re-open the file for reading:
  myFile = SD.open("CONFIG.DAT");

  if (myFile) {

    Serial.println("CONFIG.DAT:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

      Serial.write(myFile.read());

    }

    // close the file:
    myFile.close();

  } else {

    // if the file didn't open, print an error:
    Serial.println("error opening CONFIG.DAT");

  }     

  }
   return;    
}


void loop() {

 while (Serial.available()) {
          delay(10);       
          char c = Serial.read();
          readString  += c;
 }

    if(readString == "setup device") {

      Serial.println(readString);
      readString = "";



insertNo();

    }else{
      if(readString == "start device") {

      Serial.println(readString);
      readString = "";


    }
 sound_detect();   
    }       
}

The section of commented out code are the AT commands to send the sms message. 注释掉的代码部分是用于发送短信的AT命令。

telNo = (myFile.read()); does that compile? 会编译吗? What is the type of telNo ? telNo的类型是telNo If it's an int or some other numerical, you won't get that result you want because read() reads characters and not numbers.. The 10 that you see is the ascii code for the newline character ('\\n') which is the first character in the file (that's why you get your number below the CONFIG.DAT: ). 如果它是int或其他数字,则不会获得所需的结果,因为read()读取字符而不是数字。.您看到的10是newline ('\\ n')的ascii代码,即文件中的第一个字符(这就是为什么将数字放在CONFIG.DAT:以下的CONFIG.DAT: )。

read() reads a byte at a time and returns -1 if none is available. read()读取一个字节,如果没有可用字节,则返回-1。 To get the whole content, you should use a loop to read one character at a time and append those characters to a char[] : 要获取全部内容,您应该使用循环一次读取一个字符并将这些字符附加到char[]

unsigned int MAX_SIZE = 100; 
char string[MAX_SIZE];
unsigned int index = 0;
char next_char;
while (next_char = myfile.read()) string[index++] = next_char;
string[index] = '/0'; // terminate with the null character

Now, if you really want an int , you need to filter out the newline , the + and any other non-valid digit and then cast your number to int : 现在,如果您确实想要一个int ,则需要过滤掉newline+和任何其他无效数字,然后将数字转换为int

char new_string[MAX_SIZE];
unsigned int i = 0, unsigned int j = 0;
while (i < MAX_SIZE) {  
    if ((string[i] >= 48) && (string[i] <= 57)) 
        new_string[j++] = string[i++];  
    else i++;  
}  
int number = atoi(new_string);

I think I found an answer as follows 我想我找到了以下答案

// read from the file until there's nothing else in it:
while (myFile.available()) {

            char ltr = myFile.read();
            number += ltr;      

} 

This was found at Text file on SD to String 在SD上的文本文件中找到此字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM