Net Command is a proof-of-concept project that enables users to control an ESP8266 device from anywhere in the world using a website. It demonstrates the integration of a web application with IoT hardware, using a database and APIs for seamless communication.
- Host a website to control your ESP8266 device inside a Docker container.
- Store device control data in a MySQL database.
- Secure API with JWT authentication for authorized access.
- Simple setup and deployment process.
- PlatformIO project for ESP8266 firmware development.
- Easy to scale and deploy using Docker.
- Docker
- Web server (handled by Docker)
- ESP8266 board
- PlatformIO IDE for firmware upload
- PHP (for the API)
- MySQL (for the database)
- JWT for API authentication
git clone https://github.com/ktauchathuranga/net-command.git
- Navigate to the
net-command/public_html
folder. - Build and run the Docker container:
docker-compose up --build
- Once the container is running, the website will be accessible locally at
http://localhost
(or your external domain if deployed). - The MySQL database will be automatically set up and populated with the necessary data during the container initialization, so there's no need to manually import the SQL file.
- Open the
net-command/netcommand_8266
folder in PlatformIO. - Update the Wi-Fi credentials and other I/O details in the firmware code.
- Replace the placeholder API URL with your hosted website URL (use your Docker container's IP or a domain name if hosted externally):
const char* ssid = "YOUR_WIFI_NAME"; const char* password = "YOUR_WIFI_PASSWORD"; const char* serverUrl = "http://localhost/api/command"; // or use your public domain const String jwtToken = "YOUR_JWT_TOKEN"; // Set your JWT token here
- Upload the code to your ESP8266 board.
-
Login to obtain a JWT Token: To authenticate users before accessing the API, they must first log in by sending their username and password. Here's how to obtain a JWT token:
API Endpoint:
POST /api/login
Parameters:username
,password
Example request using
curl
:curl -X POST -d "username=your_user&password=your_pass" http://localhost/api/login
Response:
{ "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." }
-
Accessing the Command API: To interact with the command API, users must include the JWT token in the
Authorization
header. For example:API Endpoint:
POST /api/command
Parameters:readstate
,writestate
Example request using
curl
:curl -X POST -H "Authorization: Bearer YOUR_JWT_TOKEN" -d "readstate=1" http://localhost/api/command
- Replace
YOUR_JWT_TOKEN
with the token you received after login. - Replace
readstate=1
with the appropriate data for the API request.
- Replace
- Access the hosted website at
http://localhost
(or your external domain if deployed). - Log in using your credentials to obtain a JWT token.
- Interact with the controls on the website to send commands to your ESP8266 device via the secure API.
Feel free to fork the repository and contribute to the project. Create a pull request with detailed changes to improve functionality.
This project is licensed under the MIT License. See the LICENSE file for details.
Watch the demo
View a short version
docker-compose down
: Stops and removes containers, networks, but keeps named volumes.docker-compose down -v
: Stops and removes containers, networks, and also deletes named volumes.