我正在尝试让我的 arduino 与 gps 和 gprs shield 一起工作。到现在为止还挺好。我的代码在我注释了注释行时有效,如果我取消注释它们就停止工作。
甚至更早的事件也失败了。例如,我必须输出GPRS Registered
,GPRS AT Ready
但它不会发生。
我的arduino有可能损坏吗?
这是代码。
请注意,我使用实际屏蔽测试了我发送到 gprs 屏蔽的命令,并且它可以工作。
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#define BUFFSIZ 90
int GPRS_Registered;
int GPRS_AT_Ready;
char incoming_char = 0;
char buffidx;
char at_buffer[BUFFSIZ];
int firstLoop = 1;
TinyGPS gps;
SoftwareSerial cell(2,3);
SoftwareSerial uart_gps(0,1);
void sendSMS(char *msg){
cell.println("AT+CMGF=1");
cell.print("AT+CMGS=");
cell.write(34);
cell.print("A number here");
cell.write(34);
cell.println("");
delay(500);
cell.println(msg);
cell.write(26);
delay(15000);
}
void readATString(){
char c;
buffidx = 0;
while(true){
if(cell.available() > 0){
c = cell.read();
if(c == -1){
at_buffer[buffidx] = '\0';
return;
}
if(c == '\n'){
continue;
}
if((buffidx == BUFFSIZ - 1) || (c == '\r')){
at_buffer[buffidx] = '\0';
return;
}
at_buffer[buffidx++] = c;
}
}
}
void processATString(){
if(strstr(at_buffer, "+SIND: 8") != 0){
GPRS_Registered = 0;
Serial.println("GPRS Network Not Available");
}
if(strstr(at_buffer, "+SIND: 11") != 0){
GPRS_Registered = 1;
Serial.println("GPRS Registered");
}
if(strstr(at_buffer, "+SIND: 4") != 0){
GPRS_AT_Ready = 1;
Serial.println("GPRS AT Ready");
}
}
void setup(){
Serial.begin(115200);
cell.begin(9600);
uart_gps.begin(4800);
cell.listen();
Serial.println("Starting SM5100 Communication...");
delay(5000);
}
void loop(){
bool dataReady = false;
unsigned long chars, prev_chars = 0;
unsigned short sentences, failed;
if(firstLoop){
firstLoop = 0;
while(GPRS_Registered == 0 || GPRS_AT_Ready == 0){
readATString();
processATString();
}
Serial.println("Everything is working");
}
uart_gps.listen();
for (unsigned long start = millis(); millis() - start < 1000;){
while (uart_gps.available()){
char c = uart_gps.read();
if (gps.encode(c))
dataReady = true;
}
}
if (dataReady)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("flat: ");
Serial.print(flat, 6);
Serial.print("flon: ");
Serial.println(flon, 6);
cell.listen();
// HERE IS THE PROBLEM
// cell.println("AT+CGDCONT=1,\"IP\",\"internet\"");
// delay(3000);
// cell.println("AT+CGACT=1,1");
// delay(3000);
// cell.println("AT+SDATACONF=1,\"TCP\",\"my.server.com\",8080");
// delay(3000);
// cell.println("AT+SDATASTART=1,1");
// cell.println("AT+SSTRSEND=1,\"testing arduino gprs\"");
Serial.println("Sent data to server");
}
}