• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Sterowanie przekaźnikiem za pomocą SMS (Arduino UNO + moduł GSM)
#6
Dopiero niedawno dotarło do mnie kilka podstawowych kwestii:
- komunikacja ARDUINO - MODUŁ SIM może odbywać się bezpośrednio z komendami AT albo z wykorzystaniem biblioteki np. GSM.h 
- jeśli komunikacja przy użyciu komend AT, to albo w trybie tekstowym albo PDU. Tryb tekstowy jest łatwiejszy.

Dla laików takich jak ja polecam:
http://www.developershome.com/sms/smsIntro.asp
http://www.jarzebski.pl/arduino/komponen...im908.html

Zaktualizowany cel mam zatem taki, żeby napisać kod z komendami AT w trybie tekstowym (bez biblioteki GSM.h) , dzięki któremu będę mógł SMSem o konkretnej treści włączyć np. diodę na pinie 13.

Dotarłem do ciekawego kodu, w którym chyba jest to, co potrzebuję:
https://www.cooking-hacks.com/forum/view...=43&t=8183

Kod:
int8_t answer;
int x;
int onModulePin= 2;
char aux_string[30];
char SMS[200];
float fSMS;

char data[20];

void setup(){

   pinMode(onModulePin, OUTPUT);
   Serial.begin(115200);  

   Serial.println("Starting...");
   power_on();

}


void loop(){


Serial.println("Setting SMS mode...");
   sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
   sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000);    // selects the memory

   answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);    // reads the first SMS (since all other messages are deleted from previous delete statment)
   if (answer == 1)
   {
       answer = 0;
       while(Serial.available() == 0);
       // this loop reads the data of the SMS
       do{
           // if there are data in the UART input buffer, reads it and checks for the asnwer
           if(Serial.available() > 0){    
               SMS[x] = Serial.read();
               x++;
               // check if the desired answer (OK) is in the response of the module
               if (strstr(SMS, "OK") != NULL)    
               {
                   answer = 1;
               }
           }
       }while(answer == 0);    // Waits for the asnwer with time out
       
       SMS[x] = '\0';
       
 // parse the string
   strtok(SMS, ",");
   strtok(NULL, ",");
   strtok(NULL, ",");
   strtok(NULL, "\n");
   strcpy(data,strtok(NULL, ""));
 
   Serial.println(SMS);    
       
   fSMS= atof (data); // Convert the char array to floating point
   Serial.println(fSMS);
   
   }
   else
   {
       Serial.print("error ");
       Serial.println(answer, DEC);
   }
   
   Serial.println("AT+CMGDA= \"DEL ALL\""); //delete all messages from SIM card
   delay(10000);
   
}

void power_on(){

   uint8_t answer=0;
   
   // checks if the module is started
   answer = sendATcommand("AT", "OK", 2000);
   if (answer == 0)
   {
       // power on pulse
       digitalWrite(onModulePin,HIGH);
       delay(3000);
       digitalWrite(onModulePin,LOW);
   
       // waits for an answer from the module
       while(answer == 0){     // Send AT every two seconds and wait for the answer
           answer = sendATcommand("AT", "OK", 2000);    
       }
   }
   
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

   uint8_t x=0,  answer=0;
   char response[100];
   unsigned long previous;

   memset(response, '\0', 100);    // Initialice the string

   delay(100);

   while( Serial.available() > 0) Serial.read();    // Clean the input buffer

   Serial.println(ATcommand);    // Send the AT command


       x = 0;
   previous = millis();

   // this loop waits for the answer
   do{
       // if there are data in the UART input buffer, reads it and checks for the asnwer
       if(Serial.available() != 0){    
           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));    

   return answer;
}


Niestety blokuje mnie kilka zasadniczych kwestii, których cały czas nie rozumiem:
1. Jak przekazać do Arduino polecenie, by zakończył przenoszenie wiadomości SMS do tablicy z ostatnim znakiem tej wiadomości - gdy nie jest znana jej długość
2. Jak wyłuskać z polecenia "AT+CMGR=1" samą treść wiadomości - czyli jak dokładnie działa parsowanie z wykorzystaniem strtok?
3.  Jak dokładnie działa funkcja strstr - czyli jak porównać tablicę znaków ze zdefiniowanym wcześniej łańcuchem?
4. Dlaczego do sprawdzenia zawartości bufora w pamięci raz są stosowane polecenia IF a innym razem WHILE - jak w powyższym kodzie: 
if(Serial.available() > 0){  
while(Serial.available() == 0);

Będę bardzo wdzięczny za pomoc!
Marek
 
Odpowiedź
  


Wiadomości w tym wątku
RE: Sterowanie przekaźnikiem za pomocą SMS (Arduino UNO + moduł GSM) - przez MarJanPol - 07-02-2018, 21:29

Skocz do:


Przeglądający: 1 gości