At this project targeted to Raspberry Pi with Linux, the API First Design strategy is used to develop an ASP.NET Core Web Server automatically generated by Swagger Hub. The availability of Visual Studio running at a speedy x64 cpus with Windows 10 are the key advantages of this starter kit. Previous studies were done before this project, please see links below:
For more details about IoT Starter, please see articles and repos below:
IoT API for Raspberry Pi
Starting from a Swagger API definition, an ASP.NET Core Web API is developed using SwaggerHub. The code is automatically generated and pushed to github. Then it is loaded by a Windows x64 machine and upgraded to ASP.NET Core 2.0, using Visual Studio 2017.
A multi-stage docker image build is accomplished at the speedy Windows x64 machine, generating code for the linux-arm. The same VS solution include separated projects for the web server and web UI. Both docker images are pushed to the cloud and then pulled back into a Raspberry Pi with Raspbian 9.1 stretch installed.
Following is the expected workflow for this project, that will cycle activities for modeling, programming and development, deployment and testing. The main objectives are summarized below.
- API is designed at SwaggerHub, check the file here
- Web service stub is automatically generated by SwaggerHub
- SwaggerHub connects directly with GitHub repository
- SwaggerHub is configured to push code to 'swag' branch at Github
Development at x64 with Windows 10
- Pull Swaggerhub generated code from github
- Upgrade to .NET Core 2 framework and Docker support
- Merge code from swag branch to master
- Create multi-stage docker build for web service
- Compiles at x64 machine for linux-arm framework
- Push Docker image to cloud: home-web
- Generate another project based on default razor pages app
- Add multi-stage docker build for web ui
- Push Docker image to cloud: home-ui
Deployment at Raspberry Pi with Linux
- Raspbian GNU/Linux 9.1 (stretch)
- Install Docker
- Run both Docker images for home-web and home-ui
- Go back to x64 machine
- Access home-web and home-ui via any browser
Running Both Images at Raspberry Pi
A Raspberry Pi with Raspbian 9.1 stretch is connected in the same LAN of x64 machine. Docker is installed and commands shown below activate the home-ui and home-web images. They start to listen on ports 80 and 5010 respectively.
The commands start clearing all docker containers and images:
root@lumi:~# docker stop $(docker ps -a -q)
root@lumi:~# docker rm $(docker ps -a -q)
root@lumi:~# docker rmi --force $(docker images -q)
Then all projects are started, downloading images from DockerHub to Raspberry Pi. You can notice the home-ui project share four images with home-web project and they don't need to be downloaded again.
alias yhomeui='docker run --privileged -p 80:80 -d josemottalopes/home-ui:latest'
alias yhomeweb='docker run --privileged -p 5010:5010 -d josemottalopes/home-web:latest'
Unable to find image 'josemottalopes/home-web:latest' locally
latest: Pulling from josemottalopes/home-web
0d9fbbfaa2cd: Pull complete
b015fdc7d33a: Pull complete
60aaa226f085: Pull complete
01963091a185: Pull complete
6f687f698add: Pull complete
7523740b666e: Pull complete
Status: Downloaded newer image for josemottalopes/home-web:latest
Unable to find image 'josemottalopes/home-ui:latest' locally
latest: Pulling from josemottalopes/home-ui
0d9fbbfaa2cd: Already exists
b015fdc7d33a: Already exists
60aaa226f085: Already exists
01963091a185: Already exists
9ba45e81e264: Pull complete
2e769f6ed072: Pull complete
Status: Downloaded newer image for josemottalopes/home-ui:latest
The docker images and running containers are shown below:
root@lumi:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f43b25bff7ba josemottalopes/home-ui:latest "dotnet Home.UI.dll" 12 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp sleepy_mayer
73780b8675a5 josemottalopes/home-web:latest "dotnet IO.Swagger..." About a minute ago Up About a minute 80/tcp, 0.0.0.0:5010->5010/tcp goofy_bassi
root@lumi:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
josemottalopes/home-web latest 88f48c67c247 21 hours ago 235MB
josemottalopes/home-ui latest 7eb1d110bcc4 21 hours ago 233MB
Checking at the browser, we see both home-web and home-ui projects running at Raspberry Pi with Linux.
Upgrading the API
Changes to API are done editing the API master file at Swaggerhub. In order to smoothly update the project, the following configuration should be done, selecting the proper repo and a new branch "
swag" to store the changes.
Please note that it is necessary to allow full control over some folders, including models, controllers and wwwroot, as shown below.
The generated files may include some garbage that should be cleaned properly, including some .json and .sln files from previous Visual Studio versions.
From time to time, some breaking changes may happen at Swaggerhub generated code. In these cases, conversion to ASP.NET Core 2.0 maybe done automatically by Visual Studio, I used Community 2017 Preview2. Change the Output Folder at Swaggerhub Integration to "home2", and allow VS to adjust automatically the solution files, libraries, etc. Then bring the changes to "home" in another safe step.
Use the Visual Studio to setup Docker, apply menu "Add Docker support" for the solution. Then docker-compose and dockerfile maybe adjusted, properly pulling the strings for a smooth operation at Windows x64 machine.
This starter kit may improve or establish connections to the huge amount of work already done at Linux platform. Projects like LIRC, the Linux Infrared Remote Control for Raspberry Pi, will be explored soon.
For now, have fun with this project!