gpt4 book ai didi

c++ - Arduino SIM800L : converting String to char* error

转载 作者:行者123 更新时间:2023-11-30 03:26:25 25 4
gpt4 key购买 nike

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

从SIM800L库sample sketch中,有发送短信的功能

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 返回手机号码。

+639051234567String数据类型。

当我尝试将手机号码传递给 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);
}

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

最佳答案

我认为只需将字符串 response 转换为 char * 数组就足够了:

...
String str = response.substring(contentBodyIndex);
int respLen = str.length() + 1
char charArr[respLen];
str.toCharArray(charArr, respLen);

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

关于c++ - Arduino SIM800L : converting String to char* error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48313690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com