Code Mikro WIFI
Code Mikro WIFI
h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// ESP8266 Constants
#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
void interrupt(void)
RCIF_bit = 0;
ReceiveData = UART1_Read();
receive_flag = 1;
if (INT0IF_bit)
mode = 1;
isChangedMode = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'A';
esp8266_tx_count++;
esp8266_tx_flag = 1;
else if (INT1IF_bit)
mode = 2;
isChangedMode = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'B';
esp8266_tx_count++;
esp8266_tx_flag = 1;
else if (INT2IF_bit)
mode = 3;
isChangedMode = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'C';
esp8266_tx_count++;
esp8266_tx_flag = 1;
void Process_ESP8266_TX_Queue()
if (esp8266_tx_flag) {
unsigned char i;
_esp8266_putch(esp8266_tx_buffer[i]);
esp8266_tx_count = 0;
esp8266_tx_flag = 0;
}
// ===== ESP8266 COMMUNICATION FUNCTIONS =====
// 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
void esp8266_connect(unsigned char* ssid, unsigned char* pass)
_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");
switch(state)
default: break;
void Update_LED_Status()
if (currentLedState != lastLedState)
esp8266_tx_buffer[esp8266_tx_count] = LED_CODES[currentLedState];
esp8266_tx_count++;
esp8266_tx_flag = 1;
lastLedState = currentLedState;
// Configure buttons
void Process_Received_Data()
if (receive_flag)
receive_flag = 0;
if(ReceiveData == '@')
mode = 1;
isChangedMode = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'A';
esp8266_tx_count++;
esp8266_tx_flag = 1;
mode = 2;
isChangedMode = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'B';
esp8266_tx_count++;
esp8266_tx_flag = 1;
mode = 3;
isChangedMode = 1;
// Queue response instead of direct send
esp8266_tx_buffer[esp8266_tx_count] = 'C';
esp8266_tx_count++;
esp8266_tx_flag = 1;
specialCommandReceived = 1;
Configure_Buttons(0);
esp8266_tx_buffer[esp8266_tx_count] = 'F';
esp8266_tx_count++;
esp8266_tx_flag = 1;
specialCommandReceived = 1;
Configure_Buttons(1);
esp8266_tx_buffer[esp8266_tx_count] = 'O';
esp8266_tx_count++;
esp8266_tx_flag = 1;
{
specialCommandReceived = 1; // Mark as special command
esp8266_tx_buffer[esp8266_tx_count] = 'N';
esp8266_tx_count++;
esp8266_tx_flag = 1;
esp8266_tx_buffer[esp8266_tx_count] = 'T';
esp8266_tx_count++;
esp8266_tx_flag = 1;
GIE_bit = 0;
esp8266_stop_send();
esp8266_trans_mode(ESP8266_TRANS_NOR);
esp8266_del_TCP();
switch(ReceiveData)
esp8266_tx_buffer[esp8266_tx_count] = ReceiveData;
esp8266_tx_count++;
esp8266_tx_flag = 1;
switch(ReceiveData)
esp8266_tx_buffer[esp8266_tx_count] = ReceiveData;
esp8266_tx_count++;
esp8266_tx_flag = 1;
switch(ReceiveData)
esp8266_tx_buffer[esp8266_tx_count] = ReceiveData;
esp8266_tx_count++;
esp8266_tx_flag = 1;
if(specialCommandReceived)
isChangedMode = 0;
unsigned int i;
void Day_Traffic_Cycle()
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(yellow_time);
if(isChangedMode) return;
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(red_time);
}
void Night_Traffic_Cycle()
Control_LED(2);
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(1000);
if(isChangedMode) return;
Control_LED(0);
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(1000);
void main(void)
ADCON1 |= 0x0F;
CMCON |= 7;
UART1_Init(115200);
delay_ms(100);
delay_ms(1000);
esp8266_restart();
esp8266_echoCmds(0);
esp8266_isStarted();
esp8266_mode(ESP8266_STATION);
esp8266_connect("trinhoa","19122003");
esp8266_trans_mode(ESP8266_TRANS_PASS);
esp8266_send();
RCIF_bit = 0;
PIE1.RCIE = 1;
GIE_bit = 1;
PEIE_bit = 1;
currentLedState = 0;
lastLedState = 0;
isChangedMode = 0;
receive_flag = 0;
specialCommandReceived = 0;
esp8266_tx_count = 0;
esp8266_tx_flag = 0;
// Main loop
while(1)
if(receive_flag)
Process_Received_Data();
Process_ESP8266_TX_Queue();
// Process by mode
switch(mode)
Update_LED_Status();
Process_ESP8266_TX_Queue();
while(!isChangedMode)
Process_ESP8266_TX_Queue();
Update_LED_Status();
Process_ESP8266_TX_Queue();
break;
while(!isChangedMode)
Control_LED(2);
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(1000);
if(isChangedMode) break;
Control_LED(0);
Update_LED_Status();
Process_ESP8266_TX_Queue();
Custom_delay_ms(1000);
break;
while(!isChangedMode)
if(TimeMode == 'N')
if(isChangedMode) break;
break;