Coder Social home page Coder Social logo

atcommands's Introduction

ATCommands

This is an Arduino library for folks who love the simplicity of AT-style commands.

Inspiration

I do a lot of work with ESP8266, ESP32 and various other radio-type modules for LoRA, GSM and other things. One thing most of them have in common is that you can either take the easy road and use their 'AT' commands via UART or take the long way and struggle through with SPI and registers.

I have noticed some folks doing amazing things with Arduinos in the areas of robotics and communications right up to the point where integration with another microcontroller or the PC is required. Here some 'interesting' inventions have emerged with bespoke serial protocols, i2c magick or even more SPI.

This library will present an option to use the UART serial port, make use of existing libraries and neatly control the flow of parameters and communication, your way :-)

Goals

  • Small-ish footprint but realistically workable by the end user. I could spent time malloc and realloc my way through things but Arduino has ways to do Arduino things. So until that does not work anymore...
  • Uncomplicated. I've seen other command parses require some elaborate definitions of structs and arrays for commands and variables which until a use case presents itself is not done here.
  • Uses existing Serial class.
  • Lots of FREEDOM to implement own functions.

Command Structure

The Hayes AT command structure on which this is loosely based has four defined command types.

TEST Command

AT+START=?                                              # TEST command
+START=<MODE:UINT32[WO]>                                # Instruction response
Start scanning after write (0 - wifi, 1 - bluetooth).   # Description response
OK                                                      # Automatic acknowledge

WRITE Command

AT+START=0                                              # WRITE command
+SCAN=-10,"wifi1"                                       # Unsolicited read response
+SCAN=-50,"wifi2"                                       # Unsolicited read response
+SCAN=-20,"wifi3"                                       # Unsolicited read response
OK     

READ Command

AT+BAUD?                                                # Query baud rate
+BAUD=115200,8,N,1                                      # Response
OK

RUN Command

ATZ                                                     # RESET 
OK

The ATCommands Object

Life begins with the inclusion of the header and the declaration of the ATCommands object as follows:

#include <ATCommands.h>

ATCommands AT

Defining Callback Functions

One of the more interesting features of this library is that commands are registered at the start of the program and during execution callbacks are invoked to run the specific functions.

All that mumbo-jumbo just means this :-

First, we need to define our RUN, READ, WRITE or TEST commands in a structured and agreed contract.

bool at_command(ATCommands *sender)
{
    ...
}

This definition has some important properties. The sender pointer contains a number of objects of information as well as commands providing internal access to the library. Moreover the return type (true/false) determines whether OK or ERROR is eventually printed on the terminal.

Adding Commands To An Array

Rarely does just one command exist so this library provides a convenient way to register commands in bulk through a uniform structure.

static at_command_t commands[] = {
    {"+PRINT", at_run_cmd_print, at_test_cmd_print, at_read_cmd_print, at_write_cmd_print},
};

The declaration above will:

  • bind AT+PRINT to the function at_run_cmd_print
  • bind AT+PRINT=? to the function at_test_cmd_print
  • bind AT+PRINT? to the function at_read_cmd_print
  • bind AT+PRINT=Hello,World to the function at_write_cmd_print

Registering The Command Array

Once the command array has been defined it can be registered in the setup() section of your sketch along with the serial port and other parameters.

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);

    AT.begin(&Serial, commands, sizeof(commands), WORKING_BUFFER_SIZE);
}

Here we declare our UART that will be servicing the AT stream as well as the command array. We also pre-declare the array size since it cannot be read as a pointer once passed to the library.

Lastly we declare a buffer size for the library which should be as big as the length of the input you are specting to receive.

If you expect short commands with few parameters then the buffer can be smaller. This is important because the buffer's memory is reserved by the library.

Calling Update

In the sketch's loop function call update() to read the serial port and process any incoming data.

void loop()
{
    // put your main code here, to run repeatedly:
    AT.update();
}

Contributions

This library is fairly basic and does a good job covering a fair amount of use cases but still requires some more real-world use and testing. If you do find a bug please report it via the Issues Tracker.

If, on the other hand, you are super awesome and find a bug AND fix it, or make an improvement no matter how big or small please consider submitting a pull request. Your awesome is everyone's awesome!

Credits

Many people do amazing things of whose tips, techniques and even bits of code made it in here:

  • SerialCommands by ppedro74. I liked the way commands were registered and the 'sender' concept.
  • cAT by Marcin Borowicz. One day I might yet port this library to that of cAT.

TODO

On the list:

  • Unsolicited responses (with threading)

atcommands's People

Contributors

0x657373656c6c avatar berrak avatar kebaranas avatar

Stargazers

thegala avatar  avatar Jens avatar arthur avatar  avatar Andreas Motl avatar CKD avatar  avatar Orkhan Amiraslan avatar HUANG Cheng avatar James White avatar Hisaya OKADA avatar  avatar  avatar  avatar Daniel Medina avatar irieda avatar  avatar Dong Gyu, Lee avatar  avatar  avatar  avatar Chris Wang avatar pc_magas avatar Mathis Guyot avatar Roche Media avatar  avatar Atisit Naknawa avatar  avatar Christian Hartinger avatar John de Graaff avatar

Watchers

James Cloos avatar  avatar 七里台技工 avatar

atcommands's Issues

I met a problem when run the example on ESP8266(Wemos D1 R1)

In file included from D:\CProject\ArduinoProject\SerialRead_At\SerialRead_At.ino:13:
D:\CProject\ArduinoProject\libraries\ATCommands\src/ATCommands.h:91:20: error: extra qualification 'ATCommands::' on member 'hexToChar' [-fpermissive]
91 | static uint8_t ATCommands::hexToChar(const char ch);
| ^~~~~~~~~~
exit status 1
为开发板 LOLIN(WeMos) D1 R1 编译时出错。

Build errors on BasicATCommands.ino

Using Arduino IDE 2.2.1 the BasicATCommands example of ATCommands library 0.0.1 fails out of the box on:

In file included from ......\BasicATCommands\BasicATCommands.ino:15:0:
....libraries\ATCommands\src/ATCommands.h:91:20: error: extra qualification 'ATCommands::' on member 'hexToChar' [-fpermissive]
static uint8_t ATCommands::hexToChar(const char ch);

Not Working

This library does not seem to be working, I've tried everything. All AT commands just return Error. I am using the Arduino Nano

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.