Guest User

Untitled

a guest
Dec 12th, 2017
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.80 KB | None | 0 0
  1. #define _XTAL_FREQ 20000000
  2.  
  3. #include <xc.h>
  4. #include "ESP8266_functions_for_pif.h"
  5. #include <time.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10.  
  11. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
  12. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
  13. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT enabled)
  14. #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
  15. #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  16. #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  17. #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  18. #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
  19.  
  20.  
  21.  
  22.  
  23.  
  24. void ADC_Init()
  25. {
  26. ADCON0 = 0b10000001;
  27. ADCON1 = 0b10000000;
  28.  
  29. }
  30.  
  31. unsigned int ADC_Read(unsigned char channel)
  32. {
  33. if(channel>7)
  34. return 0;
  35. ADCON0 & = 0xC5;
  36. ADCON0 | = channel<<3;
  37. __delay_ms(2);
  38. GO_nDONE=1;
  39. while(GO_nDONE);
  40. return ((ADRESH<<8)+ADRESL);
  41. }
  42.  
  43. void main()
  44. {
  45. float g;
  46. unsigned int a;
  47. g = 9,8;
  48. unsigned int tm;
  49. unsigned int alfa;
  50. tm=0;
  51. TRISA = 0xFF;
  52.  
  53. ADC_Init();
  54. Initialize_ESP8266() ;
  55. esp8266_mode(3);
  56. /*Connect to a AccesPoint*/
  57. // modificado conexion iphone
  58. esp8266_connect("iPhone de Matias","stratomax11"); //Enter you WiFi name and password here, here BPAS home is the name and cracksun is the pas
  59.  
  60. /*Connected to WiFi*/
  61.  
  62.  
  63.  
  64. if(tm=1){
  65.  
  66. _esp8266_enale_MUX(); //Enable multiple connections
  67. _esp8266_create_server(); //Create a server on port 80
  68. _esp8266_connect_SMPT2GO(); //Establish TCP connection with SMPT2GO
  69.  
  70. _esp8266_login_mail("ODI2NnBpZkBnbWFpbC5jb20=","MTIzNDVQSUYyMDE3");
  71.  
  72. __delay_ms(1500);
  73.  
  74.  
  75. // modificado correo emisor y receptor
  76.  
  77. _esp8266_mail_sendID("8266pif@gmail.com"); //The sender mail ID
  78. _esp8266_mail_recID("8266pif@gmail.com"); //The Receiver mail ID
  79.  
  80. _esp8266_start_mail();
  81. _esp8266_mail_subject("correo de prueba pic envío de variable"); //Enter the subject of your mail
  82. _esp8266_mail_body("prueba envio mail número 12"); //Enter the body of your mail
  83. _esp8266_End_mail();
  84. _esp8266_disconnect_SMPT2GO();
  85.  
  86. //Modificado
  87. // mail 8266pif@gmail.com
  88. // password 12345PIF2017
  89.  
  90.  
  91.  
  92.  
  93. }
  94.  
  95. else{}
  96.  
  97. while(0){
  98.  
  99. }
  100.  
  101. }]
  102.  
  103. /**
  104. * C library for the ESP8266 WiFi module with a PIC microcontroller
  105. * Copyright (C) 2015 Camil Staps <info@camilstaps.nl>
  106. *
  107. * This program is free software; you can redistribute it and/or modify
  108. * it under the terms of the GNU General Public License as published by
  109. * the Free Software Foundation; either version 2 of the License, or
  110. * (at your option) any later version.
  111. *
  112. * This program is distributed in the hope that it will be useful,
  113. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  114. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  115. * GNU General Public License for more details.
  116. *
  117. * You should have received a copy of the GNU General Public License along
  118. * with this program; if not, write to the Free Software Foundation, Inc.,
  119. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  120. *
  121. *****************************************************************************
  122. *ORIGINALLY CREATED AS
  123. * File: esp8266.h
  124. * Author: Camil Staps <info@camilstaps.nl>
  125. * Website: http://github.com/camilstaps/ESP8266_PIC
  126. * Version: 0.1
  127. *
  128. * MODFIED AND IMPROVED BY
  129. * File: esp8266_functions.h
  130. * Author: Aswinth Raj B <mailtoaswinth@gmail.com>
  131. * Website: circuitdigest.com
  132. * Version: 0.1
  133. *
  134. * See: circuitdigest.com for more explanation
  135. *
  136. * This is the header file for the ESP8266 PIC16F877A library where ESP runs on 115200 baudrate.
  137. *
  138. */
  139.  
  140.  
  141.  
  142. #include <stdbool.h>
  143. #include <stdint.h>
  144. #include <stdlib.h>
  145. #include <stdio.h>
  146. #include <string.h>
  147.  
  148.  
  149. #define ESP8266_STATION 0x01
  150. #define ESP8266_SOFTAP 0x02
  151.  
  152. #define ESP8266_TCP 1
  153. #define ESP8266_UDP 0
  154.  
  155. #define ESP8266_OK 1
  156. #define ESP8266_READY 2
  157. #define ESP8266_FAIL 3
  158. #define ESP8266_NOCHANGE 4
  159. #define ESP8266_LINKED 5
  160. #define ESP8266_UNLINK 6
  161.  
  162. int len;
  163.  
  164.  
  165. ///***______________________ESP module Function Declarations__________________**///
  166. void _esp8266_putch(unsigned char);
  167. unsigned char _esp8266_getch(void);
  168. /** Function prototypes **/
  169. bit esp8266_isStarted(void); // Check if the module is started (AT)
  170. bit esp8266_restart(void); // Restart module (AT+RST)
  171. void esp8266_echoCmds(bool); // Enabled/disable command echoing (ATE)
  172. // WIFI Mode (station/softAP/station+softAP) (AT+CWMODE)
  173. void esp8266_mode(unsigned char);
  174. // Connect to AP (AT+CWJAP)
  175. void esp8266_connect(unsigned char*, unsigned char*);
  176. // Disconnect from AP (AT+CWQAP)
  177. void esp8266_disconnect(void);
  178. // Local IP (AT+CIFSR)
  179. void esp8266_ip(char*);
  180. // Create connection (AT+CIPSTART)
  181. bit esp8266_start(unsigned char protocol, char* ip, unsigned char port);
  182. // Send data (AT+CIPSEND)
  183. bit esp8266_send(unsigned char*);
  184. // Receive data (+IPD)
  185. void esp8266_receive(unsigned char*, uint16_t, bool);
  186. /** Functions for internal use only **/
  187. // Print a string to the output
  188. void _esp8266_print(unsigned const char *);
  189. // Wait for a certain string on the input
  190. inline uint16_t _esp8266_waitFor(unsigned char *);
  191. // Wait for any response on the input
  192. inline unsigned char _esp8266_waitResponse(void);
  193. void Lcd_Set_Cursor(char , char b);
  194. void Lcd_Print_Char(char);
  195. void Lcd_Print_String(char *);
  196. void _esp8266_login_mail(unsigned char*, unsigned char*);
  197. void _esp8266_mail_sendID(unsigned char*);
  198. void _esp8266_mail_recID(unsigned char*);
  199. void _esp8266_mail_subject(unsigned char*);
  200. void _esp8266_mail_body(unsigned char*);
  201. //********__________________End of Function Declaration_________________********///
  202.  
  203. //***Initialize UART for ESP8266**//
  204. void Initialize_ESP8266(void)
  205. {
  206. //****Setting I/O pins for UART****//
  207. TRISC6 = 0; // TX Pin set as output
  208. TRISC7 = 1; // RX Pin set as input
  209. //________I/O pins set __________//
  210.  
  211. /**Initialize SPBRG register for required
  212. baud rate and set BRGH for fast baud_rate**/
  213. SPBRG = 10;
  214. BRGH = 1; // for high baud_rate
  215. //_________End of baud_rate setting_________//
  216.  
  217. //****Enable Asynchronous serial port*******//
  218. SYNC = 0; // Asynchronous
  219. SPEN = 1; // Enable serial port pins
  220. //_____Asynchronous serial port enabled_______//
  221. //**Lets prepare for transmission & reception**//
  222. TXEN = 1; // enable transmission
  223. CREN = 1; // enable reception
  224. //__UART module up and ready for transmission and reception__//
  225.  
  226. //**Select 8-bit mode**//
  227. TX9 = 0; // 8-bit reception selected
  228. RX9 = 0; // 8-bit reception mode selected
  229. //__8-bit mode selected__//
  230. }
  231. //________UART module Initialized__________//
  232.  
  233.  
  234.  
  235.  
  236. //**Function to send one byte of date to UART**//
  237. void _esp8266_putch(char bt)
  238. {
  239. while(!TXIF); // hold the program till TX buffer is free
  240. TXREG = bt; //Load the transmitter buffer with the received value
  241. }
  242. //_____________End of function________________//
  243.  
  244.  
  245.  
  246. //**Function to get one byte of date from UART**//
  247. char _esp8266_getch()
  248. {
  249. if(OERR) // check for Error
  250. {
  251. CREN = 0; //If error -> Reset
  252. CREN = 1; //If error -> Reset
  253. }
  254.  
  255. while(!RCIF); // hold the program till RX buffer is free
  256.  
  257. return RCREG; //receive the value and send it to main function
  258. }
  259. //_____________End of function________________//
  260.  
  261.  
  262.  
  263.  
  264. //**Function to convert string to byte**//
  265. void ESP8266_send_string(char* st_pt)
  266. {
  267. while(*st_pt) //if there is a char
  268. _esp8266_putch(*st_pt++); //process it as a byte data
  269. }
  270. //___________End of function______________//
  271. //**End of modified Codes**//
  272.  
  273.  
  274.  
  275. //**Function to configure soft_AP**//
  276. unsigned char esp8266_config_softAP(unsigned char* softssid, unsigned char* softpass) {
  277. _esp8266_print("AT+CWSAP="");
  278. _esp8266_print(softssid);
  279. _esp8266_print("","");
  280. _esp8266_print(softpass);
  281. _esp8266_print("",5,3rn");
  282. return _esp8266_waitResponse();
  283. }
  284. //___________End of function______________//
  285.  
  286.  
  287.  
  288.  
  289. //**Function to stations IP/MAC**//
  290. void esp8266_get_stationIP()
  291. {
  292. char rex;
  293. ESP8266_send_string("AT+CWLIFrn");
  294. Lcd_Set_Cursor(1,1);
  295. Lcd_Print_String("IP:");
  296. do
  297. {
  298. rex = _esp8266_getch() ;
  299. Lcd_Print_Char(rex);
  300. }while(rex!=',');
  301. Lcd_Set_Cursor(2,1);
  302. Lcd_Print_String("MAC:");
  303. do
  304. {
  305. rex = _esp8266_getch() ;
  306. Lcd_Print_Char(rex);
  307. }while(rex!='O');
  308. }
  309. //___________End of function______________//
  310.  
  311.  
  312.  
  313. //**Function to enable multiple connections**//
  314. void _esp8266_enale_MUX()
  315. {
  316. _esp8266_print("AT+CIPMUX=1rn"); //Enable Multiple Connections
  317. _esp8266_waitResponse();
  318. }
  319. //___________End of function______________//
  320.  
  321.  
  322. //**Function to create server on Port 80**//
  323. void _esp8266_create_server()
  324. {
  325. _esp8266_print("AT+CIPSERVER=1,80rn"); //Enable Server on port 80
  326. _esp8266_waitResponse();
  327. }
  328. //___________End of function______________//
  329.  
  330.  
  331.  
  332. /*Enter into Start typing the mail*/
  333. void _esp8266_start_mail()
  334. {
  335. _esp8266_print("AT+CIPSEND=4,6rn");
  336. _esp8266_waitResponse();
  337. _esp8266_print("DATArn");
  338. _esp8266_waitResponse();
  339. }
  340. /*Entered into the typing mode*/
  341.  
  342.  
  343. /*End the Mail using a "."*/
  344. void _esp8266_End_mail()
  345. {
  346. _esp8266_print("AT+CIPSEND=4,3rn");
  347. _esp8266_waitResponse();
  348. _esp8266_print(".rn");
  349. _esp8266_waitResponse();
  350. }
  351. /*End of mail*/
  352.  
  353. /*Quit Connection from SMPT server*/
  354. void _esp8266_disconnect_SMPT2GO()
  355. {
  356. _esp8266_print("AT+CIPSEND=4,6rn");
  357. _esp8266_waitResponse();
  358. _esp8266_print("QUITrn");
  359. _esp8266_waitResponse();
  360. }
  361. /*Disconnected*/
  362.  
  363.  
  364.  
  365.  
  366. /*Connect to SMPT2GO server*/
  367. void _esp8266_connect_SMPT2GO()
  368. {
  369. _esp8266_print("AT+CIPSTART=4,"TCP","mail.smtp2go.com",2525rn");
  370. _esp8266_waitResponse();
  371. _esp8266_print("AT+CIPSEND=4,20rn");
  372. _esp8266_waitResponse();
  373. _esp8266_print("EHLO 192.168.1.123rn");
  374. _esp8266_waitResponse();
  375. _esp8266_print("AT+CIPSEND=4,12rn");
  376. _esp8266_waitResponse();
  377. _esp8266_print("AUTH LOGINrn");
  378. _esp8266_waitResponse();
  379. }
  380. /*connected to Server*/
  381.  
  382. /*LOG IN with your SMPT2GO approved mail ID*/
  383. /*Visit the page https://www.smtp2go.com/ and sign up using any Gmail ID
  384. * Once you gmail ID is SMPT2GO approved convert your mail ID and password in 64 base format
  385. * visit https://www.base64encode.org/ for converting 64 base format online
  386. * FORMAT -> _esp8266_login_mail("mailID in base 64","Password in base 64");
  387. * This program uses the ID-> aswinthcd@gmail.com and password -> circuitdigest as an example
  388. */
  389. void _esp8266_login_mail(unsigned char* mail_ID, unsigned char* mail_Pas) {
  390. len = strlen(mail_ID);
  391. len+= 2;
  392. char l2 = len%10;
  393. char l1 = (len/10)%10;
  394.  
  395. _esp8266_print("AT+CIPSEND=4,");
  396. if ((l1+'0')>'0')
  397. _esp8266_putch(l1+'0');
  398. _esp8266_putch(l2+'0');
  399. _esp8266_print("rn");
  400. _esp8266_waitResponse();
  401.  
  402. _esp8266_print(mail_ID);
  403. _esp8266_print("rn");
  404. _esp8266_waitResponse();
  405.  
  406. len = strlen(mail_Pas);
  407. len+= 2;
  408. char l2 = len%10;
  409. char l1 = (len/10)%10;
  410.  
  411. _esp8266_print("AT+CIPSEND=4,");
  412. if ((l1+'0')>'0')
  413. _esp8266_putch(l1+'0');
  414. _esp8266_putch(l2+'0');
  415. _esp8266_print("rn");
  416. _esp8266_waitResponse();
  417.  
  418.  
  419. _esp8266_print(mail_Pas);
  420. _esp8266_print("rn");
  421. _esp8266_waitResponse();
  422. }
  423. /*End of Login*/
  424.  
  425.  
  426. void _esp8266_mail_sendID(unsigned char* send_ID)
  427. {
  428. len = strlen(send_ID);
  429. len+= 14;
  430. char l2 = len%10;
  431. char l1 = (len/10)%10;
  432.  
  433. _esp8266_print("AT+CIPSEND=4,");
  434. if ((l1+'0')>'0')
  435. _esp8266_putch(l1+'0');
  436. _esp8266_putch(l2+'0');
  437. _esp8266_print("rn");
  438. _esp8266_waitResponse();
  439.  
  440. _esp8266_print("MAIL FROM:<");
  441. _esp8266_print(send_ID);
  442. _esp8266_print(">rn");
  443. _esp8266_waitResponse();
  444. }
  445.  
  446.  
  447. void _esp8266_mail_recID(unsigned char* rec_ID)
  448. {
  449. len = strlen(rec_ID);
  450. len+= 12;
  451. char l2 = len%10;
  452. char l1 = (len/10)%10;
  453.  
  454. _esp8266_print("AT+CIPSEND=4,");
  455. if ((l1+'0')>'0')
  456. _esp8266_putch(l1+'0');
  457. _esp8266_putch(l2+'0');
  458. _esp8266_print("rn");
  459. _esp8266_waitResponse();
  460.  
  461. _esp8266_print("RCPT To:<");
  462. _esp8266_print(rec_ID);
  463. _esp8266_print(">rn");
  464. _esp8266_waitResponse();
  465. }
  466.  
  467. void _esp8266_mail_subject(unsigned char* subject)
  468. {
  469. len = strlen(subject);
  470. len+= 10;
  471. char l2 = len%10;
  472. char l1 = (len/10)%10;
  473.  
  474. _esp8266_print("AT+CIPSEND=4,");
  475. if ((l1+'0')>'0')
  476. _esp8266_putch(l1+'0');
  477. _esp8266_putch(l2+'0');
  478. _esp8266_print("rn");
  479. _esp8266_waitResponse();
  480.  
  481. _esp8266_print("Subject:");
  482. _esp8266_print(subject);
  483. _esp8266_print("rn");
  484. _esp8266_waitResponse();
  485. }
  486.  
  487.  
  488.  
  489. void _esp8266_mail_body(unsigned char* body)
  490. {
  491. len = strlen(body);
  492. len+= 2;
  493. char l2 = len%10;
  494. char l1 = (len/10)%10;
  495.  
  496. _esp8266_print("AT+CIPSEND=4,");
  497. if ((l1+'0')>'0')
  498. _esp8266_putch(l1+'0');
  499. _esp8266_putch(l2+'0');
  500. _esp8266_print("rn");
  501. _esp8266_waitResponse();
  502.  
  503. _esp8266_print(body);
  504. _esp8266_print("rn");
  505. _esp8266_waitResponse();
  506. }
  507.  
  508. /**
  509. * Check if the module is started
  510. *
  511. * This sends the `AT` command to the ESP and waits until it gets a response.
  512. *
  513. * @return true if the module is started, false if something went wrong
  514. */
  515. bit esp8266_isStarted(void) {
  516. _esp8266_print("ATrn");
  517. return (_esp8266_waitResponse() == ESP8266_OK);
  518. }
  519.  
  520. /**
  521. * Restart the module
  522. *
  523. * This sends the `AT+RST` command to the ESP and waits until there is a
  524. * response.
  525. *
  526. * @return true iff the module restarted properly
  527. */
  528. bit esp8266_restart(void) {
  529. _esp8266_print("AT+RSTrn");
  530. if (_esp8266_waitResponse() != ESP8266_OK) {
  531. return false;
  532. }
  533. return (_esp8266_waitResponse() == ESP8266_READY);
  534. }
  535.  
  536. /**
  537. * Enable / disable command echoing.
  538. *
  539. * Enabling this is useful for debugging: one could sniff the TX line from the
  540. * ESP8266 with his computer and thus receive both commands and responses.
  541. *
  542. * This sends the ATE command to the ESP module.
  543. *
  544. * @param echo whether to enable command echoing or not
  545. */
  546. void esp8266_echoCmds(bool echo) {
  547. _esp8266_print("ATE");
  548. if (echo) {
  549. _esp8266_putch('1');
  550. } else {
  551. _esp8266_putch('0');
  552. }
  553. _esp8266_print("rn");
  554. _esp8266_waitFor("OK");
  555. }
  556.  
  557. /**
  558. * Set the WiFi mode.
  559. *
  560. * ESP8266_STATION : Station mode
  561. * ESP8266_SOFTAP : Access point mode
  562. *
  563. * This sends the AT+CWMODE command to the ESP module.
  564. *
  565. * @param mode an ORed bitmask of ESP8266_STATION and ESP8266_SOFTAP
  566. */
  567. void esp8266_mode(unsigned char mode) {
  568. _esp8266_print("AT+CWMODE=");
  569. _esp8266_putch(mode + '0');
  570. _esp8266_print("rn");
  571. _esp8266_waitResponse();
  572. }
  573.  
  574. /**
  575. * Connect to an access point.
  576. *
  577. * This sends the AT+CWJAP command to the ESP module.
  578. *
  579. * @param ssid The SSID to connect to
  580. * @param pass The password of the network
  581. * @return an ESP status code, normally either ESP8266_OK or ESP8266_FAIL
  582. */
  583. void esp8266_connect(unsigned char* ssid, unsigned char* pass) {
  584. _esp8266_print("AT+CWJAP="");
  585. _esp8266_print(ssid);
  586. _esp8266_print("","");
  587. _esp8266_print(pass);
  588. _esp8266_print(""rn");
  589. _esp8266_waitResponse();
  590. }
  591.  
  592. /**
  593. * Disconnect from the access point.
  594. *
  595. * This sends the AT+CWQAP command to the ESP module.
  596. */
  597. void esp8266_disconnect(void) {
  598. _esp8266_print("AT+CWQAPrn");
  599. _esp8266_waitFor("OK");
  600. }
  601.  
  602. /**
  603. * Store the current local IPv4 address.
  604. *
  605. * This sends the AT+CIFSR command to the ESP module.
  606. *
  607. * The result will not be stored as a string but byte by byte. For example, for
  608. * the IP 192.168.0.1, the value of store_in will be: {0xc0, 0xa8, 0x00, 0x01}.
  609. *
  610. * @param store_in a pointer to an array of the type unsigned char[4]; this
  611. * array will be filled with the local IP.
  612. */
  613. void esp8266_ip(unsigned char* store_in) {
  614. _esp8266_print("AT+CIFSRrn");
  615. unsigned char received;
  616. do {
  617. received = _esp8266_getch();
  618. } while (received < '0' || received > '9');
  619. for (unsigned char i = 0; i < 4; i++) {
  620. store_in[i] = 0;
  621. do {
  622. store_in[i] = 10 * store_in[i] + received - '0';
  623. received = _esp8266_getch();
  624. } while (received >= '0' && received <= '9');
  625. received = _esp8266_getch();
  626. }
  627. _esp8266_waitFor("OK");
  628. }
  629.  
  630. /**
  631. * Open a TCP or UDP connection.
  632. *
  633. * This sends the AT+CIPSTART command to the ESP module.
  634. *
  635. * @param protocol Either ESP8266_TCP or ESP8266_UDP
  636. * @param ip The IP or hostname to connect to; as a string
  637. * @param port The port to connect to
  638. *
  639. * @return true iff the connection is opened after this.
  640. */
  641. bit esp8266_start(unsigned char protocol, char* ip, unsigned char port) {
  642. _esp8266_print("AT+CIPSTART="");
  643. if (protocol == ESP8266_TCP) {
  644. _esp8266_print("TCP");
  645. } else {
  646. _esp8266_print("UDP");
  647. }
  648. _esp8266_print("","");
  649. _esp8266_print(ip);
  650. _esp8266_print("",");
  651. unsigned char port_str[5] = "";
  652. sprintf(port_str, "%u", port);
  653. _esp8266_print(port_str);
  654. _esp8266_print("rn");
  655. if (_esp8266_waitResponse() != ESP8266_OK) {
  656. return 0;
  657. }
  658. if (_esp8266_waitResponse() != ESP8266_LINKED) {
  659. return 0;
  660. }
  661. return 1;
  662. }
  663.  
  664. // Send data (AT+CIPSEND)
  665. /**
  666. * Send data over a connection.
  667. *
  668. * This sends the AT+CIPSEND command to the ESP module.
  669. *
  670. * @param data The data to send
  671. *
  672. * @return true iff the data was sent correctly.
  673. */
  674. bit esp8266_send(unsigned char* data) {
  675. unsigned char length_str[6] = "";
  676. sprintf(length_str, "%u", strlen(data));
  677. _esp8266_print("AT+CIPSEND=");
  678. _esp8266_print(length_str);
  679. _esp8266_print("rn");
  680. while (_esp8266_getch() != '>');
  681. _esp8266_print(data);
  682. if (_esp8266_waitResponse() == ESP8266_OK) {
  683. return 1;
  684. }
  685. return 0;
  686. }
  687.  
  688. /**
  689. * Read a string of data that is sent to the ESP8266.
  690. *
  691. * This waits for a +IPD line from the module. If more bytes than the maximum
  692. * are received, the remaining bytes will be discarded.
  693. *
  694. * @param store_in a pointer to a character array to store the data in
  695. * @param max_length maximum amount of bytes to read in
  696. * @param discard_headers if set to true, we will skip until the first rnrn,
  697. * for HTTP this means skipping the headers.
  698. */
  699. void esp8266_receive(unsigned char* store_in, uint16_t max_length, bool discard_headers) {
  700. _esp8266_waitFor("+IPD,");
  701. uint16_t length = 0;
  702. unsigned char received = _esp8266_getch();
  703. do {
  704. length = length * 10 + received - '0';
  705. received = _esp8266_getch();
  706. } while (received >= '0' && received <= '9');
  707.  
  708. if (discard_headers) {
  709. length -= _esp8266_waitFor("rnrn");
  710. }
  711.  
  712. if (length < max_length) {
  713. max_length = length;
  714. }
  715.  
  716. /*sprintf(store_in, "%u,%u:%c%c", length, max_length, _esp8266_getch(), _esp8266_getch());
  717. return;*/
  718.  
  719. uint16_t i;
  720. for (i = 0; i < max_length; i++) {
  721. store_in[i] = _esp8266_getch();
  722. }
  723. store_in[i] = 0;
  724. for (; i < length; i++) {
  725. _esp8266_getch();
  726. }
  727. _esp8266_waitFor("OK");
  728. }
  729.  
  730. /**
  731. * Output a string to the ESP module.
  732. *
  733. * This is a function for internal use only.
  734. *
  735. * @param ptr A pointer to the string to send.
  736. */
  737. void _esp8266_print(unsigned const char *ptr) {
  738. while (*ptr != 0) {
  739. _esp8266_putch(*ptr++);
  740. }
  741. }
  742.  
  743. /**
  744. * Wait until we found a string on the input.
  745. *
  746. * Careful: this will read everything until that string (even if it's never
  747. * found). You may lose important data.
  748. *
  749. * @param string
  750. *
  751. * @return the number of characters read
  752. */
  753. inline uint16_t _esp8266_waitFor(unsigned char *string) {
  754. unsigned char so_far = 0;
  755. unsigned char received;
  756. uint16_t counter = 0;
  757. do {
  758. received = _esp8266_getch();
  759. counter++;
  760. if (received == string[so_far]) {
  761. so_far++;
  762. } else {
  763. so_far = 0;
  764. }
  765. } while (string[so_far] != 0);
  766. return counter;
  767. }
  768.  
  769. /**
  770. * Wait until we received the ESP is done and sends its response.
  771. *
  772. * This is a function for internal use only.
  773. *
  774. * Currently the following responses are implemented:
  775. * * OK
  776. * * ready
  777. * * FAIL
  778. * * no change
  779. * * Linked
  780. * * Unlink
  781. *
  782. * Not implemented yet:
  783. * * DNS fail (or something like that)
  784. *
  785. * @return a constant from esp8266.h describing the status response.
  786. */
  787. inline unsigned char _esp8266_waitResponse(void) {
  788. unsigned char so_far[6] = {0,0,0,0,0,0};
  789. unsigned const char lengths[6] = {2,5,4,9,6,6};
  790. unsigned const char* strings[6] = {"OK", "ready", "FAIL", "no change", "Linked", "Unlink"};
  791. unsigned const char responses[6] = {ESP8266_OK, ESP8266_READY, ESP8266_FAIL, ESP8266_NOCHANGE, ESP8266_LINKED, ESP8266_UNLINK};
  792. unsigned char received;
  793. unsigned char response;
  794. bool continue_loop = true;
  795. while (continue_loop) {
  796. received = _esp8266_getch();
  797. for (unsigned char i = 0; i < 6; i++) {
  798. if (strings[i][so_far[i]] == received) {
  799. so_far[i]++;
  800. if (so_far[i] == lengths[i]) {
  801. response = responses[i];
  802. continue_loop = false;
  803. }
  804. } else {
  805. so_far[i] = 0;
  806. }
  807. }
  808. }
  809. return response;
  810. }
Add Comment
Please, Sign In to add comment