Sim800L loib obj sms
sim800l.cpp@1:2e6fa015734c, 2022-04-30 (annotated)
- Committer:
- schnf30
- Date:
- Sat Apr 30 20:07:22 2022 +0000
- Revision:
- 1:2e6fa015734c
- Parent:
- 0:5c39ec4eaaa1
VERS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
schnf30 | 0:5c39ec4eaaa1 | 1 | |
schnf30 | 0:5c39ec4eaaa1 | 2 | #include "mbed.h" |
schnf30 | 0:5c39ec4eaaa1 | 3 | #include "sim800l.h" |
schnf30 | 0:5c39ec4eaaa1 | 4 | char _DataTmp[maxdata + 1]; // tableau de donnees pour stocker pendant reception |
schnf30 | 0:5c39ec4eaaa1 | 5 | char _Data[maxdata + 1]; // tableau de donnees lorsque toutes les donnees sont recues |
schnf30 | 0:5c39ec4eaaa1 | 6 | char _DataSms[maxdata + 1]; // tableau de donnees lorsque toutes les donnees sont recues |
schnf30 | 0:5c39ec4eaaa1 | 7 | char _Phrase[maxdata + 1]; // pour convertir en phrase avant envoi |
schnf30 | 0:5c39ec4eaaa1 | 8 | Sim800l::Sim800l(PinName tx, PinName rx, PinName resetpin,int baudrate):serial(tx,rx), reset (resetpin) |
schnf30 | 0:5c39ec4eaaa1 | 9 | { |
schnf30 | 0:5c39ec4eaaa1 | 10 | reset.write(0); //reset |
schnf30 | 1:2e6fa015734c | 11 | _DataTmp = new char[maxdata + 1]; // tableau de donnees pour stocker pendant reception |
schnf30 | 1:2e6fa015734c | 12 | _Data = new char[maxdata + 1]; // tableau de donnees lorsque toutes les donnees sont recues |
schnf30 | 1:2e6fa015734c | 13 | _DataSms = new char[maxdata + 1]; // tableau de donnees lorsque toutes les donnees sont recues |
schnf30 | 1:2e6fa015734c | 14 | _Phrase = new char[maxdata + 1]; // pour convertir en phrase avant envoi |
schnf30 | 0:5c39ec4eaaa1 | 15 | _DataTmp[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 16 | _Data[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 17 | _DataPtr = 0; |
schnf30 | 0:5c39ec4eaaa1 | 18 | _DataReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 19 | _SmsReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 20 | _IndexSms = 0; |
schnf30 | 0:5c39ec4eaaa1 | 21 | serial.baud(baudrate); // configure data baud |
schnf30 | 0:5c39ec4eaaa1 | 22 | } |
schnf30 | 0:5c39ec4eaaa1 | 23 | // retourne incdex du sms recu |
schnf30 | 0:5c39ec4eaaa1 | 24 | int Sim800l::IndexSmsRecu(void) |
schnf30 | 0:5c39ec4eaaa1 | 25 | { |
schnf30 | 0:5c39ec4eaaa1 | 26 | return _IndexSms; |
schnf30 | 0:5c39ec4eaaa1 | 27 | } |
schnf30 | 0:5c39ec4eaaa1 | 28 | // return true si une phrase a ete recue |
schnf30 | 0:5c39ec4eaaa1 | 29 | bool Sim800l::dataready(void) |
schnf30 | 0:5c39ec4eaaa1 | 30 | { |
schnf30 | 0:5c39ec4eaaa1 | 31 | return _DataReady; |
schnf30 | 0:5c39ec4eaaa1 | 32 | } |
schnf30 | 0:5c39ec4eaaa1 | 33 | // return phrase recu |
schnf30 | 0:5c39ec4eaaa1 | 34 | char * Sim800l::read(void) |
schnf30 | 0:5c39ec4eaaa1 | 35 | { |
schnf30 | 0:5c39ec4eaaa1 | 36 | _DataReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 37 | return _Data; |
schnf30 | 0:5c39ec4eaaa1 | 38 | } |
schnf30 | 0:5c39ec4eaaa1 | 39 | // recoit les caractere serie et les stocke en tache de fond |
schnf30 | 0:5c39ec4eaaa1 | 40 | void Sim800l::receive() |
schnf30 | 0:5c39ec4eaaa1 | 41 | { |
schnf30 | 0:5c39ec4eaaa1 | 42 | char inChar; |
schnf30 | 0:5c39ec4eaaa1 | 43 | if (serial.readable()) { |
schnf30 | 0:5c39ec4eaaa1 | 44 | inChar = serial.getc(); |
schnf30 | 0:5c39ec4eaaa1 | 45 | // pc.putc(inChar); // pour echo |
schnf30 | 0:5c39ec4eaaa1 | 46 | switch (inChar) { |
schnf30 | 0:5c39ec4eaaa1 | 47 | case '\n' : |
schnf30 | 0:5c39ec4eaaa1 | 48 | if (_DataPtr > 1) { |
schnf30 | 0:5c39ec4eaaa1 | 49 | // pc.printf("-%s/\r\n",_DataTmp); |
schnf30 | 0:5c39ec4eaaa1 | 50 | if (strncmp(_DataTmp,"+CMTI:",6)==NULL) { // recoit sms +CMTI: "SM",3 |
schnf30 | 0:5c39ec4eaaa1 | 51 | if (sscanf(_DataTmp,"+CMTI: \"SM\",%d",&_IndexSms)==1) _SmsReady = true; |
schnf30 | 0:5c39ec4eaaa1 | 52 | } else { // cmde |
schnf30 | 0:5c39ec4eaaa1 | 53 | strcpy(_Data,_DataTmp); |
schnf30 | 0:5c39ec4eaaa1 | 54 | _DataReady = true; |
schnf30 | 0:5c39ec4eaaa1 | 55 | } |
schnf30 | 0:5c39ec4eaaa1 | 56 | } |
schnf30 | 0:5c39ec4eaaa1 | 57 | _DataTmp[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 58 | _DataPtr = 0; |
schnf30 | 0:5c39ec4eaaa1 | 59 | break; |
schnf30 | 0:5c39ec4eaaa1 | 60 | case 0 : |
schnf30 | 0:5c39ec4eaaa1 | 61 | break; |
schnf30 | 0:5c39ec4eaaa1 | 62 | case '\r' : |
schnf30 | 0:5c39ec4eaaa1 | 63 | break; |
schnf30 | 0:5c39ec4eaaa1 | 64 | case ' ' : |
schnf30 | 0:5c39ec4eaaa1 | 65 | if (_DataPtr==1) { |
schnf30 | 0:5c39ec4eaaa1 | 66 | if ( _DataTmp[0]=='>') { |
schnf30 | 0:5c39ec4eaaa1 | 67 | strcpy(_Data,_DataTmp); |
schnf30 | 0:5c39ec4eaaa1 | 68 | _DataReady = true; |
schnf30 | 0:5c39ec4eaaa1 | 69 | _DataTmp[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 70 | _DataPtr = 0; |
schnf30 | 0:5c39ec4eaaa1 | 71 | break; |
schnf30 | 0:5c39ec4eaaa1 | 72 | } |
schnf30 | 0:5c39ec4eaaa1 | 73 | } |
schnf30 | 0:5c39ec4eaaa1 | 74 | default : |
schnf30 | 0:5c39ec4eaaa1 | 75 | _DataTmp[_DataPtr] = inChar; |
schnf30 | 0:5c39ec4eaaa1 | 76 | _DataPtr = _DataPtr + 1; |
schnf30 | 0:5c39ec4eaaa1 | 77 | _DataTmp[_DataPtr] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 78 | } |
schnf30 | 0:5c39ec4eaaa1 | 79 | if (_DataPtr >= maxdata) { // si phrase trop longue vide phrase |
schnf30 | 0:5c39ec4eaaa1 | 80 | _DataTmp[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 81 | _DataPtr = 0; |
schnf30 | 0:5c39ec4eaaa1 | 82 | } |
schnf30 | 0:5c39ec4eaaa1 | 83 | } |
schnf30 | 0:5c39ec4eaaa1 | 84 | } |
schnf30 | 0:5c39ec4eaaa1 | 85 | // fonction appele en fin timerout |
schnf30 | 0:5c39ec4eaaa1 | 86 | void Sim800l::Ftimeout() |
schnf30 | 0:5c39ec4eaaa1 | 87 | { |
schnf30 | 0:5c39ec4eaaa1 | 88 | FinTimeOut = true; |
schnf30 | 0:5c39ec4eaaa1 | 89 | } |
schnf30 | 0:5c39ec4eaaa1 | 90 | // defini time out et le lance |
schnf30 | 0:5c39ec4eaaa1 | 91 | void Sim800l::SetTimeout(float delay) |
schnf30 | 0:5c39ec4eaaa1 | 92 | { |
schnf30 | 0:5c39ec4eaaa1 | 93 | FinTimeOut = false; |
schnf30 | 0:5c39ec4eaaa1 | 94 | _timeout.attach(callback(this,&Sim800l::Ftimeout),delay); |
schnf30 | 0:5c39ec4eaaa1 | 95 | // timeout.attach(callback(this, &Sonar::trigger_toggle), 10.0e-6); |
schnf30 | 0:5c39ec4eaaa1 | 96 | } |
schnf30 | 0:5c39ec4eaaa1 | 97 | // attente reponse dans *reponse jusqua timeout |
schnf30 | 0:5c39ec4eaaa1 | 98 | bool Sim800l::waitreponse(const char *reponse,float timeout) |
schnf30 | 0:5c39ec4eaaa1 | 99 | { |
schnf30 | 0:5c39ec4eaaa1 | 100 | SetTimeout(timeout); |
schnf30 | 0:5c39ec4eaaa1 | 101 | while (FinTimeOut==false) { // attente Ok |
schnf30 | 0:5c39ec4eaaa1 | 102 | if (dataready()) { |
schnf30 | 0:5c39ec4eaaa1 | 103 | if (strcmp(read(),reponse)==0) return true; |
schnf30 | 0:5c39ec4eaaa1 | 104 | } |
schnf30 | 0:5c39ec4eaaa1 | 105 | } |
schnf30 | 0:5c39ec4eaaa1 | 106 | return false; // timeout ecoule donc erreur |
schnf30 | 0:5c39ec4eaaa1 | 107 | } |
schnf30 | 0:5c39ec4eaaa1 | 108 | // envoi commande et attend reponse |
schnf30 | 0:5c39ec4eaaa1 | 109 | bool Sim800l::writecmdandwaitreponse(const char *data, const char *reponse,float timeout) |
schnf30 | 0:5c39ec4eaaa1 | 110 | { |
schnf30 | 0:5c39ec4eaaa1 | 111 | serial.printf("%s",data); |
schnf30 | 0:5c39ec4eaaa1 | 112 | return waitreponse(reponse,timeout); |
schnf30 | 0:5c39ec4eaaa1 | 113 | } |
schnf30 | 0:5c39ec4eaaa1 | 114 | // initialise SIM800L en mode SMS Texte |
schnf30 | 0:5c39ec4eaaa1 | 115 | // configure baud |
schnf30 | 0:5c39ec4eaaa1 | 116 | // Renvoi |
schnf30 | 0:5c39ec4eaaa1 | 117 | // - true tout s'est bien passé |
schnf30 | 0:5c39ec4eaaa1 | 118 | // - false erreur |
schnf30 | 0:5c39ec4eaaa1 | 119 | // cmdecho = true --> copie donne de reponse sim800l vers pc |
schnf30 | 0:5c39ec4eaaa1 | 120 | bool Sim800l::init() |
schnf30 | 0:5c39ec4eaaa1 | 121 | { |
schnf30 | 0:5c39ec4eaaa1 | 122 | bool resultat = true; |
schnf30 | 0:5c39ec4eaaa1 | 123 | _DataTmp[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 124 | _Data[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 125 | _DataPtr = 0; |
schnf30 | 0:5c39ec4eaaa1 | 126 | _DataReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 127 | _SmsReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 128 | _IndexSms = 0; |
schnf30 | 0:5c39ec4eaaa1 | 129 | reset.write(0); //reset |
schnf30 | 0:5c39ec4eaaa1 | 130 | thread_sleep_for(200); |
schnf30 | 0:5c39ec4eaaa1 | 131 | reset.write(1); |
schnf30 | 0:5c39ec4eaaa1 | 132 | serial.attach(callback(this, &Sim800l::receive), RawSerial::RxIrq); // affecte interruption sur Rx |
schnf30 | 0:5c39ec4eaaa1 | 133 | thread_sleep_for(1000); |
schnf30 | 0:5c39ec4eaaa1 | 134 | int i=0; |
schnf30 | 0:5c39ec4eaaa1 | 135 | while (!writecmdandwaitreponse("AT\r\n","OK",timeoutdefaut)) { // on attend 10 x pas plus |
schnf30 | 0:5c39ec4eaaa1 | 136 | if (i++>10) return false; |
schnf30 | 0:5c39ec4eaaa1 | 137 | } |
schnf30 | 0:5c39ec4eaaa1 | 138 | if (!waitreponse("Call Ready",timewaitCallready)) return false; |
schnf30 | 0:5c39ec4eaaa1 | 139 | if (!waitreponse("SMS Ready",timewaitSmsready)) return false; |
schnf30 | 0:5c39ec4eaaa1 | 140 | return writecmdandwaitreponse("AT+CMGF=1\r\n","OK",timeoutdefaut); // Passe sms mode texte |
schnf30 | 0:5c39ec4eaaa1 | 141 | } |
schnf30 | 0:5c39ec4eaaa1 | 142 | bool Sim800l::smsclearall(void) |
schnf30 | 0:5c39ec4eaaa1 | 143 | { |
schnf30 | 0:5c39ec4eaaa1 | 144 | return writecmdandwaitreponse("AT+CMGDA=\"DEL ALL\"\r\n","OK",timewaitSmsallclear); // efface sms |
schnf30 | 0:5c39ec4eaaa1 | 145 | |
schnf30 | 0:5c39ec4eaaa1 | 146 | } |
schnf30 | 0:5c39ec4eaaa1 | 147 | bool Sim800l::smsready(void) |
schnf30 | 0:5c39ec4eaaa1 | 148 | { |
schnf30 | 0:5c39ec4eaaa1 | 149 | bool _SmsReadytemp = _SmsReady; |
schnf30 | 0:5c39ec4eaaa1 | 150 | _SmsReady = false; |
schnf30 | 0:5c39ec4eaaa1 | 151 | return _SmsReadytemp; |
schnf30 | 0:5c39ec4eaaa1 | 152 | } |
schnf30 | 0:5c39ec4eaaa1 | 153 | bool Sim800l::sendsms(const char *phonenumber,const char *data) |
schnf30 | 0:5c39ec4eaaa1 | 154 | { |
schnf30 | 0:5c39ec4eaaa1 | 155 | if (!writecmdandwaitreponse("AT+CMGF=1\r\n","OK",timeoutdefaut)) return false; // Passe sms mode texte |
schnf30 | 0:5c39ec4eaaa1 | 156 | sprintf(_Phrase,"AT+CMGS=\"%s\"\r\n",phonenumber); |
schnf30 | 0:5c39ec4eaaa1 | 157 | if (!writecmdandwaitreponse(_Phrase,">",timeoutsendsms)) return false; // ouvre sms |
schnf30 | 0:5c39ec4eaaa1 | 158 | sprintf(_Phrase,"%s%c\r\n",data,26); |
schnf30 | 0:5c39ec4eaaa1 | 159 | return writecmdandwaitreponse(_Phrase,"OK",timeoutsendsms); // envoi message |
schnf30 | 0:5c39ec4eaaa1 | 160 | } |
schnf30 | 0:5c39ec4eaaa1 | 161 | // attente r eponse dans *reponse |
schnf30 | 0:5c39ec4eaaa1 | 162 | bool Sim800l::waitsmsreponse(const char *reponse,float timeout) |
schnf30 | 0:5c39ec4eaaa1 | 163 | { |
schnf30 | 0:5c39ec4eaaa1 | 164 | char DataSmsOk = 0; |
schnf30 | 0:5c39ec4eaaa1 | 165 | _DataSms[0] = 0; |
schnf30 | 0:5c39ec4eaaa1 | 166 | SetTimeout(timeout); |
schnf30 | 0:5c39ec4eaaa1 | 167 | while (FinTimeOut==false) { // attente Ok |
schnf30 | 0:5c39ec4eaaa1 | 168 | if (dataready()) { |
schnf30 | 0:5c39ec4eaaa1 | 169 | // pc.printf("-%s --> %d:%s/\r\n",_Data,DataSmsOk,_DataSms); |
schnf30 | 0:5c39ec4eaaa1 | 170 | if (strcmp(read(),reponse)==0) return true; |
schnf30 | 0:5c39ec4eaaa1 | 171 | if (strncmp(_Data,"+CMGR:",6)==NULL) DataSmsOk = 1; |
schnf30 | 0:5c39ec4eaaa1 | 172 | else if (DataSmsOk!=0) { |
schnf30 | 0:5c39ec4eaaa1 | 173 | if (DataSmsOk > 1) strcat(_DataSms,","); |
schnf30 | 0:5c39ec4eaaa1 | 174 | strcat(_DataSms,_Data); |
schnf30 | 0:5c39ec4eaaa1 | 175 | DataSmsOk = 2; |
schnf30 | 0:5c39ec4eaaa1 | 176 | } |
schnf30 | 0:5c39ec4eaaa1 | 177 | // pc.printf("-%s --> %d:%s/\r\n",_Data,DataSmsOk,_DataSms); |
schnf30 | 0:5c39ec4eaaa1 | 178 | } |
schnf30 | 0:5c39ec4eaaa1 | 179 | } |
schnf30 | 0:5c39ec4eaaa1 | 180 | return false; // timeout ecoule donc erreur |
schnf30 | 0:5c39ec4eaaa1 | 181 | } |
schnf30 | 0:5c39ec4eaaa1 | 182 | char *Sim800l::readtextesms() |
schnf30 | 0:5c39ec4eaaa1 | 183 | { |
schnf30 | 0:5c39ec4eaaa1 | 184 | return _DataSms; |
schnf30 | 0:5c39ec4eaaa1 | 185 | } |
schnf30 | 0:5c39ec4eaaa1 | 186 | bool Sim800l::readsms(int index) |
schnf30 | 0:5c39ec4eaaa1 | 187 | { |
schnf30 | 0:5c39ec4eaaa1 | 188 | serial.printf("AT+CMGR=%d,0\r\n",index); |
schnf30 | 0:5c39ec4eaaa1 | 189 | return waitsmsreponse("OK",timeoutreadsms); |
schnf30 | 0:5c39ec4eaaa1 | 190 | } |