Bonjour à tous et à toutes.
Je suis en train de réaliser un tracker GPS pour voiture, moto et autres... Oui, encore un
Mon code fonctionne à merveille, quand j'envoie un sms au SIM808, il m'en renvoie un avec les coordonnées GPS. Par contre, dans void loop(), si je dé-commente deplacement(), le module ne reçois plus les sms que je lui envoie. Cette fonction est sensée envoyer un sms au propriétaire du véhicule pour lui indiquer que celui ci se déplace. Voici mon code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
String textMessage;
String coord;
String num;
String numero;
String mouvement;
bool mouv = false;
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void setup(){
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
delay(1000);
digitalWrite(5, HIGH);
delay(2000);
pinMode(5, INPUT);
delay(1000);
mySerial.begin(9600);
Serial.begin(9600);
delay(100);
atcommand();
}
void atcommand(){
mySerial.println ("AT+CPIN=1234"); // Code PIN de la carte SIM FREE
delay (200);
mySerial.println ("AT+CGNSPWR=1"); // Mise en fonction du gps
delay (200);
mySerial.println("AT+CMGF=1"); //Mode texte
delay(1000);
mySerial.println("AT+CNMI=2,2,0,0,0"); // Actication de la réception SMS
delay(1000);
}
void loop(){
if (mySerial.available()>0){
Serial.write(mySerial.read());
textMessage = mySerial.readString();
num = getValue(textMessage, ':' ,1);
numero = getValue(num, '"' ,1);
Serial.println(numero);
}
if (textMessage.indexOf("Track")>=0){
textMessage = "";
SendMessage();
}
if (textMessage.indexOf("Init")>=0){
mouv = false;
textMessage = "";
initialisation();
}
//deplacement();
}
void SendMessage(){
mySerial.println("AT+CGPSINF=2");
coord = mySerial.readString();
String lati = getValue(coord, ',' ,2);
String longi = getValue(coord, ',' ,4);
//String Vitesse = getValue(coord, "," ,6);
Serial.println(coord);
mySerial.println("AT+CMGS=\""+numero+"\"\r");
delay(1000);
mySerial.println("Votre vehicule se trouve ici:\n https://www.google.com/maps/place/" + lati + "," + longi); // Message
delay(100);
mySerial.println((char)26);
delay(1000);
}
void deplacement(){
mySerial.println("AT+CGPSINF=2");
mouvement = mySerial.readString();
String vite = getValue(mouvement, "," ,6);
int vitesse = vite.toInt();
Serial.println(vitesse);
if (vitesse>=30 && mouv==false){
mySerial.println("AT+CMGS=\""+numero+"\"\r");
delay(1000);
mySerial.println("ALERTE\n Votre vehicule est en mouvement"); // Message
delay(100);
mySerial.println((char)26);
delay(1000);
mouv = true;
}
}
void initialisation(){
mySerial.println("AT+CMGS=\""+numero+"\"\r");
delay(1000);
mySerial.println("Votre tracker a ete reinitialise"); // Message
delay(100);
mySerial.println((char)26);
delay(1000);
}
Je n'en suis pas sûr, mais je pense que cette fonction prend trop de ressources.
Une autre petite chose, comment mettre du texte accentué dans les SMS?