This project provides a Docker image that runs a Python script as a service. The script reads data from a Modbus host and exposes it as a simple API.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
You will need Docker installed on your machine. Visit Docker's website to download and install Docker Desktop.
To build the Docker image, navigate to the directory containing the Dockerfile
and run the following command:
docker build -t modbus2api .
To run the Docker image, use the following command:
docker run -d -p <PORT>:80 -e MODBUS_HOST=<HOST_IP> -e MODBUS_PORT=<HOST_PORT> -e MODBUS_REGISTER=<REGISTER> modbus2api
Replace with the port on which to publish the endpoint, <HOST_IP>, <HOST_PORT> and with your Modbus host, port and register, respectively.
Once the Docker container is running, you can access the API by sending a GET request to :/regval.
- Currently only extracts 1 register
- Multi-register values are not supported (no Hi/Low bytes)
- GET route name is fixed (/regval)
- There is no authentication, security, etc.
You can use the value through a simple JavaScript call and display it on a website. Here's an example (save it as an HTML file):
<script>
window.onload = function() {
fetch('http://<DOCKER_HOST_IP>:<PORT>/regval')
.then(response => response.json())
.then(data => {
document.getElementById('apiValue').innerHTML = data.value;
})
.catch(error => console.error('Error:', error));
};
</script>
<p id="apiValue">Loading...</p>
This project is licensed under the MIT License - see the license.md