Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #define SSID "wi-fi_login" // ваш SSID
  2. #define PASS "wi-fi_password" // ваш пароль Wi-Fi
  3.  
  4. #define SMTPServer "s02.atomsmtp.com" //smtp сервер
  5. #define SMTPPort "2525" // smtp порт
  6. #define MailLogin "smtp_example@gmail.com" // логин для smtp
  7. #define MailLoginBase64 "dWd1LCBrb25lNG5vCg==" //логин для smtp в Base64
  8. #define MailPasswordBase64 "aHJlbiB0YW0K" // пароль для smtp в Base64
  9. #define MailRelay "example@mail.ru" // промежуточная почта для "отмывания" email
  10. #define PhoneNumber "375290000000" // номер телефона
  11. #define Message "Hello from Arduino!" //сообщение
  12.  
  13. #define SERIAL_RX_BUFFER_SIZE 256
  14. #define SERIAL_TX_BUFFER_SIZE 256
  15.  
  16. void setup()
  17. {
  18. delay(2000);
  19. Serial3.begin(115200);
  20. Serial3.setTimeout(5000);
  21. Serial.begin(115200); // для отладки
  22. Serial.println("Init");
  23. Serial3.println("AT+RST"); // сброс и проверка, что модуль готов
  24. if(WiteString("Ready", 5000)) {
  25. while(Serial3.available()) { Serial3.read();}
  26. Serial.println("WiFi - Module is ready");
  27. }else{
  28. Serial.println("Module dosn't respond.");
  29. while(1);
  30. }
  31. delay(100);
  32.  
  33. Serial3.println(" AT+CIPMODE=0");
  34. WiteString("OK");
  35. while(Serial3.available()) { Serial3.read();}
  36.  
  37. Serial3.println("AT+CIPMUX=1");
  38. WiteString("OK");
  39. while(Serial3.available()) { Serial3.read();}
  40.  
  41. // try to connect to wifi
  42. boolean connected = false;
  43. for(int i=0;i<5;i++) {
  44. if(connectWiFi()) {
  45. connected = true;
  46. break;
  47. }
  48. }
  49. if (!connected) {
  50. while(1);
  51. }
  52. }
  53. void loop()
  54. {
  55. String cmd = "AT+CIPSTART=0,\"TCP\",\"";
  56. cmd += String(SMTPServer);
  57. cmd += "\"," + String(SMTPPort);
  58. Serial3.println(cmd);
  59.  
  60. if(WiteString("Linked", 5000)) {
  61. while(Serial3.available()) { Serial3.read();}
  62. Serial.println("Link");
  63. }
  64. else {
  65. Serial.println("Link fail");
  66. while (1);
  67. }
  68.  
  69. if (WiteString("OK", 2000)) {
  70. while(Serial3.available()) { Serial3.read();}
  71. }
  72. else {
  73. while (1);
  74. }
  75.  
  76. Send("HELO 1.2.3.4", true);
  77. Send("AUTH LOGIN", true);
  78. Send(MailLoginBase64, true);
  79. Send(MailPasswordBase64, true);
  80. Send("MAIL FROM:<" + String(MailLogin) + ">", true);
  81. Send("RCPT TO:<" + String(MailRelay) + ">", true);
  82. Send("DATA", true);
  83. Send("Subject:SMS", false);
  84. Send("To:\"" + String(PhoneNumber) + "\" <" + String(PhoneNumber) + "@sms.mts.by>", false);
  85. Send("From: <" + String(MailLogin) + ">", false);
  86. Send("", false);
  87. Send(Message, false);
  88. Send(".", true);
  89. Send("QUIT", true);
  90.  
  91. while(1) {};
  92. }
  93.  
  94. boolean connectWiFi()
  95. {
  96. Serial3.println("AT+CWMODE=1");
  97. while (!Serial3.available()) { delay(10);}
  98. while (Serial3.available()) {Serial3.read();}
  99. String cmd="AT+CWJAP=\"";
  100. cmd+=SSID;
  101. cmd+="\",\"";
  102. cmd+=PASS;
  103. cmd+="\"";
  104. Serial3.println(cmd);
  105.  
  106. if(WiteString("OK", 8000)){
  107. Serial.println("Connected to WiFi.");
  108. return true;
  109. }else{
  110. Serial.println("Can not connect to the WiFi.");
  111. return false;
  112. }
  113. }
  114.  
  115. bool Send(String S, bool wite) {
  116. Serial3.print("AT+CIPSEND=0,");
  117. Serial3.println(S.length()+2);
  118. while (!Serial3.available()) { delay(10);}
  119. if(Serial3.find(">")){
  120. }else{
  121. Serial3.println("AT+CIPCLOSE=0");
  122. delay(1000);
  123. return false;
  124. }
  125. Serial3.print(S + "\r\n");//добаяляем перевод строки
  126. if (WiteString("OK", 15000)) {
  127. if (wite) {
  128. WiteString("+IPD", 15000);
  129. while(Serial3.available()) {
  130. Serial3.read();}}
  131. return true;}
  132. else {
  133. return false;}
  134. }
  135.  
  136. void WiteString(String S) {
  137. int L = S.length();
  138. String T = String(" ");
  139. while(1) {
  140. if (Serial3.available()) {
  141. char c = Serial3.read();
  142. T = T + String(c);
  143. if (T.length() > L) T = T.substring(1);
  144. if (S.charAt(0) == T.charAt(0))
  145. if (S.compareTo(T) == 0) return;
  146. }
  147. else {
  148. delay(1);
  149. }
  150. }
  151. }
  152.  
  153. bool WiteString(String S, int Time) {
  154. int L = S.length();
  155. String T = String(" ");
  156. while(Time>0) {
  157. if (Serial3.available()) {
  158. char c = Serial3.read();
  159. T = T + String(c);
  160. if (T.length() > L) T = T.substring(1);
  161. if (S.charAt(0) == T.charAt(0))
  162. if (S.compareTo(T) == 0) return true;
  163. }
  164. else {
  165. delay(1);
  166. Time--;
  167. }
  168. }
  169. return false;
  170. }
  171.  
  172. String WiteString(int Time) {
  173. String T = String("");
  174. while(Time>0) {
  175. if (Serial3.available()) {
  176. char c = Serial3.read();
  177. T = T + String(c);
  178. }
  179. else {
  180. delay(1);
  181. Time--;
  182. }
  183. }
  184. return T;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement