Arduino SIM800L:将String转换为char *错误

时间:2018-01-18 04:06:40

标签: c++ arduino

我目前正在学习Arduino编程并致力于使用SIM800L模块发送短信。

从SIM800L库样本草图中,有一个发送短信的功能

char* text;
char* number;
bool error;                     //to catch the response of sendSms


void setup(){
   GSM.begin(4800);             
   text="Testing Sms";          //text for the message. 
   number="2926451386";         //change to a valid number.
   error=SIM800L .sendSms(number,text);
}

我需要将手机号码更改为API API响应中的手机号码。 当我调用API时,我能够得到响应。 API会返回一个手机号码。

+639051234567它位于String数据类型中。

当我尝试将手机号码传递给sendSms功能时,我收到错误提示cannot convert 'String' to 'char*' in assignment

这里是完整的Arduino代码。

#include <Sim800L.h>
#include <SoftwareSerial.h>     
#include <UIPEthernet.h>

#define RX  10
#define TX  11

Sim800L GSM(RX, TX);

int buzzer = 8;
int smokeA0 = A5;
int smokeA1 = A4;
int smokeA2 = A3;

int sensorThres1 = 650;
int sensorThres2 = 500;
int sensorThres3 = 500;


byte mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};    
EthernetClient client;
char server[] = "192.168.2.100";
int  interval = 5000; 
char uid[] = "1"; 
String response = String(100); 
char* text;
char* number;

void setup() {

  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  pinMode(smokeA1, INPUT);
  pinMode(smokeA2, INPUT);


  Serial.begin(9600);
  Ethernet.begin(mac);

  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
  GSM.begin(4800);
}

void loop() {
   int analogSensor1 = analogRead(smokeA0);
   int analogSensor2 = analogRead(smokeA1);
   int analogSensor3 = analogRead(smokeA2);


  if (analogSensor1 > sensorThres1 || analogSensor2 > sensorThres2 || analogSensor3 > sensorThres3){

if(client.connect(server, 80)){
        Serial.println("-> Connected");
        // Make a HTTP request:
        client.print( "GET /apartment/insert.php?");
        client.print("uid=");
        client.print(uid);
        client.println( " HTTP/1.1");
        client.print( "Host: " );
        client.println(server);
        client.println( "Connection: close" );
        client.println();
        client.println();

       while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            //Serial.print(c);
            response= response + c;
          }
       }

       int contentBodyIndex = response.lastIndexOf('\n');
       if (contentBodyIndex > 0) {
          String str = response.substring(contentBodyIndex);
          int respLen = str.length() + 1;
          char charArr[respLen];

          number = str.toCharArray(charArr, respLen); // getting error 'void value not ignored as it ought to be'

          text = "Smoke was detected in your apartment unit";
          error=GSM.sendSms(number ,text);
       }



        client.stop();
        delay(1000); 
        tone(buzzer, 1000);
        delay(10000); 
        noTone(buzzer);
        delay(1000);    
      }else{
        Serial.println("--> connection failed/n");
      }
 }
 else
 {
   noTone(buzzer);
 }
 delay(1000);
}

感谢任何帮助..谢谢你们。

2 个答案:

答案 0 :(得分:2)

我认为只需将字符串<p-column *ngFor="let head of headersList" [field]="head.field" [header]="head.header" [filter]="true" filterPlaceholder="search" filterMatchMode="in"> <ng-template pTemplate="filter" let-col> <p-multiSelect [options]="head.options" defaultLabel="Search" (onChange)="dt.filter($event.value,col.field,col.filterMatchMode)"></p-multiSelect> </ng-template> </p-column>转换为response数组即可:

char *

答案 1 :(得分:0)

除非sendSms修改数字,否则无需将String对象复制到char数组中。

您可以使用String传递c_str对象的内部字符数组的指针。

String num = response.substring(contentBodyIndex+1); // +1 to get rid of the '+'
error = SIM800L.sendSms(num.c_str(), text);