Use asp.net core 3.0 build smart car 1

  Follow .net core 3.0 released with System.Device.Gpio 1.0 has allowed us to be familiar with C # authentic raspberry pie GPIO development of the above. And there are many developers have been encapsulated in this package below Iot.Device.Bindings well many modules, for our use. Getting Started tutorial can refer https://www.cnblogs.com/zhanggaoxing/ , very well written and Kazakhstan.

  I used to eat here gray long-raspberry pie 3B, other accessories also includes ultrasonic module infrared obstacle avoidance module, TT motor, (using L298N drive), a custom circuit board (personal feeling, primarily to look good), a battery case, the display module voltage, the first step, a pile of parts which are assembled first effect is as follows:

1. Install Raspberry Pi .net core environment

  How wiring wait for the next chapter tell us first choice for Raspberry Pi running .net core placement in the environment, because the operating system is now official Raspbian than 64-bit. I did not use a third-party system, want to come in raspberry run .net core 3.0, we can only use the ARM32. how to install the system, Baidu a lot, not presented here.

Raspberry Pi to be networked, to update the system:

sudo apt-get update
sudo apt-get upgrade

 After downloading download .net core 3.0 sdk and asp.net core 3.0 runtime:

wget https://download.visualstudio.microsoft.com/download/pr/8ddb8193-f88c-4c4b-82a3-39fcced27e91/b8e0b9bf4cf77dff09ff86cc1a73960b/dotnet-sdk-3.0.100-linux-arm.tar.gz 
wget https://download.visualstudio.microsoft.com/download/pr/e9d4b012-a877-443c-8344-72ef910c86dd/b5e729b532d7b3b5488c97764bd0fb8e/aspnetcore-runtime-3.0.0-linux-arm.tar.gz

After the download is complete we create a directory, just unzip the downloaded file into

mkdir dotnet
tar zxf aspnetcore-runtime-3.0.0-linux-arm.tar.gz -C $HOME/dotnet
tar zxf dotnet-sdk-3.0.100-linux-arm.tar.gz -C $HOME/dotnet

Now this can only be performed dotnet dotnet directory command, to be able to execute commands anywhere we want to create an environment variable to point to this folder.

export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

 We use DOTNET --info look:

Bahrain is the environment, but to restart the missing environment so we need to configure the environment variables from the start:

sudo vim .profile

Here I am used to using vim If you do not you can replace nano

At the end add:

export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

Dotnet save and reboot commands can be used directly. So far, we can build a asp.net core of the web on the interview about the whole thing.

2. Create a new project and configure nginx

   Open VS2019, create a new asp.net core3.0 project:

I am here to modify the  Program.cs  file to add the specified listener:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseUrls("http://localhost:5000/");
                });

After the Raspberry Pi home / pi under the new folder web-> iot folder generate debug mode all generated files are all after we reached this directory dotnet it and see:

 Now our website can only access localhost. Now we do install NGINX reverse proxy. First, install and start ngix

sudo apt-get install nginx 
sudo /etc/init.d/nginx start

Open the configuration file:

sudo vim /etc/nginx/sites-available/default

Replace with:

server {
    listen        80 default_server;
    server_name   _;
    location / {
       proxy_buffer_size  128k;
       proxy_buffers   32 32k;
       proxy_busy_buffers_size 128k;
       proxy_pass         http://localhost:5000;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;                                                                
       # 转发websocket需要的设置
       proxy_set_header X-Real_IP $remote_addr;
       proxy_set_header X_Forward_For $proxy_add_x_forwarded_for;
     }
}

Save complete test restart nginx:

sudo nginx -t
sudo nginx -s reload

Now we dotnet our website using the IP raspberry pie in the browser to visit:

But this has a drawback. We must be dotnet xx.dll and each time you restart the program once the anomaly, also need to be performed manually. Here we use  systemd to restart the service implementation

sudo vim /etc/systemd/system/kestrel-carapp.service

Replace the following:

[Unit]
Dcription=ASP.NET Core 3.0 App - Car

[Service]
WorkingDirectory=/home/pi/web/iot
ExecStart=/home/pi/dotnet/dotnet /home/pi/web/iot/WebIoT.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-car
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Note, systemd requires us to use absolute paths.

Sign up and start the service:

sudo systemctl enable kestrel-carapp.service
sudo systemctl start kestrel-carapp.service
sudo systemctl status kestrel-carapp.service

Now restart the system can also visit the Web site, we do not have to manually type commands up.

3. Postscript

   Today so much, the next chapter in saying how wiring, and using a web project just created display data of the ultrasonic module

reference:

 https://github.com/dotnet/iot

 https://mp.weixin.qq.com/s/ksWs-_5JsmTQpACZfcKo6Q

 

  

Guess you like

Origin www.cnblogs.com/Gorillaz/p/11810121.html
Recommended