-->
Page 1 of 2

Sending AT Commands via UART serial port from PIC device

PostPosted: Wed Sep 30, 2015 11:43 am
by Tomer Abramovich
Hello,

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:


Code: Select all#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!

Re: Sending AT Commands via UART serial port from PIC device

PostPosted: Wed Sep 30, 2015 2:10 pm
by Barnabybear
Hi, the link below show some responces from the AT commands.
At the bottom of page 5 it shows the responce from AT+RST which is the responce you will get from a reset. If you don't allready you'll need to filter the responce for the "ready".

http://rancidbacon.com/files/kiwicon8/E ... _1.0.4.pdf

Re: Sending AT Commands via UART serial port from PIC device

PostPosted: Wed Sep 30, 2015 5:05 pm
by kenn
For learning to use the AT command firmware, I spent alot of time just using a USB-serial cable to interface an ESP-01 to my laptop. First I just used a terminal, and then I started writing Python scripts to drive the firmware. (you could also write in C, C++, or anything else you're comfortable with). It's real easy to keep trying AT functions and calls. Once something is working from a PC, it would be pretty easy to rewrite it in C for your PIC.

Re: Sending AT Commands via UART serial port from PIC device

PostPosted: Fri Oct 09, 2015 9:48 am
by Tomer Abramovich
For learning to use the AT command firmware, I spent alot of time just using a USB-serial cable to interface an ESP-01 to my laptop. First I just used a terminal, and then I started writing Python scripts to drive the firmware. (you could also write in C, C++, or anything else you're comfortable with). It's real easy to keep trying AT functions and calls. Once something is working from a PC, it would be pretty easy to rewrite it in C for your PIC.


I tried to connect to my ESP-08 via FTDI but I can't open the serial connection from my computer. I found that Windows assigned it to COM6 and when I open this port via Putty and hard reset the module I receive a few garbage characters (maybe 4-8) and that's it. I also tried a few different baud rates but all results in the same thing. Is there something else I need to configure? Like data bits, stop bits, parity, flow control, etc...

EDIT:

Using 76800 baud rate I receive this message:

Image