Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XTAL_FREQ 20000000
- #include <xc.h>
- #include "ESP8266_functions_for_pif.h"
- #include <time.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT enabled)
- #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
- #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)
- #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
- #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
- #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
- void ADC_Init()
- {
- ADCON0 = 0b10000001;
- ADCON1 = 0b10000000;
- }
- unsigned int ADC_Read(unsigned char channel)
- {
- if(channel>7)
- return 0;
- ADCON0 & = 0xC5;
- ADCON0 | = channel<<3;
- __delay_ms(2);
- GO_nDONE=1;
- while(GO_nDONE);
- return ((ADRESH<<8)+ADRESL);
- }
- void main()
- {
- float g;
- unsigned int a;
- g = 9,8;
- unsigned int tm;
- unsigned int alfa;
- tm=0;
- TRISA = 0xFF;
- ADC_Init();
- Initialize_ESP8266() ;
- esp8266_mode(3);
- /*Connect to a AccesPoint*/
- // modificado conexion iphone
- esp8266_connect("iPhone de Matias","stratomax11"); //Enter you WiFi name and password here, here BPAS home is the name and cracksun is the pas
- /*Connected to WiFi*/
- if(tm=1){
- _esp8266_enale_MUX(); //Enable multiple connections
- _esp8266_create_server(); //Create a server on port 80
- _esp8266_connect_SMPT2GO(); //Establish TCP connection with SMPT2GO
- _esp8266_login_mail("ODI2NnBpZkBnbWFpbC5jb20=","MTIzNDVQSUYyMDE3");
- __delay_ms(1500);
- // modificado correo emisor y receptor
- _esp8266_mail_sendID("8266pif@gmail.com"); //The sender mail ID
- _esp8266_mail_recID("8266pif@gmail.com"); //The Receiver mail ID
- _esp8266_start_mail();
- _esp8266_mail_subject("correo de prueba pic envío de variable"); //Enter the subject of your mail
- _esp8266_mail_body("prueba envio mail número 12"); //Enter the body of your mail
- _esp8266_End_mail();
- _esp8266_disconnect_SMPT2GO();
- //Modificado
- // mail 8266pif@gmail.com
- // password 12345PIF2017
- }
- else{}
- while(0){
- }
- }]
- /**
- * C library for the ESP8266 WiFi module with a PIC microcontroller
- * Copyright (C) 2015 Camil Staps <info@camilstaps.nl>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- *****************************************************************************
- *ORIGINALLY CREATED AS
- * File: esp8266.h
- * Author: Camil Staps <info@camilstaps.nl>
- * Website: http://github.com/camilstaps/ESP8266_PIC
- * Version: 0.1
- *
- * MODFIED AND IMPROVED BY
- * File: esp8266_functions.h
- * Author: Aswinth Raj B <mailtoaswinth@gmail.com>
- * Website: circuitdigest.com
- * Version: 0.1
- *
- * See: circuitdigest.com for more explanation
- *
- * This is the header file for the ESP8266 PIC16F877A library where ESP runs on 115200 baudrate.
- *
- */
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define ESP8266_STATION 0x01
- #define ESP8266_SOFTAP 0x02
- #define ESP8266_TCP 1
- #define ESP8266_UDP 0
- #define ESP8266_OK 1
- #define ESP8266_READY 2
- #define ESP8266_FAIL 3
- #define ESP8266_NOCHANGE 4
- #define ESP8266_LINKED 5
- #define ESP8266_UNLINK 6
- int len;
- ///***______________________ESP module Function Declarations__________________**///
- void _esp8266_putch(unsigned char);
- unsigned char _esp8266_getch(void);
- /** Function prototypes **/
- bit esp8266_isStarted(void); // Check if the module is started (AT)
- bit esp8266_restart(void); // Restart module (AT+RST)
- void esp8266_echoCmds(bool); // Enabled/disable command echoing (ATE)
- // WIFI Mode (station/softAP/station+softAP) (AT+CWMODE)
- void esp8266_mode(unsigned char);
- // Connect to AP (AT+CWJAP)
- void esp8266_connect(unsigned char*, unsigned char*);
- // Disconnect from AP (AT+CWQAP)
- void esp8266_disconnect(void);
- // Local IP (AT+CIFSR)
- void esp8266_ip(char*);
- // Create connection (AT+CIPSTART)
- bit esp8266_start(unsigned char protocol, char* ip, unsigned char port);
- // Send data (AT+CIPSEND)
- bit esp8266_send(unsigned char*);
- // Receive data (+IPD)
- void esp8266_receive(unsigned char*, uint16_t, bool);
- /** Functions for internal use only **/
- // Print a string to the output
- void _esp8266_print(unsigned const char *);
- // Wait for a certain string on the input
- inline uint16_t _esp8266_waitFor(unsigned char *);
- // Wait for any response on the input
- inline unsigned char _esp8266_waitResponse(void);
- void Lcd_Set_Cursor(char , char b);
- void Lcd_Print_Char(char);
- void Lcd_Print_String(char *);
- void _esp8266_login_mail(unsigned char*, unsigned char*);
- void _esp8266_mail_sendID(unsigned char*);
- void _esp8266_mail_recID(unsigned char*);
- void _esp8266_mail_subject(unsigned char*);
- void _esp8266_mail_body(unsigned char*);
- //********__________________End of Function Declaration_________________********///
- //***Initialize UART for ESP8266**//
- void Initialize_ESP8266(void)
- {
- //****Setting I/O pins for UART****//
- TRISC6 = 0; // TX Pin set as output
- TRISC7 = 1; // RX Pin set as input
- //________I/O pins set __________//
- /**Initialize SPBRG register for required
- baud rate and set BRGH for fast baud_rate**/
- SPBRG = 10;
- BRGH = 1; // for high baud_rate
- //_________End of baud_rate setting_________//
- //****Enable Asynchronous serial port*******//
- SYNC = 0; // Asynchronous
- SPEN = 1; // Enable serial port pins
- //_____Asynchronous serial port enabled_______//
- //**Lets prepare for transmission & reception**//
- TXEN = 1; // enable transmission
- CREN = 1; // enable reception
- //__UART module up and ready for transmission and reception__//
- //**Select 8-bit mode**//
- TX9 = 0; // 8-bit reception selected
- RX9 = 0; // 8-bit reception mode selected
- //__8-bit mode selected__//
- }
- //________UART module Initialized__________//
- //**Function to send one byte of date to UART**//
- void _esp8266_putch(char bt)
- {
- while(!TXIF); // hold the program till TX buffer is free
- TXREG = bt; //Load the transmitter buffer with the received value
- }
- //_____________End of function________________//
- //**Function to get one byte of date from UART**//
- char _esp8266_getch()
- {
- if(OERR) // check for Error
- {
- CREN = 0; //If error -> Reset
- CREN = 1; //If error -> Reset
- }
- while(!RCIF); // hold the program till RX buffer is free
- return RCREG; //receive the value and send it to main function
- }
- //_____________End of function________________//
- //**Function to convert string to byte**//
- void ESP8266_send_string(char* st_pt)
- {
- while(*st_pt) //if there is a char
- _esp8266_putch(*st_pt++); //process it as a byte data
- }
- //___________End of function______________//
- //**End of modified Codes**//
- //**Function to configure soft_AP**//
- unsigned char esp8266_config_softAP(unsigned char* softssid, unsigned char* softpass) {
- _esp8266_print("AT+CWSAP="");
- _esp8266_print(softssid);
- _esp8266_print("","");
- _esp8266_print(softpass);
- _esp8266_print("",5,3rn");
- return _esp8266_waitResponse();
- }
- //___________End of function______________//
- //**Function to stations IP/MAC**//
- void esp8266_get_stationIP()
- {
- char rex;
- ESP8266_send_string("AT+CWLIFrn");
- Lcd_Set_Cursor(1,1);
- Lcd_Print_String("IP:");
- do
- {
- rex = _esp8266_getch() ;
- Lcd_Print_Char(rex);
- }while(rex!=',');
- Lcd_Set_Cursor(2,1);
- Lcd_Print_String("MAC:");
- do
- {
- rex = _esp8266_getch() ;
- Lcd_Print_Char(rex);
- }while(rex!='O');
- }
- //___________End of function______________//
- //**Function to enable multiple connections**//
- void _esp8266_enale_MUX()
- {
- _esp8266_print("AT+CIPMUX=1rn"); //Enable Multiple Connections
- _esp8266_waitResponse();
- }
- //___________End of function______________//
- //**Function to create server on Port 80**//
- void _esp8266_create_server()
- {
- _esp8266_print("AT+CIPSERVER=1,80rn"); //Enable Server on port 80
- _esp8266_waitResponse();
- }
- //___________End of function______________//
- /*Enter into Start typing the mail*/
- void _esp8266_start_mail()
- {
- _esp8266_print("AT+CIPSEND=4,6rn");
- _esp8266_waitResponse();
- _esp8266_print("DATArn");
- _esp8266_waitResponse();
- }
- /*Entered into the typing mode*/
- /*End the Mail using a "."*/
- void _esp8266_End_mail()
- {
- _esp8266_print("AT+CIPSEND=4,3rn");
- _esp8266_waitResponse();
- _esp8266_print(".rn");
- _esp8266_waitResponse();
- }
- /*End of mail*/
- /*Quit Connection from SMPT server*/
- void _esp8266_disconnect_SMPT2GO()
- {
- _esp8266_print("AT+CIPSEND=4,6rn");
- _esp8266_waitResponse();
- _esp8266_print("QUITrn");
- _esp8266_waitResponse();
- }
- /*Disconnected*/
- /*Connect to SMPT2GO server*/
- void _esp8266_connect_SMPT2GO()
- {
- _esp8266_print("AT+CIPSTART=4,"TCP","mail.smtp2go.com",2525rn");
- _esp8266_waitResponse();
- _esp8266_print("AT+CIPSEND=4,20rn");
- _esp8266_waitResponse();
- _esp8266_print("EHLO 192.168.1.123rn");
- _esp8266_waitResponse();
- _esp8266_print("AT+CIPSEND=4,12rn");
- _esp8266_waitResponse();
- _esp8266_print("AUTH LOGINrn");
- _esp8266_waitResponse();
- }
- /*connected to Server*/
- /*LOG IN with your SMPT2GO approved mail ID*/
- /*Visit the page https://www.smtp2go.com/ and sign up using any Gmail ID
- * Once you gmail ID is SMPT2GO approved convert your mail ID and password in 64 base format
- * visit https://www.base64encode.org/ for converting 64 base format online
- * FORMAT -> _esp8266_login_mail("mailID in base 64","Password in base 64");
- * This program uses the ID-> aswinthcd@gmail.com and password -> circuitdigest as an example
- */
- void _esp8266_login_mail(unsigned char* mail_ID, unsigned char* mail_Pas) {
- len = strlen(mail_ID);
- len+= 2;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print(mail_ID);
- _esp8266_print("rn");
- _esp8266_waitResponse();
- len = strlen(mail_Pas);
- len+= 2;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print(mail_Pas);
- _esp8266_print("rn");
- _esp8266_waitResponse();
- }
- /*End of Login*/
- void _esp8266_mail_sendID(unsigned char* send_ID)
- {
- len = strlen(send_ID);
- len+= 14;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print("MAIL FROM:<");
- _esp8266_print(send_ID);
- _esp8266_print(">rn");
- _esp8266_waitResponse();
- }
- void _esp8266_mail_recID(unsigned char* rec_ID)
- {
- len = strlen(rec_ID);
- len+= 12;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print("RCPT To:<");
- _esp8266_print(rec_ID);
- _esp8266_print(">rn");
- _esp8266_waitResponse();
- }
- void _esp8266_mail_subject(unsigned char* subject)
- {
- len = strlen(subject);
- len+= 10;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print("Subject:");
- _esp8266_print(subject);
- _esp8266_print("rn");
- _esp8266_waitResponse();
- }
- void _esp8266_mail_body(unsigned char* body)
- {
- len = strlen(body);
- len+= 2;
- char l2 = len%10;
- char l1 = (len/10)%10;
- _esp8266_print("AT+CIPSEND=4,");
- if ((l1+'0')>'0')
- _esp8266_putch(l1+'0');
- _esp8266_putch(l2+'0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- _esp8266_print(body);
- _esp8266_print("rn");
- _esp8266_waitResponse();
- }
- /**
- * Check if the module is started
- *
- * This sends the `AT` command to the ESP and waits until it gets a response.
- *
- * @return true if the module is started, false if something went wrong
- */
- bit esp8266_isStarted(void) {
- _esp8266_print("ATrn");
- return (_esp8266_waitResponse() == ESP8266_OK);
- }
- /**
- * Restart the module
- *
- * This sends the `AT+RST` command to the ESP and waits until there is a
- * response.
- *
- * @return true iff the module restarted properly
- */
- bit esp8266_restart(void) {
- _esp8266_print("AT+RSTrn");
- if (_esp8266_waitResponse() != ESP8266_OK) {
- return false;
- }
- return (_esp8266_waitResponse() == ESP8266_READY);
- }
- /**
- * Enable / disable command echoing.
- *
- * Enabling this is useful for debugging: one could sniff the TX line from the
- * ESP8266 with his computer and thus receive both commands and responses.
- *
- * This sends the ATE command to the ESP module.
- *
- * @param echo whether to enable command echoing or not
- */
- void esp8266_echoCmds(bool echo) {
- _esp8266_print("ATE");
- if (echo) {
- _esp8266_putch('1');
- } else {
- _esp8266_putch('0');
- }
- _esp8266_print("rn");
- _esp8266_waitFor("OK");
- }
- /**
- * Set the WiFi mode.
- *
- * ESP8266_STATION : Station mode
- * ESP8266_SOFTAP : Access point mode
- *
- * This sends the AT+CWMODE command to the ESP module.
- *
- * @param mode an ORed bitmask of ESP8266_STATION and ESP8266_SOFTAP
- */
- void esp8266_mode(unsigned char mode) {
- _esp8266_print("AT+CWMODE=");
- _esp8266_putch(mode + '0');
- _esp8266_print("rn");
- _esp8266_waitResponse();
- }
- /**
- * Connect to an access point.
- *
- * This sends the AT+CWJAP command to the ESP module.
- *
- * @param ssid The SSID to connect to
- * @param pass The password of the network
- * @return an ESP status code, normally either ESP8266_OK or ESP8266_FAIL
- */
- void esp8266_connect(unsigned char* ssid, unsigned char* pass) {
- _esp8266_print("AT+CWJAP="");
- _esp8266_print(ssid);
- _esp8266_print("","");
- _esp8266_print(pass);
- _esp8266_print(""rn");
- _esp8266_waitResponse();
- }
- /**
- * Disconnect from the access point.
- *
- * This sends the AT+CWQAP command to the ESP module.
- */
- void esp8266_disconnect(void) {
- _esp8266_print("AT+CWQAPrn");
- _esp8266_waitFor("OK");
- }
- /**
- * Store the current local IPv4 address.
- *
- * This sends the AT+CIFSR command to the ESP module.
- *
- * The result will not be stored as a string but byte by byte. For example, for
- * the IP 192.168.0.1, the value of store_in will be: {0xc0, 0xa8, 0x00, 0x01}.
- *
- * @param store_in a pointer to an array of the type unsigned char[4]; this
- * array will be filled with the local IP.
- */
- void esp8266_ip(unsigned char* store_in) {
- _esp8266_print("AT+CIFSRrn");
- unsigned char received;
- do {
- received = _esp8266_getch();
- } while (received < '0' || received > '9');
- for (unsigned char i = 0; i < 4; i++) {
- store_in[i] = 0;
- do {
- store_in[i] = 10 * store_in[i] + received - '0';
- received = _esp8266_getch();
- } while (received >= '0' && received <= '9');
- received = _esp8266_getch();
- }
- _esp8266_waitFor("OK");
- }
- /**
- * Open a TCP or UDP connection.
- *
- * This sends the AT+CIPSTART command to the ESP module.
- *
- * @param protocol Either ESP8266_TCP or ESP8266_UDP
- * @param ip The IP or hostname to connect to; as a string
- * @param port The port to connect to
- *
- * @return true iff the connection is opened after this.
- */
- bit esp8266_start(unsigned char protocol, char* ip, unsigned char port) {
- _esp8266_print("AT+CIPSTART="");
- if (protocol == ESP8266_TCP) {
- _esp8266_print("TCP");
- } else {
- _esp8266_print("UDP");
- }
- _esp8266_print("","");
- _esp8266_print(ip);
- _esp8266_print("",");
- unsigned char port_str[5] = "