Use an ATmega microcontroller (atmega328p) to control an at command device (modem) at the example of an esp8266 wifi module

tutorials › atmega 21.12.16 scenprogs

In this article I will show you my solution to control an at command device with an atmega microcontroller through the build in uart interface of an atmega microcontroller in avr-gcc.

Controlling an at command device is from technical perspective a serial and asynchron half-duplex communication with a software handshake between a master device (microcontroller) and a slave which is a modem or an at cmd device. The master device sends AT-commands which are consecutive ascii characters (strings) and which contain the instruction information for the at command device. The device afterwards interacts with the given instruction and normally responds through an OK for success or an ERROR for a fault.

The following tutorial (code) has been written to setup an esp8266 wifi chip, but you can easily change the code to send for example short messages (SMS) through a gsm modem. The article doesn’t cover any information about uart. Therefore if you dont know howto setup uart of an atmega you might have a look for uart programming on atmel microcontrollers first.

uart rx interrupt service routine

The uart rx interrupt is called every time a new charater has been written to the uart input register UDRO and is used to fill a string until the modem answers with OK or ERROR. Every time a carriage return or a newline was detected we proof afterwards if the the uart string contains an OK or an ERROR so far.

Please note: The modem returns always contain a carriage return and a newline, the actually full modem return is OK\r\n or ERROR\r\n. Therefore it is acceptable to check the uart string for OK and ERROR always only then if a newline or carriage return has been received.

The variable UART_AT is used to set the routine in an AT-CMD-Mode which forces the routine to read in characters until the modem answers with OK or ERROR. If it keeps disabled the routine reads in uart strings until a carriage return or a newline was detected. The indication flags for an full received modem answer is the variable AT_CMD_COMPLETE, while a full received string in normal uart mode is signed through UART_STR_COMPLETE.

function at_writecmd

We now know how we read in the modem returns. We also need a function which builds and sends our at commands to the modem.

Since we dont know the response time (the time the modem needs to handle the given instruction) we need a functionality to wait for the modem answers or detect in addition to it a timeout if the modem doens’t answer. This is implemented through an do-while-loop wherein the program sequence is paused until an at command answer has been received through the uart interrupt service routine. This functionality is essential if you think for example about the mode command AT+CMGS which instructs a modem to send a short message. The modem will surely take at least 5 seconds to give an answer back. The routine has to wait at this point for the reaction of a modem.

Please note: The timeout is not a fixed duration the routine waits for the modem answer, it is the maximum time the routine waits for the modem to answer and used to give the routine an error treatment if the modem doens’t answer.

howto use  the function at_writecmd

This is the point where our work will pay off. The method shows how the function at_writecmd is used. You just give it the at command as a string value and the timeout (which is the maximum time and little more the modem is allowed to answer) and the function will return 1 for success or 0 for error.

the whole code sticked together

Written for an atmega328p with atmel studio 7. Probably adjustable for all atmegas.

If you use an another atmel mircocontroller make sure to adjust the F_CPU and the fitting registers.


test circuit

If there are any questions left please let me know.