Hello all, after a few vacation periods, I started working on a small project. So I wanted to have a REST API server
on a Node MCU to get sensor data. So Let’s checkout, how did I create this small REST API server from Node MCU. I
hope this might help you with your projects too.

At end of this blog, you will be able,

What Do You Need?
  • 1x Node MCU
  • USB Cable to program Node MCU
  • Arduino Software (Link)
What am I am using to program this?
  • Arduino 1.8.13
  • Ubuntu 20.04
Let’s start…
Before start programming,
  1. You need to install Arduino software into your computer (Depending on Linux or Windows OS, your installation
    process will be changed, there are many tutorials on how to do this, so no need to talk about this 😉 )
  2. You need to install Node MCU via Boards Manager. (This
    Tutorial shows, how to install Node MCU board to your Arduino IDE
    )
  3. Install ArduinoJson library version 5.13.1.  You need to install version 5.xx. if you
    install version 6.xx or the latest version, there may be a chance that your code will not work since those
    new libraries do not contain StaticJsonBuffer data struct and some other data structs.
Let’s start coding…
First I have explained each code block below. Or you can see the source code from Github.
Including Libraries & define variables
First include the libraries, define variables as done above. Then we need to create a few functions to do different
tasks. 
Function to Initializing the WiFi

we need to call the above function to connect the Node MCU into a WiFi connection. This function will be called in
the void setup() function.
Function To Execute when HTTP GET is called

The above function will execute when the HTTP GET request is sent to Node MCU via a web browser, from an app or
anything. 

Configure REST server

Above function is used to link REST APIs with our executable functions.

void setup?
As you all know, we need to do the setup things in void setup function. Above is over void setup function. What it
does is, it first initialize the serial communication & connect to our wifi router. after connecting to WiFi
router, it config the REST server & start our REST server. Its that simple right?
void Loop?

Finally, in void loop function we keep calling this http
server client handler function to check the HTTP GET requests.
FYI: You can use this REST API to on/ off GPIO
pins/ read & get sensor data and many more.

To catch this kind of small tutorials, and things I do for fun, don’t forget to subscribe my Blogger, YouTube & follow me on GitHub.
Do you facing any issues while trying this? Let’s discuss in bellow…

***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

Leave a Reply

Your email address will not be published. Required fields are marked *