Skip to content

Commit 31c5cc9

Browse files
Merge pull request #7334 from pjgpetecodes/dev/djo/docs-pi-1
docs: Added Raspberry Pi tutorial 1
2 parents d92db96 + 63ac3e7 commit 31c5cc9

14 files changed

+414
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Connecting a Raspberry Pi Uno App to Azure IoT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Creating a Raspberry Pi GPIO control interface using Uno Platform
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Getting Started with Uno Platform and the Raspberry Pi
2+
3+
![Uno Hello World](images/00_uno-hello-world.jpg)
4+
5+
In this series of guides, we'll build a cloud connected Raspberry Pi GUI app to perform some straightforward GPIO (General Purpose Input Output) operations.
6+
7+
1. Introduction and Getting Started (this article)
8+
2. [GPIO Control](raspberry-pi-gpio.md)
9+
3. [Connecting to an Azure IoT Hub](raspberry-pi-azure.md)
10+
11+
## Prerequisites
12+
13+
For this series of guides, you'll need various pieces of hardware and an Azure Account;
14+
15+
- Raspberry Pi 3b+ and above (I'll be using a [4Gb Pi 4](https://shop.pimoroni.com/products/raspberry-pi-4?variant=29157087445075))
16+
- [Raspberry Pi Power Supply](https://shop.pimoroni.com/products/universal-usb-c-power-supply-5-1v-3a)
17+
- [16GB SD Card](https://amzn.to/2YAI07e)
18+
- Optional: Keyboard and mouse
19+
- SSH Client (Both Windows and Mac have a built in ssh client)
20+
- Code Editor - [Visual Studio Code](https://code.visualstudio.com)
21+
- [Jumper Cables, Breadboard, 220 Ohm Resistors, Assorted LEDs](https://amzn.to/3uYybMu)
22+
- [Microsoft Azure Account](https://portal.azure.com)
23+
- Optional: [LCD Touchscreen](https://amzn.to/3uYSXvt)
24+
- [VNC Viewer](https://www.realvnc.com/en/connect/download/viewer/)
25+
26+
## What we'll be doing
27+
28+
In this guide, we'll be setting up our Raspberry Pi to launch a "Hello World" Uno Application.
29+
30+
For this guide, we'll install all of the requirements to build and run the application directly on the Pi, but I will show you how to do this on your main machine at the end.
31+
32+
There will be a series of steps involved in this;
33+
34+
1. [Connect to your Raspberry Pi](#connect-to-your-raspberry-pi)
35+
2. [Update Raspberry Pi OS](#update-raspberry-pi-os)
36+
3. [Install .NET Framework](#install-dot-net-framework)
37+
4. [Install Uno Platform Templates](#install-uno-platform-templates)
38+
5. [Create a new Uno Solution](#create-a-new-uno-solution)
39+
6. [Give the SSH Session access to use the display](#give-the-ssh-session-access-to-use-the-display)
40+
7. [Build and run the application](#build-and-run-the-application)
41+
8. [Creating and Building on a PC](#creating-and-building-on-a-pc)
42+
9. [Wrap Up](#wrap-up)
43+
44+
## Connect to your Raspberry Pi
45+
46+
Before we go anywhere, let's make sure we can dial in to our Raspberry Pi.
47+
48+
You may need to do this part with a Keyboard, a Mouse and Monitor of course, unless you enabled SSH the SD card before you installed Raspberry Pi OS.
49+
50+
Firstly, make sure that your Pi is connected to the same network.
51+
52+
We need to enable both SSH and VNC connections, so click the Raspberry Pi OS start button, then go to the Preferences sub menu and hit the "Raspberry Pi Configuration Item";
53+
54+
![Raspberry Pi Configuration Menu Item](images/01_raspberry_pi_configuration_menu_item.png)
55+
56+
Next, click the Interfaces Tab, and make sure that SSH and VNC are enabled;
57+
58+
![Raspberry Pi Configuration](images/02_enabled_ssh_and_vnc.png)
59+
60+
Hit the Ok button, and accept the offer to reboot your Pi.
61+
62+
## Update Raspberry Pi OS
63+
64+
Before we can do anything, assuming you've gotten your Raspberry Pi all set up, we need to make sure that everything is up to date.
65+
66+
Run the following two commands;
67+
68+
```
69+
sudo apt update
70+
sudo apt full-upgrade
71+
```
72+
73+
Once those two commands have completed, restart your Pi using;
74+
75+
`sudo reboot`
76+
77+
## Install Dot NET Framework
78+
79+
Now that our Pi is all up to date, we're ready to install the .NET Framework.
80+
81+
Normally for the Pi, as there's no `apt-get install dotnet`, we'd need to go through a bunch of steps to get .NET Framework installed.
82+
83+
However, I've created a single line install script for .NET 5 on the Raspberry Pi.
84+
85+
Run the following command;
86+
87+
`wget -O - https://raw.githubusercontent.com/pjgpetecodes/dotnet5pi/master/install.sh | sudo bash`
88+
89+
You can see the contents of this script [here](scripts/install.sh)
90+
91+
![Install Dot NET 5](images/03_install-dot-net-5.gif)
92+
93+
Once the process has completed, go ahead and reboot your Pi again with;
94+
95+
`sudo reboot`
96+
97+
Once you're finished, you should be able to run the following to check your .NET framework version;
98+
99+
`dotnet --info`
100+
101+
![Dot NET 5 Installed](images/04_dot-net-info.png)
102+
103+
## Install Uno Platform Templates
104+
105+
Next we can add the Uno Platform Project Templates to our .NET Installation;
106+
107+
`dotnet new --install Uno.ProjectTemplates.Dotnet::3.11.0-dev.348`
108+
Or once Uno Platform 3.11 or later is out, you can use :
109+
`dotnet new --install Uno.ProjectTemplates.Dotnet`
110+
111+
![Install Uno Templates](images/05_install-uno-templates.png)
112+
113+
Once the templates are installed, you can scroll back up and see the list of Uno templates that were installed;
114+
115+
![Uno Templates Installed](images/06_uno-templates-installed.png)
116+
117+
## Create a new Uno Solution
118+
119+
Now we have the moving parts installed on our Pi, we can spin up a brand new Uno solution with the following command;
120+
121+
`dotnet new UnoApp -o unoapp1 -ios=false -android=false -macos=false -wasm=false -skia-wpf=false -st=false && cd unoapp1`
122+
123+
You should now find yourself in the solution directory for your new Uno App. If we have a look at the folder contents with;
124+
125+
`dir`
126+
127+
![Uno Templates Installed](images/07_uno-app-directory.png)
128+
129+
In the solution we've just created we have several files, including a global.json file which enforces that we use at least .NET Core 3.1.100.
130+
131+
We also have the main solution files that we can open in Visual Studio on Windows or Mac.
132+
133+
Then we have four directories;
134+
135+
| Directory | Purpose |
136+
| :--- | :--- |
137+
| unoapp1.Shared | Contains the main XAML page |
138+
| unoapp1.Skia.Gtk | Contains the Linux / Raspberry Pi Version |
139+
| unoapp1.Skia.Linux.FrameBuffer | Provides Access to a Window Manager and the Cursor |
140+
| unoapp1.UWP | Contains the Windows UWP version |
141+
142+
The directory we're interested in is the `unoapp1.Skia.Gtk` directory. This directory contains the project which we'll build and run on the Raspberry Pi.
143+
144+
It actually includes the build outputs from the `unoapp1.Shared` and `unoaddp1.Skia.Linux.FrameBuffer` projects too, and in the next tutorial, we'll dive in to those in a bit more detail.
145+
146+
## Give the SSH Session access to use the display
147+
148+
Before we can launch our application, we need to give our SSH session access to the display.
149+
150+
If we don't do this, we'll get an error like;
151+
152+
```
153+
Unable to init server: Could not connect: Connection refused
154+
155+
(unoapp1.Skia.Gtk:18530): Gtk-WARNING **: 19:40:51.384: cannot open display:
156+
```
157+
158+
We can sort this out using the following command;
159+
160+
`export DISPLAY=:0`
161+
162+
You won't get any response to this message, so don't worry.
163+
164+
You'll need to remember do this every time you launch a new SSH session currently, but don't worry too much about that too much for now.
165+
166+
## Build and run the application
167+
168+
We're now ready to run our application.
169+
170+
Firstly, we need to navigate to the Skia.Gtk directory;
171+
172+
`cd unoapp1.Skia.Gtk`
173+
174+
We can now run our application with;
175+
176+
`dotnet run`
177+
178+
This will take quite some time to run this command the first time as the Pi isn't as powerful as a desktop PC of course.
179+
180+
You may also see some errors, but don't worry about those, as they're complaining that we're not running this from Visual Studio for XAML Hot reload to connect to.
181+
182+
To be able to see your app running, you're going to need to either connect a Monitor or VNC, but all being well, after a minute or so, you should see the Uno Hello World application running happily on your Pi;
183+
184+
![Uno App Running](images/09_uno-app-running.png)
185+
186+
## Creating and Building on a PC
187+
188+
We've performed most of this on the Pi itself of course. However, you can actually create and build the whole application on your PC and copy the built files to your Pi using;
189+
190+
`dotnet publish -r linux-arm -o bin\linux-arm\publish --no-self-contained`
191+
192+
You can copy the contents of the `bin\linux-arm\publish` directory to your Pi in whatever way takes your fancy.
193+
194+
You then need to navigate to the directory where you've copied the files and make the main application executable with;
195+
196+
`chmod +x unoapp1.Skia.GTK`
197+
198+
Don't forget that, if you've just dialled in you'll need to give access to the Display;
199+
200+
`export DISPLAY=:0`
201+
202+
You can then run the application with;
203+
204+
`./unoapp1.Skia.Gtk`
205+
206+
I've created a blog post around how you can actually automate Building, Deploying and even debugging from your Windows machine using VS Code;
207+
208+
[Developing UWP Apps for the Raspberry Pi with Uno Platform](https://www.petecodes.co.uk/developing-uwp-apps-for-the-raspberry-pi-with-uno-platform/)
209+
210+
## Wrap Up
211+
212+
With that, we've gotten our first Hello World application up and running on the Raspberry Pi.
213+
214+
In the next guide, we'll look at how we can create a UI and some code to control the GPIO on the Raspberry Pi to read the status of Buttons and turn on and off LEDs.
215+
216+
Thanks.
217+
218+
[Pete Gallagher](https://www.twitter.com/pete_codes)

0 commit comments

Comments
 (0)