Basic Microcontroller API Consumption
This repository is part of a series on how to consume an API with a microcontroller. The other repository in the series is the microcontroller firmware: Basic-ESP-IoT.
“Consume API” means to engage with the API, this is done by sending requests to the API and receiving responses.
The API will respond to requests with data, which is often in the form of JSON. It will also respond to requests with a status code, which indicates whether the request was successful or not.
- Clone this repository or download it as a zip file.
git clone https://github.com/ThingEngineer/Basic-IoT-API.git
- Navigate to the python directory.
- Install the required packages. (sqlite3 is comes with Python 3.4+ but we will install it anyway)
pip install Flask sqlite3
- Run the API.
python iot-app.py
- Install curl.
curl is a command line tool for sending requests to APIs. It is not required but it is helpful for testing the API. This is the linux command to install curl. If you are using Windows, you can download curl from here. On macOS, curl is already installed.
sudo apt install curl
- Test the API with curl. Replace the IP address with the IP address of the computer running the API.
These are some examples of how to use curl to test the API. You can also use a tool like Postman to test the API. These API endpoints are what the microcontroller and the web app will be using to communicate.
GET all temperature readings
curl -v http://127.0.0.1:5000/api/temperature
POST a new temperature reading
curl -d 'temperature=99' http://127.0.0.1:5000/api/temperature
GET the current message
curl -v http://127.0.0.1:5000/api/message
POST a new message
curl -d 'body=Hello World' http://127.0.0.1.22:5000/api/message
- Open the web app in a browser. Replace the IP address with the IP address of the computer running the API.
http://127.0.0.1.22:5000
Here you can view the temperature readings and clear the temperature readings from the database. You can also add and update the message that is displayed on the microcontroller serial monitor.
- Clone this repository or download it as a zip file.
git clone https://github.com/ThingEngineer/Basic-IoT-API.git
- Navigate to the python directory.
- TODO
Python Docs - The official Python documentation.
Flask Docs - A Python web framework.
What is REST - A brief introduction to REST.
HTTP Status Codes - A list of HTTP status codes and what they mean.
JSON - JavaScript Object Notation, the most common data format used for APIs.
SQLite - A lightweight database.
SQLite Browser - A helpful tool for viewing and editing SQLite databases.
Postman - A helpful tool for testing APIs.