BayEOS-Arduino  1.8.0_0.0.4
BayTCPSim900.cpp
1 #include "BayTCPSim900.h"
2 
3 void RTC_SIM900::adjust(const DateTime& dt) {
4  last_millis=millis();
5  offset = dt.get();
6 }
7 
8 DateTime RTC_SIM900::now() {
9  unsigned long diff=(millis()-last_millis)/1000L;
10  if(diff>1000000L){
11  offset+=diff;
12  last_millis=millis();
13  diff=0;
14  }
15  return offset+diff;
16 }
17 
18 uint8_t BayGPRSInterface::changeIPR(long baud) {
19  _baud = baud;
20  long t_baud[] = { baud, 9600, 38400, 57600 };
21  for (uint8_t i = 0; i < 4; i++) {
22  i_begin(t_baud[i]);
23  skipChars();
24  printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode!
25  delay(100);
26  println();
27  printP("AT+IPR=");
28  println(_baud);
29  if (!wait_forOK(200)) {
30  i_end();
31  i_begin(_baud);
32  return 0;
33  }
34  i_end();
35  }
36  return 1;
37 
38 }
39 uint8_t BayGPRSInterface::init(uint8_t unlock_only){
40  uint8_t count=0;
41  init_start:
42  uint8_t i=0;
43  printP("AT"); //Wake up
44  delay(200);
45  println();
46  wait_forOK(1000);
47  printlnP("AT"); //Check communication
48  if(! wait_forOK(200)){
49  //communication ok!
50  printlnP("ATE0"); //Command echo off
51  wait_forOK(500);
52  printlnP("AT+CSCLK=2"); //Auto-Sleepmode
53  wait_forOK(500);
54  //Check PIN
55  printlnP("AT+CPIN?");
56  while(wait_forPGM(PSTR("+CPIN: "),5000,7,_base64buffer)){
57  printlnP("AT");
58  wait_forOK(200);
59  printlnP("AT+CFUN=0");
60  //Disable
61  wait_forOK(10000);
62  printlnP("AT+CFUN=1");
63  //delay(2000);
64  //Enable
65  wait_forOK(10000);
66  printlnP("AT");
67  wait_forOK(200);
68  printlnP("AT+CPIN?");
69  i++;
70  if(i>2) return 6;
71  }
72  if(_base64buffer[5]=='U') return 3; //SIM: PUK
73  if(_base64buffer[5]=='I'){ //SIM: PIN
74  printlnP("AT");
75  wait_forOK(200);
76  printP("AT+CPIN=\"");
77  print(_pin);
78  println("\"");
79  if(wait_forOK(30000)) {
80  return 2; //Wrong PIN
81  }
82  wait_for("SMS Ready",(unlock_only?5000:60000));
83  }
84  //Return here - Moden will try to connect and attach in the background!
85  if(unlock_only) return 0;
86  // Waiting for Modem to Connect
87  for(i=0;i<127;i++){
88  if(isRegistered()) break;
89  delay(500);
90  }
91  if(i==127) return 4;
92  for(i=0;i<127;i++){
93  if(isAttached()) break;
94  delay(500);
95  }
96  if(i==127) return 5;
97  return 0;
98  }
99 
100  softReset();
101  softSwitch();
102  i_begin(_baud);
103  skipChars();
104  printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode!
105  delay(100);
106  println();
107  printP("AT+IPR=");
108  println(_baud);
109  wait_forOK(200);
110  count++;
111 
112  if(count>1) return 1;
113  goto init_start;
114 }
115 
117 #if SIM900_DEBUG
118  Serial.println("softSwitch");
119 #endif
120  pinMode(_powerPin, OUTPUT);
121  digitalWrite(_powerPin,LOW);
122  delay(1000);
123  digitalWrite(_powerPin,HIGH);
124  delay(2500);
125  digitalWrite(_powerPin,LOW);
126  delay(3500);
127  _tx_error_count=0;
128 }
129 
131  if(!_resetPin) return;
132 #if SIM900_DEBUG
133  Serial.println("softReset");
134 #endif
135  pinMode(_resetPin, OUTPUT);
136  digitalWrite(_resetPin,LOW);
137  delay(100);
138  digitalWrite(_resetPin,HIGH);
139  delay(100);
140  digitalWrite(_resetPin,LOW);
141  delay(100);
142  pinMode(_resetPin, INPUT);
143 }
144 
145 
146 uint8_t BayGPRSInterface::setClock(const char* time){
147  init();
148  printP("AT+CCLK=\"");
149  print(time);
150  printlnP("\"");
151 // SerialprintP("setting Clock to:");
152 // Serial.print(time);
153 // if(wait_forOK(1000)) SerialprintlnP("failed");
154 // else SerialprintlnP("ok");
155 // return 1;
156  return wait_forOK(1000);
157 }
158 
159 
160 
162  printlnP("AT");
163  wait_forOK(200);
164  printlnP("AT+CSQ");
165  wait_forPGM(PSTR("+CSQ: "),500,2,_base64buffer);
166  return (uint8_t)114 -(2*(uint8_t)atoi(_base64buffer));
167 }
168 
170  printlnP("AT");
171  wait_forOK(200);
172  printlnP("AT+CREG?");
173  wait_forPGM(PSTR("+CREG: "),2000,3,_base64buffer);
174  if(_base64buffer[2]=='1'|| _base64buffer[2]=='5') return 1; //Connected or Roaming
175  else return 0;
176 }
177 
179  printlnP("AT");
180  wait_forOK(200);
181  printlnP("AT+CGATT?");
182  if(! wait_for("+CGATT: 1",2000)) return 1;
183  return 0;
184 }
185 
187  printlnP("AT");
188  wait_forOK(200);
189  uint8_t m,d,hh,mm,ss;
190  uint16_t y;
191  DateTime dt;
192  printlnP("AT+CCLK?");
193  if(! wait_forPGM(PSTR("+CCLK: \""),3000,20,_base64buffer)){ //YY/MM/DD,HH:MM:SS+02
194 // Serial.print("LocalTime:");
195 // Serial.println(_base64buffer);
196  y = atoi(_base64buffer+0) + 2000;
197  m = atoi(_base64buffer+3);
198  d = atoi(_base64buffer+6);
199  hh = atoi(_base64buffer+9);
200  mm = atoi(_base64buffer+12);
201  ss = atoi(_base64buffer+15);
202  dt=DateTime (y, m, d, hh, mm, ss);
203  dt=DateTime(dt.get()-(3600L*atoi(_base64buffer+17))); //Adjust for Timezone!
204  }
205  return dt;
206 }
207 /*
208 void BayTCPInterface::serialprintPGM(const char *str){
209  char c;
210  while (true) {
211  c=pgm_read_byte(str);
212  if (!c) break;
213  Serial.write(c);
214  str++;
215  }
216 }
217 
218 void BayTCPInterface::serialprintlnPGM(const char *str){
219  serialprintPGM(str);
220  Serial.println();
221 }
222 */
223 void BayGPRSInterface::flushMTU(void){
224  write((uint8_t) 0x1a);
225  wait_forOK(2000);
226  printlnP("AT+CIPSEND");
227  wait_for("> ",20000);
228 }
229 
230 void BayGPRSInterface::finishTransmissionMode(void){
231  write((uint8_t) 0x1a);
232 }
233 
234 
236  if(_tx_error_count>20) softSwitch();
237  uint8_t count=0;
238  uint8_t res;
239  if(res=init()) return res+1;
240  setup_start:
241  printlnP("AT+CIFSR");
242  if(wait_for("ERROR",2000)){
243  //has IP
244  printP("AT+CIPSTART=\"TCP\",\"");
245  print(_server);
246  printP("\",");
247  println(_port);
248  if(wait_for("CONNECT",10000)) return 1;
249 
250  printlnP("AT+CIPSEND?");
251  if(! wait_forPGM(PSTR(": "),200,4,_base64buffer)){
252  _base64buffer[4]=0;
253  _mtu=atoi(_base64buffer);
254  _mtu-=(BayTCP_BUFFER+17);
255  } else _mtu=500;
256 
257 
258  printlnP("AT+CIPSEND");
259  if(wait_for("> ",10000)) return 1;
260  return 0;
261  }
262 
263  count++;
264  if(count>100)
265  return 1;
266 
267  printlnP("AT+CIPMUX=0");
268  //if(wait_forOK(400)) goto setup_start;
269 
270  printlnP("AT+CIPCLOSE");
271  wait_forOK(200);
272  printlnP("AT+CIPSHUT");
273  wait_forOK(200);
274 
275 
276  printP("AT+CSTT=\"");
277  print(_apn);
278  printP("\",\"");
279  print(_prov_user);
280  printP("\",\"");
281  print(_prov_pw);
282  printlnP("\"");
283  if(wait_forOK(400)) goto setup_start;
284 
285  printlnP("AT+CIICR");
286  if(wait_forOK(10000)) goto setup_start;
287 
288  goto setup_start;
289 
290 }
291 
293  //init();
294  printlnP("AT+CIPCLOSE");
295  wait_forOK(2000);
296 // printlnP("AT+CIPSHUT");
297 // wait_forOK(2000);
298 }
299 
300 
301 uint8_t BayGPRSInterface::sendSMS(const String &phone, const String &sms){
302  init();
303  printlnP("AT+CMGF=1");//humanreadable SMS
304  if(wait_forOK(2000)) return 2;
305  printP("AT+CMGS=\"");//Target phone number
306  print(phone);
307  printlnP("\"");
308  if(wait_for("> ",2000)) return 2;
309  print(sms);
310  write(0x1a);
311  if(wait_for("+CMGS: ",2000)) return 2;
312  return 0;
313 }
314 
315 uint8_t BayGPRSInterface::begin(long baud,uint8_t unlock_only){
316  _baud=baud;
317  i_begin(_baud);
318  skipChars();
319  printP("AT"); //Will autoconfigure BAUD-Rate - Auto BAUD-Rate did not work with Sleep-Mode!
320  delay(100);
321  println();
322  printP("AT+IPR=");
323  println(_baud);
324  wait_forOK(200);
325  return init(unlock_only);
326 }
327 
328 
329 BayGPRS::BayGPRS(HardwareSerial &serial,uint8_t powerPin,uint8_t resetPin){
330  _serial=&serial;
331  _powerPin=powerPin;
332  _resetPin=resetPin;
333  _urlencode=1;
334 }
335 int BayGPRS::available(void){return _serial->available();}
336 int BayGPRS::read(void){
337 #if SIM900_DEBUG
338  int c=_serial->read();
339  if(c!=-1) Serial.write(c);
340  return c;
341 #else
342  return _serial->read();
343 #endif
344 
345 }
346 void BayGPRS::i_begin(long b){ _serial->begin(b);}
347 void BayGPRS::i_end(void){ _serial->end();}
348 int BayGPRS::i_available(void){return _serial->available();}
349 size_t BayGPRS::write(uint8_t b){
350 #if SIM900_DEBUG
351  Serial.write(b);
352 #endif
353  return _serial->write(b);
354 }
355 int BayGPRS::peek(void){return _serial->peek();};
356 void BayGPRS::flush(void){_serial->flush();};
357 
358 
359 BayGPRSsoftserial::BayGPRSsoftserial(uint8_t rxPin, uint8_t txPin,uint8_t powerPin,uint8_t resetPin):SoftwareSerial(rxPin,txPin){
360  _powerPin=powerPin;
361  _resetPin=resetPin;
362  _urlencode=1;
363 }
364 uint8_t BayGPRSsoftserial::begin(long baud,uint8_t unlock_only){
365  BayGPRSInterface::begin(baud,unlock_only);
366 }
367 
368 int BayGPRSsoftserial::available(void){return SoftwareSerial::available();}
369 int BayGPRSsoftserial::read(void){
370 #if SIM900_DEBUG
371  int c=SoftwareSerial::read();
372  if(c!=-1) Serial.write(c);
373  return c;
374 #else
375  return SoftwareSerial::read();
376 #endif
377 }
378 void BayGPRSsoftserial::i_begin(long b){ SoftwareSerial::begin(b);}
379 void BayGPRSsoftserial::i_end(void){ SoftwareSerial::end();}
380 int BayGPRSsoftserial::i_available(void){return SoftwareSerial::available();}
381 size_t BayGPRSsoftserial::write(uint8_t b){
382 #if SIM900_DEBUG
383  Serial.write(b);
384 #endif
385  return SoftwareSerial::write(b);
386 }
387 int BayGPRSsoftserial::peek(void){return SoftwareSerial::peek();};
388 void BayGPRSsoftserial::flush(void){SoftwareSerial::flush();};
389 
390 
uint8_t sendSMS(const String &phone, const String &sms)
uint8_t init(uint8_t unlock_only=0)
void softReset(void)
uint8_t setClock(const char *time)
BayGPRS(HardwareSerial &serial, uint8_t powerPin=9, uint8_t resetPin=0)
uint8_t begin(long baud, uint8_t unlock_only=0)
void softSwitch(void)
void disconnect(void)
uint8_t getRSSI(void)
boolean _urlencode
Definition: BayTCP.h:142
uint8_t isAttached(void)
uint8_t connect(void)
uint8_t isRegistered(void)
BayGPRSsoftserial(uint8_t rxPin, uint8_t txPin, uint8_t powerPin=6, uint8_t resetPin=0)
DateTime now(void)