@em1lio189 La libreria che sto usando è TinyGPS.h.
@Gugliemo: per aumentare il buffer di ricezione devo crearmi un array di char? Una cosa del tipo:
char incomingSms[128] = {'\0'};
for (int i = 0; i < 128; i++)
{
incomingSms[i] = gsmSerial.read();
}
Ora che mi viene in mente, mettendo un print su gsmSerial.available() questo mi ritorna 1, che è il primo carattare che vedo a video, invece che la lunghezza esatta.
Non so se ci sono altri problemi comunque posto il codice completo.
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <String.h>
#include<stdlib.h>
unsigned long fix_age;
SoftwareSerial GPS(12,11);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT, LON;
//Receiver Init
int inputA = 4;
int inputB = 3;
int inputC = 5;
int inputD = 2;
//Led Init
int LED_PIN = 6;
boolean ledState = false;
//Mills
long previousMillis = 0;
long interval = 5000; // interval at which to blink (milliseconds)
//GSM Init
SoftwareSerial gsmSerial(7, 8);
String textSms = "";
char incoming_char=0;
boolean sendSMS = false;
unsigned long start = 0;
unsigned long startCheckCoordinate = 0;
void setup() {
GPS.begin(9600);
gsmSerial.begin(9600);
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(inputA, INPUT);
pinMode(inputB, INPUT);
pinMode(inputC, INPUT);
pinMode(inputD, INPUT);
LAT_PREVIOUS = "";
LON_PREVIOUS = "";
startCheckCoordinate = millis();
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(inputA) == HIGH && !ledState)
{
long diff = currentMillis - previousMillis;
if(diff > interval || previousMillis == 0)
{
previousMillis = currentMillis;
Serial.println("A");
ledState = true;
buzerState = true;
}
}else if (digitalRead(inputA) == HIGH && ledState)
{
long diff = currentMillis - previousMillis;
if(diff > interval || previousMillis == 0)
{
previousMillis = currentMillis;
Serial.println("A");
ledState = false;
buzerState = true;
}
}
if (ledState)
{
ledBlink();
CheckCoordinateSendTextMessage();
}else
{
digitalWrite(LED_PIN, LOW);
}
//receive SMS
gsmSerial.listen();
if (gsmSerial.available() >0)
{
Serial.println(gsmSerial.available());
while(gsmSerial.available())
{
incoming_char=gsmSerial.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char); //Print the incoming character to the terminal.
textSms = textSms + String(incoming_char);
}
}
if ( textSms != "")
{
Serial.println("-----------------------");
Serial.println("<"+textSms+">");
textSms = "";
}
}
void getGPS(){
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000)
{
if (feedgps ()){
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
}
bool feedgps(){
while (GPS.available())
{
if (gps.encode(GPS.read()))
{
return true;
}
}
return 0;
}
void gpsdump(TinyGPS &gps)
{
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat;
LON = lon;
{
feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
}
}
void ledBlink()
{
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 800)
{
digitalWrite(LED_PIN, HIGH);
}
start = millis();
while (millis() - start < 400)
{
digitalWrite(LED_PIN, LOW);
}
}
void CheckCoordinateSendTextMessage()
{
while(millis() - startCheckCoordinate > 10000)
{
int counter=0;
GPS.listen();
for (;;)
{
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
long Latitude, Longitude;
// retrieves /- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
getGPS();
Serial.print("Latitude : ");
Serial.print(LAT/1000000,7);
Serial.print(" :: Longitude : ");
Serial.println(LON/1000000,7);
counter++;
Serial.print(counter);
if (counter<2)
{
continue;
}
if (LAT == 0 && LON == 0)
{
break;
}
gsmSerial.listen();
gsmSerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay(1500);
gsmSerial.println("AT+CMGS = \"NUMBER\"");//send sms message, be careful need to add a country code before the cellphone number
delay(1000);
gsmSerial.println("A test message1!");//the content of the message
delay(1000);
gsmSerial.println((char)26);//the ASCII code of the ctrl+z is 26
delay(1000);
gsmSerial.println();
delay(1000);
}
counter=0;
break;
}
startCheckCoordinate = millis();
}
}
String SendUSSD()
{
String bodySms = "";
textSms = "";
int count = 0;
int countCicle = 0;
while(true)
{
Serial.print("--- ");
Serial.println(count++);
gsmSerial.println("ATD*123#");//dial the number
delay(15);
char c;
for (int i = 0; i < 100; i++) {
while (gsmSerial.available() > 0){ // If there is data, read it and reset
c = (char)gsmSerial.read(); // the counter, otherwise go try again
textSms = textSms + String(c);
}
delay(100);
}
Serial.println("---<"+textSms+">---");
int val1 = textSms.indexOf("+CUSD: 0,\""); //line separator
bodySms = textSms.substring(val1 + 10, textSms.length() - 1);
Serial.println("****<"+bodySms+">****");
if (textSms.indexOf("Euro") == -1)
{
countCicle = countCicle + 1;
if (countCicle == 3)
{
break;
}
}else
{
break;
}
delay(1000);
}
Serial.println("++++<"+bodySms+">+++");
return bodySms;
}