uprava kniznice

Odpovědět
sstann
Příspěvky: 23
Registrován: 02 pro 2017, 20:33
Reputation: 0

uprava kniznice

Příspěvek od sstann » 14 říj 2020, 15:44

Nazdar, mam zapojenie v ktorom je sim800l zapojený priamo na piny RX,Tx Atmega328p. Nahrajem program a nasledne pripojím na piny rx,tx sim800l.
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;
}
Moj kod pre arduino:

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

sstann
Příspěvky: 23
Registrován: 02 pro 2017, 20:33
Reputation: 0

Re: uprava kniznice

Příspěvek od sstann » 14 říj 2020, 18:59

Vyzerá to tak že problém je s funkviou CallandSMS(), po zavolaní na modul funkcia nevyhodnotí ci ide o sms alebo hovor

ondraN
Příspěvky: 932
Registrován: 08 srp 2019, 20:01
Reputation: 0

Re: uprava kniznice

Příspěvek od ondraN » 14 říj 2020, 20:06

Několikrát jsem místo Software Serial použil normální Serial a nikdy jsem nenarazil na problém (opačně jsem narazil). Zkusil bych ověřit, jestli se s tou změnou omylem nezměnilo i něco jiného. Nejlépe v celé knihovně, protože chyba může byt do metody klidně zavlečená odjinud.

sstann
Příspěvky: 23
Registrován: 02 pro 2017, 20:33
Reputation: 0

Re: uprava kniznice

Příspěvek od sstann » 14 říj 2020, 22:11

Ano, nahradit to problem nieje, kedze povodne procesor komunikoval so SIM800l pomocou software serial a komunikacia atmega <-> PC bola cez Serial,
slúžila na aktualny výpis stavu. Ale kedze moje zapojenie my nedovoluje vyuzit software serial a SIM800L som pripojil priamo na RX,TX piny(0,1) tak som vymazal všetku komunikáciu, ktora bola cez Serial a komunikáciu cez software serial som nahradil Serialom napr. SIM800.println("AT+SAPBR=1,1"); som nahradil Serial.println("AT+SAPBR=1,1");

Dufam, že som to napísal zrozumiteľne. :)

ondraN
Příspěvky: 932
Registrován: 08 srp 2019, 20:01
Reputation: 0

Re: uprava kniznice

Příspěvek od ondraN » 15 říj 2020, 07:33

Přesně tak jsem to pochopil. Jen jsem chtěl naznačit, že když to po změně nefunguje, tak je třeba zjistit, jestli třeba nedošlo i k nějaké nechtěné změně. Při použití Najdi/nahraď se to stát může celkem snadno. Jestli změna proběhla OK, tak to bude velký problém, zjistit zdroj rozdílného chování, obvzláště při nemožnosti debugování kódu. Jedna z odlišností je třeba to, že při použití HW serial, není při 8MHz krystalu přesně dodržena baud rate (chyba je cca 0,7%) a HW zpracování je přísnější na správné časování přenosu. SW serial je v tomhle flexibilnější a dokáže zpracovat i více odchýlené časování. To může, ale nemusí být problém.

analytik
Příspěvky: 148
Registrován: 26 čer 2020, 12:48
Reputation: 0
Bydliště: Děčín

Re: uprava kniznice

Příspěvek od analytik » 15 říj 2020, 07:59

Ajťácké pravidlo: Každá oprava zanese do kódu novou chybu!

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 3 hosti