Create your own AT commands on the ESP8266

Nou doubt I will be, as we call it here in Holland, “swearing in the church” but I will show you how to create your own functions in the AT Demo. This to give you some idea how to build your own software. I am assuming you know how to create source and header files in Eclipse as I am using that for this example. Also I am assuming you have a project already created/loaded containing the AT demo as described in my previous post about installing the ESP8266 SDK under Eclipse in Ubuntu.

Create a new myFunctions.c and a new myFunctions.h.

In myFunctions.c enter the following text :


/*
 * 	myFunctions.c
 *
 *  Created on: 27 okt. 2014
 *	Author: nico
 */

//
// includes
//
#include "c_types.h"		// C types used in this applicationm
#include "at.h"				// needed for AT enumerations and sending info to caller
#include "myFunctions.h"	// my function prototypes
#include "osapi.h"			// enables OS api calls
#include "driver/uart.h"	// needed to use the uart
#include 			// standard C library functions

/**
 * @brief  command to return the MAC address of the ESP8266 to the user
 * @param  id: command id number (we actually do not need this)
 * @retval None
 */
ICACHE_FLASH_ATTR
at_exeCmdGetMacAddress(uint8_t id) {

	char temp[20];			// buffer for formatting the output (xx:xx:xx:xx:xx:xx\r\n) = 20 bytes
	uint8_t macaddr[6];		// array that will receive the MAC address from the chip
	//
	// get the mac address from the API. This is described in the  documentation from
	// SDK 0.9.1. The document is/was missing in SDK 0.9.2. If anyone needs it, let me know
	//
	wifi_get_macaddr(0x00, macaddr);
	//
	// format the a string into temp field as 6 hex numbers with a colon in between
	//
	os_sprintf(temp, "%02x:%02x:%02x:%02x:%02x:%02x\r\n", macaddr[0],
			macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
	//
	// and return the string to the caller through the uart
	//
	uart0_sendStr(temp);
	//
	// send Ok to caller
	//
	at_backOk;
}

and in myFunctions.h enter :


/*
 * myFunctions.h
 *
 * Created on: 27 okt. 2014
 * Author: nico
 */

#ifndef MYFUNCTIONS_H_
#define MYFUNCTIONS_H_
//
// function prototypes
//
ICACHE_FLASH_ATTR at_exeCmdGetMacAddress(uint8_t id);

#endif /* MYFUNCTIONS_H_ */

We are almost done. So far we have created our function which does the following:

  • get the ESP8266 mac address from the chip in an array of 6 bytes
  • convert the array in a formatted mac address (i.e. yy:yy:zz:aa:bb:cc)
  • send the formatted string through the uart0 port
  • send the text “\r\nOK\r\n” through the uart0 port

Although we have now created our function() we are not there yet. The system needs a mapping from an AT command to our function. this is done in the file at_cmd.h. This file contains an array of known commands and different actions to take place with a command like the “?” or “=” addition. this is the contents as it is received in the example AT firmware:


#ifndef __AT_CMD_H
#define __AT_CMD_H

#include "at.h"
#include "at_wifiCmd.h"
#include "at_ipCmd.h"
#include "at_baseCmd.h"

#define at_cmdNum   20

at_funcationType at_fun[at_cmdNum]={
  {NULL, 0, NULL, NULL, NULL, at_exeCmdNull},
  {"E", 1, NULL, NULL, at_setupCmdE, NULL},
  {"+RST", 4, NULL, NULL, NULL, at_exeCmdRst},
  {"+GMR", 4, NULL, NULL, NULL, at_exeCmdGmr},
  {"+CWMODE", 7, at_testCmdCwmode, at_queryCmdCwmode, at_setupCmdCwmode, NULL},
  {"+CWJAP", 6, NULL, at_queryCmdCwjap, at_setupCmdCwjap, NULL},
  {"+CWLAP", 6, NULL, NULL, at_setupCmdCwlap, at_exeCmdCwlap},
  {"+CWQAP", 6, at_testCmdCwqap, NULL, NULL, at_exeCmdCwqap},
  {"+CWSAP", 6, NULL, at_queryCmdCwsap, at_setupCmdCwsap, NULL},
  {"+CWLIF", 6, NULL, NULL, NULL, at_exeCmdCwlif},
  {"+CIFSR", 6, at_testCmdCifsr, NULL, at_setupCmdCifsr, at_exeCmdCifsr},
  {"+CIPSTATUS", 10, at_testCmdCipstatus, NULL, NULL, at_exeCmdCipstatus},
  {"+CIPSTART", 9, at_testCmdCipstart, NULL, at_setupCmdCipstart, NULL},
  {"+CIPCLOSE", 9, at_testCmdCipclose, NULL, at_setupCmdCipclose, at_exeCmdCipclose},
  {"+CIPSEND", 8, at_testCmdCipsend, NULL, at_setupCmdCipsend, at_exeCmdCipsend},
  {"+CIPMUX", 7, NULL, at_queryCmdCipmux, at_setupCmdCipmux, NULL},
  {"+CIPSERVER", 10, NULL, NULL,at_setupCmdCipserver, NULL},
  {"+CIPMODE", 8, NULL, at_queryCmdCipmode, at_setupCmdCipmode, NULL},
  {"+CIPSTO", 7, NULL, at_queryCmdCipsto, at_setupCmdCipsto, NULL},
  {"+CIUPDATE", 9, NULL, NULL, NULL, at_exeCmdUpdate}
};

#endif

I will not go into details of how the array works although it is quite obvious looking at the names of the different functions. Now let’s expand this array with our extra user command. Expand the array like this:


#ifndef __AT_CMD_H
#define __AT_CMD_H

#include "at.h"
#include "at_wifiCmd.h"
#include "at_ipCmd.h"
#include "at_baseCmd.h"

#define at_cmdNum   21

at_funcationType at_fun[at_cmdNum]={
  {NULL, 0, NULL, NULL, NULL, at_exeCmdNull},
  {"E", 1, NULL, NULL, at_setupCmdE, NULL},
  {"+RST", 4, NULL, NULL, NULL, at_exeCmdRst},
  {"+GMR", 4, NULL, NULL, NULL, at_exeCmdGmr},
  {"+CWMODE", 7, at_testCmdCwmode, at_queryCmdCwmode, at_setupCmdCwmode, NULL},
  {"+CWJAP", 6, NULL, at_queryCmdCwjap, at_setupCmdCwjap, NULL},
  {"+CWLAP", 6, NULL, NULL, at_setupCmdCwlap, at_exeCmdCwlap},
  {"+CWQAP", 6, at_testCmdCwqap, NULL, NULL, at_exeCmdCwqap},
  {"+CWSAP", 6, NULL, at_queryCmdCwsap, at_setupCmdCwsap, NULL},
  {"+CWLIF", 6, NULL, NULL, NULL, at_exeCmdCwlif},
  {"+CIFSR", 6, at_testCmdCifsr, NULL, at_setupCmdCifsr, at_exeCmdCifsr},
  {"+CIPSTATUS", 10, at_testCmdCipstatus, NULL, NULL, at_exeCmdCipstatus},
  {"+CIPSTART", 9, at_testCmdCipstart, NULL, at_setupCmdCipstart, NULL},
  {"+CIPCLOSE", 9, at_testCmdCipclose, NULL, at_setupCmdCipclose, at_exeCmdCipclose},
  {"+CIPSEND", 8, at_testCmdCipsend, NULL, at_setupCmdCipsend, at_exeCmdCipsend},
  {"+CIPMUX", 7, NULL, at_queryCmdCipmux, at_setupCmdCipmux, NULL},
  {"+CIPSERVER", 10, NULL, NULL,at_setupCmdCipserver, NULL},
  {"+CIPMODE", 8, NULL, at_queryCmdCipmode, at_setupCmdCipmode, NULL},
  {"+CIPSTO", 7, NULL, at_queryCmdCipsto, at_setupCmdCipsto, NULL},
  {"+CIUPDATE", 9, NULL, NULL, NULL, at_exeCmdUpdate},
  //
  // user commands
  //
  {"+CIUSRMAC", 9, NULL, NULL, NULL, at_exeCmdGetMacAddress}
};

#endif

Notice the extra comma (“,”) after at_exeCmdUpdate. As the array shows I have added the function CIUSRMAC and if entered like “AT+CIUSRMAC” the function at_exeCmdGetMacAddress() will be called which is in our myFunctions.c.

Thats it….. compile and upload it into the firmware and try it out.
 

Geef een reactie