I'm trying to send AT commands from my PIC18F26K80 device, but I failed to find any detailed explanation about HOW to send the ASCII commands over the UART (formatting, terminating char/chars, delays, etc) and what responses I should expect to receive, how they are structured and how can I interpet them.
The following code is my attempt according to the documentation I did find:
#include "DataTypes.h"
#include "HardwareProfile.h"
#define STATUS_UNKNOWN 0
#define STATUS_OK 1
#define STATUS_ERROR 2
#define STATUS_NO_CHANGE 3
#define STATUS_FAIL 4
#define STATUS_READY 5
#define AT_COMMAND_BUFFER_SIZE 256
CHAR buffer[AT_COMMAND_BUFFER_SIZE];
UINT16 i = 0;
UINT8 ReadResponse()
{
CHAR receivedByte;
UINT16 responseStatus = STATUS_UNKNOWN;
BOOL responseReceived = FALSE;
i = 0;
while(!responseReceived)
{
if(UART1_Data_Ready())
{
receivedByte = UART1_Read();
if(receivedByte == '\r')
{
buffer[i] = 0;
if(strstr(buffer, "OK"))
responseStatus = STATUS_OK;
else if(strstr(buffer, "ERROR"))
responseStatus = STATUS_ERROR;
else if(strstr(buffer, "no change"))
responseStatus = STATUS_NO_CHANGE;
else if(strstr(buffer, "FAIL"))
responseStatus = STATUS_FAIL;
else if(strstr(buffer, "ready"))
responseStatus = STATUS_READY;
responseReceived = TRUE;
}
else
{
buffer[i] = receivedByte;
i++;
if(i == AT_COMMAND_BUFFER_SIZE)
responseReceived = TRUE; // TODO set response status to unknown size?
}
}
}
return responseStatus;
}
void WaitForStatus(UINT8 status)
{
while(ReadResponse() != status)
;
}
void main()
{
volatile UINT8 response;
LED_TEST1_TRIS = 0;
LED_TEST1_LAT = 1;
LED_TEST2_TRIS = 0;
LED_TEST2_LAT = 1;
UART1_Init(115200);
Delay_ms(100);
// Hard reset the ESP8266
TRISC5_bit = 0;
LATC5_bit = 0;
Delay_ms(100);
LATC5_bit = 1;
Delay_ms(20);
WaitForStatus(STATUS_READY); // PROGRAM HANGS HERE, NEVER RECEIVES "ready" string
UART1_Write_Text("AT\r\n");
WaitForStatus(STATUS_OK);
LED_TEST1_LAT = 0;
while(1)
{
Delay_ms(500);
}
}
The program hangs while waiting for the "ready" string from the UART. Any suggestions for why this is happening?
Also, is there any tutorial/document about how to send and receive AT commands via UART on an embedded platform using ANSI C?
Thanks in advanced!