【问题标题】:Define Pin RX, TX Without SoftwareSerial Arduino定义 Pin RX, TX without SoftwareSerial Arduino
【发布时间】:2017-03-09 16:26:38
【问题描述】:

您好,我做了 2 个不同的代码,然后在验证它们有效后,我尝试将它们组合起来,但我遇到了问题。我使用 GSM.h 来控制我的 GSM 模块,并使用 SoftwareSerial.h 来控制 GPS。

我尝试将这些与代码结合起来,这两个库相互冲突。

有人可以帮我吗?

这是我的代码

//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");  
}

【问题讨论】:

  • 不清楚你在问什么。请参阅How to Ask a Good Question 并编辑您的问题,以更明确地说明您的问题是什么、您遇到了什么错误、您期望得到什么等等。

标签: arduino gps gsm


【解决方案1】:

我会尝试使用 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");  
}

【讨论】:

  • 感谢您的帮助,是的,它确实可以编译,但硬件确实会启动它们之间的端口冲突。我该如何阻止它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多