Arduino不推荐使用从字符串常量到'char *'的转换[-Wwrite-strings]

时间:2016-10-03 06:51:25

标签: arduino arduino-uno

当我编译/验证或上传我的代码到Arduino板时,我收到了这个弃用的转换错误。

#define SENSOR 3
#define BUZZER 4
#define RELAY 5
#define MOTOR 6


char phonenumber[] = "+639278832517";
//char phonenumber[] = "+639166390471";
uint8_t vibrationData = 0;
uint8_t isArmed = 1;
float latitude, longitude, speed_kph, heading, altitude;
long randNumber;

void setup() {
  Serial.begin(115200);
  pinMode(RELAY, OUTPUT);
  pinMode(MOTOR, OUTPUT);
  pinMode(BUZZER, OUTPUT);

  digitalWrite(RELAY, HIGH);
  digitalWrite(MOTOR, HIGH);
  digitalWrite(BUZZER, HIGH);

  randomSeed(analogRead(A0));

  attachInterrupt(digitalPinToInterrupt(SENSOR), vibrationISR, RISING);
  setupFona();

  disarm();
  Serial.println("Setup done!");

  latitude = 14.7042209;
  longitude = 121.0369513;

  deleteMessages();
}

void vibrationISR() {
  if(!isArmed) return;
  if(vibrationData == 2) return;
  vibrationData = 1;
}

void loop() {

  if(isArmed) {
    if(vibrationData > 0) {
      vibrationData = 2;
      digitalWrite(BUZZER, LOW);
      Serial.println("Vibration Detected");
      if(sendSMS(phonenumber, "Vibration sensor triggered")) {
        Serial.println("Send notification success!");
      } else {
        Serial.println("Send notification failed");
      }
      delay(1000);
      digitalWrite(BUZZER, HIGH);
      vibrationData = 0;
    }
  }

  String response =  readSMS(phonenumber);

  if(response.equals("!@disarm")) {
    disarm();
  } else if(response.equals("!@arm")) {
    arm();
  } else if(response.equals("!@on")) {
    motorOn();
  } else if(response.equals("!@off")) {
    motorOff();
  } else if(response.equals("!@gps")) {
    sendGPS();
  }
}

void sendGPS() {
  if(getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude)) {
    Serial.println("Got GPS fix");
  } else {
    randNumber = random(300);

    if (randNumber % 2 == 0) {
      latitude += 0.0001;
      longitude += 0.0001;
    } else {
      latitude -= 0.0001;
      longitude -= 0.0001;
    }

    Serial.println("Cannot get GPS fix");
    Serial.print(latitude, 6); Serial.print(","); Serial.println(longitude, 6);
  }

  String message = "!@location:" + String(latitude, 6) + "," + String(longitude, 6);
  Serial.println(message);
  int messageLen = message.length(); 
  char messageArray[messageLen + 1];

  message.toCharArray(messageArray, messageLen + 1);
  if(sendSMS(phonenumber, messageArray)) {
    Serial.println("Send GPS success!");
  } else {
    Serial.println("Send GPS failed");
  }
}


void arm() {
  Serial.println("Arm");
  isArmed = 1;
  digitalWrite(RELAY, HIGH);
  digitalWrite(MOTOR, HIGH);
  if(sendSMS(phonenumber, "System is armed")) {
    Serial.println("Send Arm notification success!");
  } else {
    Serial.println("Send Arm notification failed");
  }
}

void disarm() {
  Serial.println("Disarm");
  isArmed = 0;
  digitalWrite(RELAY, LOW);
    if(sendSMS(phonenumber, "System is disamred")) {
    Serial.println("Send Disarm notification success!");
  } else {
    Serial.println("Send Disarm notification failed");
  }
}

void motorOn() {
  Serial.println("Motor On");
  digitalWrite(MOTOR, LOW);
    if(sendSMS(phonenumber, "Motor is now on")) {
    Serial.println("Send Motor On notification success!");
  } else {
    Serial.println("Send Motor On notification failed");
  }
}

void motorOff() {
  Serial.println("Motor Off");
  digitalWrite(MOTOR, HIGH);
    if(sendSMS(phonenumber, "Motor is now off")) {
    Serial.println("Send Motor Off notification success!");
  } else {
    Serial.println("Send Motor Off notification failed");
  }
}

错误日志:

E:\smart-motorcycle\smart-motorcycle.ino:145:47: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

 if(sendSMS(phonenumber, "Motor is now off")) {

                                           ^

此代码已经过测试,但在从另一台笔记本电脑导入项目后,出现了这些错误。

1 个答案:

答案 0 :(得分:0)

您的sendSMS可能看起来像这样

void sendSMS(char *phonenumber, char *message);

当你传递字符串文字时,这应该是const char *。

void sendSMS(const char *phonenumber, const char *message);
请注意,由于数组不是指向const内存的指针,因此您不会收到有关电话号码的警告。

char phonenumber[] = "+639278832517";