djbouns:
Donc dans le serial le sms est bien lu mais la réponse recu par sms est toujours la même quelque soit le sms envoyer commencant pas ssid
La config est la suivante : SSID = d
Je me suis re penché sur la partie SMS.
Je suis arrivé a avoir une réponse correcte a mes demandes.
Voyer vous des incohérences ? si non, je l’inclus dans mon code principale
#define gsm Serial1
char * _SSID ;
char * _MDP ;
const byte ctrlZ = 0x1A;
const char * ctrlZString = "\x1A";
const char * escapeString = "\x1B";
const char * ATString = "AT";
const char * OKShortString = "OK";
const char * OKLongString = "\r\nOK\r\n";
const byte max_SSID = 50;// prévoir la bonne longueur
char copie_SSID[max_SSID+1];
const byte max_MDP = 50;// prévoir la bonne longueur
char copie_MDP[max_MDP+1];
const uint32_t oneSecond = 1000ul;
const byte maxMessageSize = 200;
char GSM_MessageLine[maxMessageSize + 1]; // +1 pour le '\0' à la fin de la c-string
// --------------------------------------
// gsmPrintlnAndWaitATCommand executes an AT commmand by adding at the end a CR LF
// then it checks if endMarker string is receivedon the GSM Serial port
// for max duration ms returns a boolean stating if the marker was found
// with the verbose option, the output from the GSM is also printed to Serial
// --------------------------------------
boolean gsmPrintlnAndWaitATCommand(const char * command, const char * endMarker, unsigned long duration, boolean verbose)
{
gsm.println(command);
return waitForString(endMarker, duration, verbose);
}
boolean gsmPrintAndWaitATCommand(const char * command, const char * endMarker, unsigned long duration, boolean verbose)
{
gsm.print(command);
return waitForString(endMarker, duration, verbose);
}
// --------------------------------------
// gsmPrintATCommand or gsmpWriteATCommand is used when you don't want to send the CR LF
// at the end of the commmand line; use it to build up a multi part command
// same syntax as print as they are Variadic Macros using print or write
// --------------------------------------
#define gsmPrintATCommand(...) gsm.print(__VA_ARGS__)
#define gsmPrintlnATCommand(...) gsm.println(__VA_ARGS__)
#define gsmWriteATCommand(...) gsm.write(__VA_ARGS__)
// --------------------------------------
// read a line from gsm, ignore '\r' and returns true if '\n' is found
// --------------------------------------
boolean getGSMLine()
{
static byte indexMessage = 0;
boolean incomingMessage = true;
while (gsm.available() && incomingMessage) {
int c = gsm.read();
if (c != -1) {
switch (c) {
case '\n':
GSM_MessageLine[indexMessage] = '\0'; // trailing NULL char for a correct c-string
indexMessage = 0; // get ready for next time
incomingMessage = false;
break;
case '\r': // don't care about this one
break;
default:
if (indexMessage <= maxMessageSize - 1) GSM_MessageLine[indexMessage++] = (char) c; // else ignore it..
break;
}
}
}
return !incomingMessage;
}
// --------------------------------------
// waitForString wait max for duration ms whilst checking if endMarker string is received
// on the GSM Serial port returns a boolean stating if the marker was found
// --------------------------------------
boolean waitForString(const char * endMarker, unsigned long duration, boolean verbose)
{
int localBufferSize = strlen(endMarker);
char localBuffer[localBufferSize];
int index = 0;
boolean endMarkerFound = false;
unsigned long currentTime;
memset(localBuffer, '\0', localBufferSize); // clear buffer
currentTime = millis();
while (millis() - currentTime <= duration) {
if (gsm.available() > 0) {
if (index == localBufferSize) index = 0;
localBuffer[index] = (uint8_t) gsm.read();
if (verbose) Serial.print((char) localBuffer[index]);
endMarkerFound = true;
for (int i = 0; i < localBufferSize; i++) {
if (localBuffer[(index + 1 + i) % localBufferSize] != endMarker[i]) {
endMarkerFound = false;
break;
}
}
index++;
}
if (endMarkerFound) break;
}
return endMarkerFound;
}
void dieHere()
{
while (true);
}
void setup() {
Serial.begin(115200);
gsm.begin(9600); // suivant votre config, essayez 9600 19200 38400 57600 74880 115200
// prévoir éventuellement AT+CPIN="1234" pour rentrer le PIN
if (gsmPrintlnAndWaitATCommand(ATString, OKLongString, 5 * oneSecond, false)) {
Serial.println(F("GPRS Modem OK"));
} else {
Serial.println(F("Erreur GPRS Modem"));
dieHere();
}
if (!gsmPrintlnAndWaitATCommand("AT+GSV", OKLongString, oneSecond, true)) {
Serial.print(F("Erreur Version"));
dieHere();
}
if (!gsmPrintlnAndWaitATCommand("ATE0", OKLongString, oneSecond, false)) {
Serial.print(F("Erreur Echo off"));
dieHere();
}
if (!gsmPrintlnAndWaitATCommand("ATV1", OKLongString, oneSecond, false)) {
Serial.print(F("Erreur Message en Verbal"));
dieHere();
}
// on passe en les SMS en mode texte
if (!gsmPrintlnAndWaitATCommand("AT+CMGF=1", OKLongString, oneSecond, true)) {
Serial.print(F("Error AT+CMGF"));
// dieHere(); // on ne meurt pas ici mais AT+CNMI non configurgé
}
// on passe en mode routage des SMS vers le terminal
if (!gsmPrintlnAndWaitATCommand("AT+CNMI=1,2,0,0,0", OKLongString, oneSecond, true)) {
Serial.print(F("Error AT+CNMI"));
// dieHere(); // on ne meurt pas ici mais AT+CNMI non configurgé
}
Serial.println("Initialisation terminé");
}
boolean sendSMS(const char * noDeTel, const char * messageAEnvoyer)
{
boolean SMSenvoye = false;
gsmPrintATCommand("AT+CMGS=\"");
gsmPrintATCommand(noDeTel);
if (gsmPrintlnAndWaitATCommand("\"", ">", oneSecond, false)) {
gsmPrintlnATCommand(messageAEnvoyer);
SMSenvoye = gsmPrintlnAndWaitATCommand(ctrlZString, OKShortString, 5 * oneSecond, false);
} else {
gsmPrintlnATCommand(escapeString);
}
return SMSenvoye;
}
void loop()
{
if (getGSMLine()) {
// ********************************************
// TEST RECEPTION D'UN SMS
// +CMT: "+33620000000",,"01/03/22,16:33:31+00"<CRLF>
// texte bla bla bla<CRLF>
// ********************************************
if (!strncmp(GSM_MessageLine, "+CMT:", 5)) {// 5 parce que "+CMT:" c'est 5 caractères, on compare le début de la chaîne
const byte maxDigitsPhoneNb = 30;
char phoneNb[maxDigitsPhoneNb + 1];
char *ptr = strtok(GSM_MessageLine, "\"");
if (ptr) {
ptr = strtok(NULL, "\"");
strncpy(phoneNb, ptr, maxDigitsPhoneNb);
phoneNb[maxDigitsPhoneNb] = '\0';
// *** DEBUG *** POUR VOIR CE QU'IL SE PASSE
Serial.print(F("SMS de = ")); Serial.println(phoneNb);
// ***
while (!getGSMLine()); // on attend le texte du SMS
// *** DEBUG *** POUR VOIR CE QU'IL SE PASSE
Serial.print(F("Message = [")); Serial.print(GSM_MessageLine); Serial.println(F("]"));
// ***
if (!strncmp(GSM_MessageLine, "CONFIG", 6)) { // 6 parce que 6 caractères dans le mot "CONFIG"
// on a rerçu un SMS nous demandant la config, on construit la réponse
strcpy(GSM_MessageLine, "La config est la suivante:\r\nSSID = ");
strcat(GSM_MessageLine, _SSID);
strcat(GSM_MessageLine, "\r\nMot de Passe = ");
strcat(GSM_MessageLine, _MDP);
sendSMS(phoneNb, GSM_MessageLine);
}
else if (!strncmp(GSM_MessageLine, "Ssid", 4)) { // 6 parce que 6 caractères dans le mot "CONFIG"
_SSID = GSM_MessageLine + 5; // Notez que c'est aussi simple d'écrire _SSID = GSM_MessageLine+6;...
strncpy(copie_SSID, _SSID,max_SSID);
copie_SSID[max_SSID] = '\0';
_SSID = copie_SSID;
strcpy(GSM_MessageLine, "nouveau SSID recu = ");
strcat(GSM_MessageLine, _SSID);
sendSMS(phoneNb, GSM_MessageLine);
}
else if (!strncmp(GSM_MessageLine, "Mdp", 3)) { // 6 parce que 6 caractères dans le mot "CONFIG"
_MDP = GSM_MessageLine + 4; // Notez que c'est aussi simple d'écrire _SSID = GSM_MessageLine+6;...
strncpy(copie_MDP, _MDP,max_MDP);
copie_MDP[max_MDP] = '\0';
_MDP = copie_MDP;
strcpy(GSM_MessageLine, "nouveau MDP recu = ");
strcat(GSM_MessageLine, _MDP);
sendSMS(phoneNb, GSM_MessageLine);
}
else {
sendSMS(phoneNb, "Pas compris");
}
}
}
}
}