^
^
h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define ESP8266_TCP 1
#define ESP8266_UDP 0
#define ESP8266_TRANS_PASS 1
#define ESP8266_TRANS_NOR 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
#define ESP8266_CONNECT 7
// Interrupt routine
void interrupt(void)
RCIF_bit = 0;
ReceiveData = UART1_Read();
receive_flag = 1;
// UART send
while(!TXIF_bit);
TXREG = bt;
// UART receive
char _esp8266_getch()
if(OERR_bit)
CREN_bit = 0;
CREN_bit = 1;
while(!RCIF_bit);
return RCREG;
}
while(*st_pt)
_esp8266_putch(*st_pt++);
// Print string
while (*ptr != 0)
_esp8266_putch(*ptr++);
uint16_t counter = 0;
do
{
received = _esp8266_getch();
counter++;
if (received == string[so_far])
so_far++;
else
so_far = 0;
return counter;
// Restart module
void esp8266_restart(void)
_esp8266_print("AT+RST\r\n");
_esp8266_waitFor("OK");
_esp8266_waitFor("ready");
void esp8266_isStarted(void)
_esp8266_print("AT\r\n");
_esp8266_waitFor("OK");
// Enable/disable echo
_esp8266_print("ATE");
if (echo)
_esp8266_putch('1');
else
_esp8266_putch('0');
_esp8266_print("\r\n");
_esp8266_waitFor("OK");
_esp8266_print("AT+CWMODE=");
_esp8266_putch(mode + '0');
_esp8266_print("\r\n");
_esp8266_waitFor("OK");
{
_esp8266_print("AT+CIPMODE=");
_esp8266_putch(mode + '0');
_esp8266_print("\r\n");
_esp8266_waitFor("OK");
// Connect to AP
_esp8266_print("AT+CWJAP=\"");
_esp8266_print(ssid);
_esp8266_print("\",\"");
_esp8266_print(pass);
_esp8266_print("\"\r\n");
_esp8266_waitFor("OK");
// Start TCP/UDP
_esp8266_print("AT+CIPSTART=\"");
if (protocol == ESP8266_TCP)
_esp8266_print("TCP");
else
_esp8266_print("UDP");
_esp8266_print("\",\"");
_esp8266_print(ip);
_esp8266_print("\",");
_esp8266_print(port_str);
_esp8266_print("\r\n");
_esp8266_waitFor("OK");
// Send data
void esp8266_send(void)
_esp8266_print("AT+CIPSEND\r\n");
_esp8266_waitFor("OK");
// Receive data
unsigned char i;
_esp8266_waitFor("+IPD,");
do
{
received = _esp8266_getch();
store_in[i] = _esp8266_getch();
// Disconnect
void esp8266_disconnect(void)
_esp8266_print("AT+CWQAP\r\n");
_esp8266_waitFor("OK");
void esp8266_stop_send(void)
_esp8266_print("+++");
delay_ms(2000);
// Delete TCP
void esp8266_del_TCP(void)
_esp8266_print("AT+CIPCLOSE\r\n");
_esp8266_waitFor("OK");
// Main function
void main(void)
ADCON1 |= 0x0F;
CMCON |= 7;
TRISB0_bit = 1;
TRISE0_bit = 0;
TRISE1_bit = 0;
TRISC0_bit = 0;
UART1_Init(115200);
delay_ms(100);
delay_ms(1000);
esp8266_restart();
esp8266_echoCmds(0);
esp8266_isStarted();
esp8266_mode(ESP8266_STATION);
esp8266_connect("IOT_AI LAB","rC7YNUqM");
RE0_bit = 1;
esp8266_trans_mode(ESP8266_TRANS_PASS);
esp8266_send();
RCIF_bit = 0;
PIE1.RCIE = 1;
GIE_bit = 1;
PEIE_bit = 1;
while(1)
TransmitData = 'S';
UART1_Write(TransmitData);
if(receive_flag == 1)
{
receive_flag = 0;
ReceiveData = UART1_Read();
if (ReceiveData == '@')
LATE1_bit = 1;
TransmitData = 'O';
UART1_Write(TransmitData);
LATE1_bit = 0;
TransmitData = 'F';
UART1_Write(TransmitData);
GIE_bit = 0;
RE0_bit = 0;
esp8266_stop_send();
esp8266_trans_mode(ESP8266_TRANS_NOR);
esp8266_del_TCP();