Avsak kedze som doteraz vyuzíval kniznicu ArduinotechGSMShield a v nej bola riesená komunikácia procesor <-> SIM800l cez softwareSerial potrebujem ju pravit tak aby som využíval na komunikáciu len Serial (rx,tx piny 0,1). Po úprave knižnice, kde využívam na komunikáciu len rx,tx piny (0,1) my kód nefunguje podla predstáv. Vyzerá to, ze funkcia CallandSMS(); nevracia po prezvonení hednotu 1, taktiež po prijatí sms nevracia 2. Neviete kde by mohla byt chyba?
PS: nesom ziaden velký programator len sa snažim upravit niečo co uz existuje
knižnica :
Kód: Vybrat vše
//knihovna pro obsluhu shieldu Arduinotech GSM
//vytvořeno 15.11.2015
//Update:
//Autor: Ing. Petr Foltýn
//www.arduinotech.cz
#ifndef ArduinotechGSMShield1
#define ArduinotechGSMShield1
//Includes
#include "Arduino.h"
//#include "SoftwareSerial.h"
class AGS
{
public:
String number;
String actTime;
String getListOfContacts();
String SMScontent;
AGS(uint8_t speed);
void SIM800Init();
void begin();
void sendSMS(String number, String sms);
String getNumber();
String getSMSContent();
String getQuality();
String getProviderName();
void checkInput();
//String timeStamp();
//String actualTime();
//void restartSIMHW();
void callEnd();
void makeCall(String callNumber);
uint8_t checkCallAndSMS();
bool isConnected();
//bool getPerson();
bool GPRSInit(String APN);
String sendDataGPRS(String dataToSend);
private:
uint8_t _debug;
bool callInProgress;
String providerName;
String GSMsignal;
bool rele1Status, rele2Status;
bool IN1Status, IN2Status;
uint8_t tempIndex;
void clrSIMbuffer();
String sendATcommandResponse(char* ATcommand, char* expected_answer, unsigned int timeout, unsigned int buf);
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout);
};
#endif
Kód: Vybrat vše
//Utilities for ArduinotechGSMShield1
#include "Arduino.h"
#include "ArduinotechGSMShield1.h"
AGS::AGS(uint8_t debug)
{
//_debug = debug;
}
void AGS::begin()
{
Serial.begin(9600);
SIM800Init();
}
//SIM800 initialization procedure for simple SMS and call features
void AGS::SIM800Init()
{
while (Serial.available() > 0) Serial.read();
//Serial.println F("****************************************");
while (sendATcommand("AT", "OK", 2000) == 0);
delay(2000);
//otestuj registraci do site
while ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) || sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0);
while (Serial.available() > 0) Serial.read();
//parameters to obtain time stamp from GSM network
while (sendATcommand("AT+CLTS=1", "OK", 500) == 0);
while (sendATcommand("AT+CENG=3", "OK", 500) == 0);
while (Serial.available() > 0) Serial.read();
sendATcommand("AT+CMGF=1", "OK", 500);
//zakaz indikace SMS
sendATcommand("AT+CNMI=0,0", "OK", 500);
//CLIP enabled
sendATcommand("AT+CLIP=1", "OK", 1000);
//smaz vsechny SMSky
sendATcommand("AT+CMGD=1,4", "OK", 2000);
sendATcommand("AT+CMGD=1", "OK", 2000);
//Serial.println F("SIM800 has been configured!");
//Serial.println F("****************************************");
}
bool AGS::GPRSInit(String APN)
{
APN = "AT+SAPBR=3,1,\"APN\",\"" + APN + "\"" ;
char APNchar[sizeof(APN)+1];
for (uint8_t i; i < (sizeof(APN) + 1); i++) APNchar[i] = APN[i];
while( (sendATcommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", 500)) == 0 );
//APN = "AT+SAPBR=3,1,\"APN\",\" + APN + "\"";
while( (sendATcommand(APNchar, "OK", 1000)) == 0 );
Serial.println("AT+SAPBR=1,1");
delay(1000);
Serial.println("AT+SAPBR=2,1");
delay(1000);
clrSIMbuffer();
while( (sendATcommand("AT+HTTPINIT", "OK", 3000))|| (sendATcommand("AT+HTTPINIT", "ERROR", 3000))== 0 );
//while( (sendATcommand("AT+HTTPINIT", "OK", 3000)));
return 1;
}
String AGS::sendDataGPRS(String dataToSend)
{
int dataLength;
dataToSend = "\""+dataToSend+"\"";
delay(10);
clrSIMbuffer();
Serial.println("AT+HTTPPARA=\"URL\"," + dataToSend);
while (Serial.find("OK"));
delay(10);
clrSIMbuffer();
while( (sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:", 10000)) == 0 );
delay(10);
clrSIMbuffer();
//read response
Serial.println("AT+HTTPREAD");
delay(500);
char gsmc;
String content="";
if(Serial.find("+HTTPREAD:"))
{
if (_debug == 1) //Serial.println("Reading response ...");
while(Serial.available()>0)
{
gsmc = Serial.read();
content += gsmc;
if (gsmc == '\n')
{
content = content.substring(0,content.length()-2);
dataLength = content.toInt();
if (_debug == 1) //Serial.println("Data Lenght:" + String(dataLength));
content = "";
break;
}
}
//read content
if (_debug == 1)//Serial.print("Content:");
delay(100);
while(Serial.available()>0)
{
gsmc = Serial.read();
content += gsmc;
dataLength --;
if (dataLength < -1)
{
content = content.substring(0,content.length()-2);
if (_debug == 1) //Serial.println(content);
content = "";
break;
}
}
}
else
{
return "COMMUNICATION FAILURE";
}
delay(10);
clrSIMbuffer();
return content;
}
//Send AT command to SIM800
int8_t AGS::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
//WDT_Restart(WDT);
uint8_t x = 0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
//clrSIMbuffer();
Serial.println(ATcommand); // Send the AT command
//WDT_Restart(WDT);
x = 0;
previous = millis();
// this loop waits for the answer
do
{
if (Serial.available() != 0) {
// if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
} while ((answer == 0) && ((millis() - previous) < timeout));
//clrSIMbuffer();
return answer;
}
//Send AT command to SIM800 with response
String AGS::sendATcommandResponse(char* ATcommand, char* expected_answer, unsigned int timeout, unsigned int buf)
{
//WDT_Restart(WDT);
uint8_t x = 0, answer = 0;
char response[150];
unsigned long previous;
String rest;
memset(response, '\0', 100); // Initialize the string
delay(100);
//clrSIMbuffer();
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
//WDT_Restart(WDT);
// this loop waits for the answer
do
{
if (Serial.available() != 0) {
// if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
} while ((answer == 0) && ((millis() - previous) < timeout));
//p?e?ti zbytek - max 20 byte
memset(response, '\0', buf); // Initialize the string
delay(100);
for (x = 0; x < buf; x++) response[x] = Serial.read();
//clrSIMbuffer();
for (x = 0; x < buf; x++) rest += char(response[x]);
delay(100);
return rest;
}
//Check SIM800 if call or SMS is present
uint8_t AGS::checkCallAndSMS()
{
char g;
String gcmd;
uint8_t gindex;
//check call presence
while (Serial.available()>0)
{
gcmd += g;
if (g == '\n')
{
gcmd = gcmd.substring(0, gcmd.length() - 2);
gindex = gcmd.indexOf('+');
if (gcmd.substring(gindex+1,gindex+6) == "CLIP:")
{
//+CLIP: "420739822476"
//cut +CLIP:
//check if + is present and parse according this character
gcmd = gcmd.substring(gindex+6);
if (gcmd.indexOf('+') != -1)
{
gindex = gcmd.indexOf('+');
gcmd = gcmd.substring((gindex + 1), (gindex + 13));
}
//parse according first " character
else
{
gindex = gcmd.indexOf('"');
gcmd = gcmd.substring((gindex + 1), (gindex + 13));
}
callInProgress = false;
number = gcmd;
return 1;
}
else
{
return 0;
}
}
}
gcmd = "";
//test SMS presence
clrSIMbuffer();
Serial.println("AT+CMGR=1");
delay(100);
//echo surpress
while (Serial.available()>0)
{
g = Serial.read();
gcmd += g;
if (g == '\n')
{
gcmd = "";
break;
}
}
//read first line with command response
while (Serial.available()>0)
{
g = Serial.read();
gcmd += g;
if (g == '\n')
{
if (gcmd.substring(0, 2) == "OK") return 0;
if (gcmd.substring(0, 5) == "+CMGR")
{
//first + "+CMGR"
gindex = gcmd.indexOf('+');
if (gindex < 0) return 0;
//cut off first + character
gcmd = gcmd.substring(gindex + 1);
//sender number
gindex = gcmd.indexOf('+');
number = gcmd.substring(gindex + 1, gindex + 13);
//Serial.println("From:" + number);
//read SMS content
gcmd = "";
delay(50);
while (Serial.available()>0)
{
g = Serial.read();
gcmd += g;
if ((g == '\n') && (gcmd.length()>2))
{
gcmd = gcmd.substring(0, gcmd.length() - 2);
//Serial.println("SMS Content:" + gcmd);
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
clrSIMbuffer();
SMScontent = gcmd;
return 2;
}
}
gcmd = "";
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
clrSIMbuffer();
//get timeStamp
//actTime = timeStamp();
return 0;
}
}
}
//get timeStamp
//actTime = timeStamp();
return 0;
}
//Call End
void AGS::callEnd()
{
sendATcommand("ATH","OK",2000);
return;
}
//Make call
void AGS::makeCall(String callNumber)
{
Serial.print("ATD ");
Serial.print(callNumber);
Serial.println(";");
clrSIMbuffer();
}
//Get last Sender or A-party number
String AGS::getNumber()
{
return number;
}
String AGS::getListOfContacts(){
String b;
Serial.println("AT+CPBF");
//delay(600);
while (Serial.available()>0){
b = Serial.readString();
}
return b;
}
//get SMS content
String AGS::getSMSContent()
{
return SMScontent;
}
/*
//Get Provider Name
String AGS::getProviderName()
{
providerName = sendATcommandResponse("AT+COPS?", "+COPS:", 1000, 20);
tempIndex = providerName.indexOf('"');
providerName = providerName.substring(tempIndex + 1);
tempIndex = providerName.indexOf('"');
providerName = providerName.substring(0, tempIndex);
return providerName;
}*/
/*
//Get signal quality
String AGS::getQuality()
{
GSMsignal = sendATcommandResponse("AT+CSQ", "+CSQ:", 1000, 3);
return GSMsignal;
}
//Send SMS
*/
void AGS::sendSMS(String number, String sms)
{
clrSIMbuffer();
Serial.println("AT+CMGS=\"" + number + "\"");
delay(200);
//toSerial();
Serial.println(sms); // message to send
delay(100);
Serial.write((char)26); //CTRL+Z
delay(100);
Serial.println();
delay(100);
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
sendATcommand("AT+CMGD=1", "OK", 2000);
delay(500);
clrSIMbuffer();
}
//Clear SIM800 buffer
void AGS::clrSIMbuffer()
{
while (Serial.available()>0)
{
delay(1);
Serial.read();
}
}
//Check if SIM800 is attached to GSM
bool AGS::isConnected()
{
if ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 1) return true;
else return false;
}
Kód: Vybrat vše
//Knižnice
#include "ArduinotechGSMShield1.h"
#include<string.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//def. pinov
#define RELE 8
// def. datoveho typu
bool pirState = LOW;
int val = 0;
int PIR = 3;
float temp;
uint8_t infoStatus;
AGS modul(1);
String number;
String text;
String list;
String number1;
bool stav = false;
//bool sprava = false;
bool ALARM = false;
//___________________________________________________________________________________
void setup()
{
pinMode(PIR, INPUT); // PIR
pinMode(RELE,OUTPUT);
//digitalWrite(RELE,HIGH);
modul.begin();
digitalWrite(RELE,HIGH);
delay(1000);
digitalWrite(RELE,LOW);
list = modul.getListOfContacts();
}
void loop() {
infoStatus = modul.checkCallAndSMS();
if(infoStatus == 1) digitalWrite(RELE,HIGH);
}