Giter Site home page Giter Site logo

asyncts's Introduction

AsyncTS

Asynchronous ThingSpeak Library for ESP8266 & ESP32.

Why is it better than sychronous?

In synchronous mode, the program waits for the server to respond. It will only continue to run after the server replies. (Or max 5 sec if no response is received). In asynchronous mode, while the server is thinking about the answer, your program can be busy with other tasks.

Brief overview

The AsyncTS client can handle one request at a time. And until it finishes the previous one, it returns "false". So the new request has not been sent, if read or write functions return "false". Before sending a request, you must set up a callback function to process server responses. The request type can be of two types, either it sends data to the server for storage or it requests to retrieve stored data.
For write functions you have to define a callback function like this:

AsyncClient client;
AsyncTS ats;
void UserCallbackForAnyWriteFuncton(int code)
{
  //you can do something with return code.
  Serial.println("Server response:" + String(code));
}
int intvalue=4;//data for sending
ats.begin(client);
ats.onWriteServerResponseUserCB(UserCallbackForAnyWriteFuncton);
ats.writeField(channelID,field_num,intvalue,writeAPIkey);

This is true for all writing functions. Since the server returns only an error code of type int.
For read functions you have to define a callback functions with an int parmeter for return code, and a std::any* for the server response. Inside the function, you must create a pointer to the type required by the function using std::any_cast<T>. If you cast to an incorrect type, you get a null pointer. Example:

AsyncClient client;
AsyncTS ats;
void IntResponse(int errorcode, std::any* resp)
{   
    //try any_cast<int>
    auto* a = std::any_cast<int>(resp);
    if (!a) 
    {
      // Type-mismatch
      Serial.println(" Bad_any_cast<int>");
      return;
    }
      Serial.println("Response code:" + String(errorcode));
      Serial.println("Response: " + String(*a));//dereference pointer
}
...

ats.begin(client);
ats.onReadServerResponseUserCB(IntResponse);
ats.readIntField(channelID,field_num,readAPIkey);

Note

To ESP32 platform I could only compile with Visual Studio Code - PlatformIO IDE. For success, put these lines in the platformio.ini file (below your ESP32 env. section).

[env:ESP32]

build_flags =
    -std=gnu++17
build_unflags =
   -std=gnu++11

Documentation here:https://dzsoni.github.io/AsyncTS/html/index.html

asyncts's People

Contributors

dzsoni avatar

Watchers

 avatar

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.