sbit led at RA4_bit;

sbit OUT_1 at RB0_bit;
sbit OUT_2 at RB1_bit;
sbit OUT_3 at RB2_bit;
sbit OUT_4 at RB3_bit;

sbit Led_1 at RA0_bit;
sbit Led_2 at RA1_bit;
sbit Led_3 at RA2_bit;
sbit Led_4 at RA3_bit;

char tmp;

const Status_wait_OK = 1;
const Status_wait_ready = 2;
const Status_wait_IPD = 3;
const Status_wait_to_send = 4;

bit response_OK;
bit wait_ready;
bit received_message;
bit received_output;
bit ready_to_send;

char mv_status =0;

char state = 0;

char data_to_send[200];
char httpHeader[]= "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\Content-Length: ";
char httpHeader_2[]="Connection: close";
char new_line[] = "\r\n";

char content_length;
char content_length_txt[4];

char content[50];
char *outNumber;
char *outStatus;

char *outStatus_1;
char *outStatus_2;
char *outStatus_3;
char *outStatus_4;

char txt_OFF[] = "OFF";
char txt_ON[] = "ON";
char txt_OUT_1[] = "OUT_1";
char txt_OUT_2[] = "OUT_2";
char txt_OUT_3[] = "OUT_3";
char txt_OUT_4[] = "OUT_4";

char comma_txt[] = ",";

char data_length;
char data_length_txt[4];

char cipSend[100];
char txt_AT_CIPSEND[] = "AT+CIPSEND=";

char connectionId;
char connectionId_txt[4];

char pinNumber;
char secondNumber;


void ESP8266_setup();
void send_response();
void content_1();
void content_2();
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void interrupt() {

if (RCIF_bit == 1) {tmp = UART1_Read();RCIF_Bit = 0;};

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//Status_wait_to_send
if(mv_status==Status_wait_to_send){

switch (state) {
case 0: {
if (tmp == '>') {ready_to_send = 1;}
state = 0;
break;
}

default: {
state = 0;
break;
}
} //end switch wait_to_send
} // end if

//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if(mv_status==Status_wait_OK){

switch (state) {
case 0: {
if (tmp == 'O') { state = 1;}
break;
}

case 1: {
if (tmp == 'K') {response_OK = 1;}
state = 0;
break;
}
default: {
state = 0;
break;
}
} //end switch wait_OK
} // end if
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if(mv_status==Status_wait_ready){

switch (state) {
case 0: {
if (tmp == 'r') { state = 1;}
break;
}

case 1: {
if (tmp == 'e') {state = 2;}
break;
}

case 2: {
if (tmp == 'a') {state = 3;}
break;
}

case 3: {
if (tmp == 'd') {state = 4;}
break;
}

case 4: {
if (tmp == 'y') {wait_ready = 1;}
state = 0;
break;
}

default: {
state = 0;
break;
}
} //end switch wait_ready
} // end if
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if(mv_status==Status_wait_IPD){

switch (state) {
case 0: {
if (tmp == '+') { state = 1;}
break;
}

case 1: {
if (tmp == 'I') {state = 2;}
break;
}

case 2: {
if (tmp == 'P') {state = 3;}
break;
}

case 3: {
if (tmp == 'D') {state = 4;}
break;
}

case 4: {
if (tmp == ',') {state = 5;}
break;
}

case 5: {
connectionId=tmp-48;
state = 6;
break;
}

//'''''''''''''''''''''''''''''''''''''''''''''''
case 6: {
if (tmp == 'o') {state = 7;} else {state = 6;}
break;
}

case 7: {
if (tmp == 'u') {state = 8;}
break;
}

case 8: {
if (tmp == 't') {state = 9;}
break;
}

case 9: {
if (tmp == '=') {state = 10;received_message=1;}
break;
}

case 10: {
if (tmp == '1') {pinNumber = 1; state = 11;}
break;
}

case 11: {
secondNumber = tmp-48;
received_output=1;
state = 0;
break;
}


default: {
state = 0;
break;
}
} //end switch wait_ready
} // end if


}//end interrupt
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void main() {

ANSEL=0;
ADCON0 = 0;

RCSTA = 0;


//Disbale comparators
C1ON_bit=0;
C2ON_bit=0;

TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b10000000;
TRISD = 0b00000000;
TRISE = 0b00000000;

PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE=0;

//uart setup
UART1_Init(9600);
RCIF_Bit = 0;
RCIE_Bit = 1;
PEIE_Bit = 1;
GIE_Bit = 1;

PIE1.RCIE = 1; //leidzia USART read pertraukimus
INTCON.GIE = 1; // global interrput enable
INTCON.PEIE = 1; // Peripheral Interrupt Enable

led=0;
response_OK=0;
wait_ready=0;

delay_ms(2000);


//''''''''''''''''''''''''''''''''''''''''''''''

ESP8266_setup();

received_message=0;
received_output=0;

mv_status=Status_wait_IPD;


while(1){

if(received_message==1)
{

if(received_output==1)
{


if(OUT_1==1) {outStatus_1=txt_ON;} else {outStatus_1=txt_OFF;}
if(OUT_2==1) {outStatus_2=txt_ON;} else {outStatus_2=txt_OFF;}
if(OUT_3==1) {outStatus_3=txt_ON;} else {outStatus_3=txt_OFF;}
if(OUT_4==1) {outStatus_4=txt_ON;} else {outStatus_4=txt_OFF;}

if(secondNumber==1) { OUT_1 = ~OUT_1;Led_1=~Led_1; outNumber="OUT_1"; if(OUT_1==1) {outStatus=txt_ON;} else {outStatus=txt_OFF;}}
if(secondNumber==2) { OUT_2 = ~OUT_2;Led_2=~Led_2; outNumber="OUT_2"; if(OUT_2==1) {outStatus=txt_ON;} else {outStatus=txt_OFF;}}
if(secondNumber==3) { OUT_3 = ~OUT_3;Led_3=~Led_3; outNumber="OUT_3"; if(OUT_3==1) {outStatus=txt_ON;} else {outStatus=txt_OFF;}}
if(secondNumber==4) { OUT_4 = ~OUT_4;Led_4=~Led_4; outNumber="OUT_4"; if(OUT_4==1) {outStatus=txt_ON;} else {outStatus=txt_OFF;}}

if(secondNumber==9) { content_2(); } else { content_1(); }


send_response();

mv_status=Status_wait_IPD;
received_message=0;
received_output=0;

}//end if received_output

}//end if received_message

}//end while

} //end main
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void ESP8266_setup()
{
mv_status=Status_wait_ready;
UART1_write_Text("AT+RST\r\n"); // reset module
while(wait_ready==0) { led=1;};
wait_ready=0;
response_OK=0;
state = 0;
led=0;


mv_status=Status_wait_OK;
UART1_write_Text("AT+CWMODE=1\r\n"); // configure as access point
while(response_OK==0) { led=1;}
response_OK=0;
state = 0;
led=0;


UART1_write_Text("AT+CIPMUX=1\r\n"); // configure for multiple connections
while(response_OK==0) { led=1;}
response_OK=0;
state = 0;
led=0;

UART1_write_Text("AT+CIPSERVER=1,40002\r\n"); // turn on server on port
while(response_OK==0) { led=1;}
response_OK=0;
state = 0;
led=0;
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void content_1()
{
//sudarome stringa content
content[0]=0x00;
strcat(content,outNumber);
strcat(content," ");
strcat(content,outStatus);

//content stringo ilgis
content_length= strlen(content);
ByteToStr(content_length, content_length_txt);
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void content_2()
{
//sudarome stringa content
content[0]=0x00;
strcat(content,"OUT_1");
strcat(content," ");
strcat(content,outStatus_1);
strcat(content,new_line);

strcat(content,"OUT_2");
strcat(content," ");
strcat(content,outStatus_2);
strcat(content,new_line);

strcat(content,"OUT_3");
strcat(content," ");
strcat(content,outStatus_3);
strcat(content,new_line);

strcat(content,"OUT_4");
strcat(content," ");
strcat(content,outStatus_4);
strcat(content,new_line);

//content stringo ilgis
content_length= strlen(content)-5;
ByteToStr(content_length, content_length_txt);
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void send_response()
{

//sudarome stringa data_to_send
data_to_send[0]=0x00;

strcat(data_to_send,httpHeader); //+httpHeader

strcat(data_to_send,ltrim(content_length_txt));//+content_length

strcat(data_to_send,new_line); //+"\r\n"

strcat(data_to_send,httpHeader_2); //+"Connection: close";
strcat(data_to_send,new_line); //+"\r\n"
strcat(data_to_send,new_line); //+"\r\n"

//pridedame content
strcat(data_to_send,content); //+content

//paskaiciuojame data_to_send stringo ilgi
data_length= strlen(data_to_send)-5; //
ByteToStr(data_length, data_length_txt);
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

//sudarome stringa cipSend
cipSend[0]=0x00;
strcat(cipSend,txt_AT_CIPSEND); //+"AT+CIPSEND="

ByteToStr(connectionId, connectionId_txt);

strcat(cipSend,ltrim(connectionId_txt)); //+connectionId
strcat(cipSend,comma_txt); //+","
strcat(cipSend,ltrim(data_length_txt)); //+data_length
strcat(cipSend,new_line); //+"\r\n"


mv_status=Status_wait_to_send;
ready_to_send=0;
UART1_Write_Text(cipSend);
while(ready_to_send==0) { led=1;};
ready_to_send=0;
led=0;

UART1_Write_Text(data_to_send);
UART1_write_Text("\r\n");
UART1_write_Text(" "); //cia kazkoks bugas, dar reikia pasiusti 5 dummy simbolius

response_OK=0;
mv_status=Status_wait_OK;
while(response_OK==0) { led=1;};
response_OK=0;
led=0;

}