定义引脚RX,没有SoftwareSerial的TX Arduino

定义引脚RX,没有SoftwareSerial的TX Arduino

问题描述:

嗨我做了2个不同的代码,然后在验证他们的工作后,我尝试将它们结合起来,我得到了一个问题。我用GSM.h来控制我的GSM模块,并且使用SoftwareSerial.h来控制GPS。定义引脚RX,没有SoftwareSerial的TX Arduino

我尝试将这些代码结合起来,并将这两个库相互混淆。

有人能帮助我吗?

这是我的代码

//GSM 
#include <GSM.h> 
#define PINNUMBER "3805" 
GSM gsmAccess; // include a 'true' parameter for debug enabled 
GSM_SMS sms; 
//N de telefone de envio 
char remoteNumber[20]= "914181875"; 
//Conteudo do SMS 
char txtMsg[200]="Tester"; 
int val = 0; 
//GPS 
//#include <SoftwareSerial.h> 
#include <TinyGPS.h> 
TinyGPS gps; 
SoftwareSerial ss(4, 3); 

static void print_float(float val, float invalid, int len, int prec); 

int button = 7; 

void setup() 
{ 
    // initialize serial communications 
    pinMode(button, INPUT); 
    Serial.begin(9600); 

    Serial.println("SMS Messages Sender"); 

    // connection state 
    boolean notConnected = true; 

    // Start GSM shield 
    // If your SIM has PIN, pass it as a parameter of begin() in quotes 
    while(notConnected) 
    { 
    if(gsmAccess.begin(PINNUMBER)==GSM_READY) 
     notConnected = false; 
    else 
    { 
     Serial.println("Not connected"); 
     delay(1000); 
    } 
    } 
    Serial.println("GSM initialized"); 
} 

void loop() 
{ 
val = digitalRead(button); 
if (val == HIGH){ 
    sendSMS(); 
    } 
} 

void sendSMS(){ 

    Serial.print("Message to mobile number: "); 
    Serial.println(remoteNumber); 

    // sms text 
    Serial.println("SENDING"); 
    Serial.println(); 
    Serial.println("Message:"); 
    Serial.println(txtMsg); 

    // send the message 
    sms.beginSMS(remoteNumber); 
    sms.print(txtMsg); 
    sms.endSMS(); 
    Serial.println("\nCOMPLETE!\n"); 
} 
+0

目前还不清楚你在问什么。请参阅[如何提出一个好问题](http://*.com/help/how-to-ask)并编辑您的问题,以更明确地了解您的问题是什么,您遇到什么错误,您期望什么得到,等等。 –

我会尝试使用AltSoftSerial库而不是SoftwareSerial:

https://github.com/PaulStoffregen/AltSoftSerial

这个库的缺点是它需要你使用串行特定脚与您的GPS硬件进行通信,这与您当前使用的引脚不同:

// AltSoftSerial always uses these pins: 
// 
// Board   Transmit Receive PWM Unusable 
// -----   -------- ------- ------------ 
// Teensy 3.0 & 3.1 21  20   22 
// Teensy 2.0   9  10  (none) 
// Teensy++ 2.0  25   4  26, 27 
// Arduino Uno  9   8   10 
// Arduino Leonardo 5  13  (none) 
// Arduino Mega  46  48  44, 45 
// Wiring-S   5   6   4 
// Sanguino   13  14   12 

我没有测试硬件,但下面的代码不会编译:

//GSM 
#include <GSM.h> 
#define PINNUMBER "3805" 
GSM gsmAccess; // include a 'true' parameter for debug enabled 
GSM_SMS sms; 
//N de telefone de envio 
char remoteNumber[20]= "914181875"; 
//Conteudo do SMS 
char txtMsg[200]="Tester"; 
int val = 0; 
//GPS 
#include <AltSoftSerial.h> 
AltSoftSerial ss; 
#include <TinyGPS.h> 
TinyGPS gps; 

static void print_float(float val, float invalid, int len, int prec); 

int button = 7; 

void setup() 
{ 
    // initialize serial communications 
    pinMode(button, INPUT); 
    Serial.begin(9600); 

    Serial.println("SMS Messages Sender"); 

    // connection state 
    boolean notConnected = true; 

    // Start GSM shield 
    // If your SIM has PIN, pass it as a parameter of begin() in quotes 
    while(notConnected) 
    { 
    if(gsmAccess.begin(PINNUMBER)==GSM_READY) 
     notConnected = false; 
    else 
    { 
     Serial.println("Not connected"); 
     delay(1000); 
    } 
    } 
    Serial.println("GSM initialized"); 
} 

void loop() 
{ 
val = digitalRead(button); 
if (val == HIGH){ 
    sendSMS(); 
    } 
} 

void sendSMS(){ 

    Serial.print("Message to mobile number: "); 
    Serial.println(remoteNumber); 

    // sms text 
    Serial.println("SENDING"); 
    Serial.println(); 
    Serial.println("Message:"); 
    Serial.println(txtMsg); 

    // send the message 
    sms.beginSMS(remoteNumber); 
    sms.print(txtMsg); 
    sms.endSMS(); 
    Serial.println("\nCOMPLETE!\n"); 
} 
+0

Thx为您提供帮助,是的,它确实编译,但硬件确实会启动它们之间的端口冲突。我如何阻止? –