Viewing entries tagged
setup

C# Discord Bot on Raspberry Pi: Setting Things Up

C# Discord Bot on Raspberry Pi: Setting Things Up

CSharPi: Getting Started

RPi-Logo-Reg-SCREEN.png

What if I said it was possible to run a Discord bot on a Raspberry Pi natively? With .NET Core we can do just that.

Getting Started

Updated for .NET Core version 3!

Let’s get some prerequisites out of the way. The first thing we’ll do is install the .NET Core SDK on our local machine, as well as our Raspberry Pi. Next, we’ll ensure Git is installed both places as well.

Finally, I will be using Visual Studio Code for this project, so I will go over installing that as well.

If you are familiar with all of these concepts, and have created Discord bots before, feel free to skip to the next part of the series:

Otherwise, carry on!

Installing .NET Core

Local Machine

Let’s start out by installing the .NET Core SDK on our local machine. You’ll want to start here, and follow specific instructions for your OS: https://dotnet.microsoft.com/download.

Once installed, you can verify it is working by dropping to the command line (OSX you’ll use Terminal, Windows you want PowerShell, and Linux whatever your favorite shell is), and running: dotnet --help

You should see something like this:

dotnet_help.png

Raspberry Pi

The one prerequisite here is that you can SSH into your Raspberry Pi. If you need some help enabling SSH, check out this article here: https://www.raspberrypi.org/documentation/remote-access/ssh/.

If you’d like to setup your Raspberry Pi headless, and ensure it connects to your WiFi network and automatically enables SSH on first boot, check out my post:

Once you’re sshed in (Windows users that need help check this out: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/ssh-under-windows), you can start installing .NET Core on your Pi.

The following commands will:

1. Navigate to the home directory

2. Download the .NET Core SDK

3. Create the directory /usr/share/dotnet

4. Uncompress the archive we downloaded into /usr/share/dotnet

5. Create a symbolic link so we can use the dotnet command

cd ~
wget https://download.visualstudio.microsoft.com/download/pr/8ddb8193-f88c-4c4b-82a3-39fcced27e91/b8e0b9bf4cf77dff09ff86cc1a73960b/dotnet-sdk-3.0.100-linux-arm.tar.gz
sudo mkdir /usr/share/dotnet
sudo tar -zxf dotnet-sdk-3.0.100-linux-arm.tar.gz -C /usr/share/dotnet
sudo ln -s /usr/share/dotnet/dotnet /usr/local/bin/dotnet

If all goes well, we should be able to run: dotnet --version.

dotnet_pi.png

Installing Git

Local Machine

Check out this URL for help installing Git on your local machine: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git.

We can verify it works after installing by dropping to your favorite shell and running: git.

git.png

Raspberry Pi

If you are using the minimal (lite) version of Raspbian, or need Git installed otherwise, run: sudo apt install git, and accept any prompts with “y”.

gitin.png

Now we can run: git to verify it installed correctly.

gitpi.png

Visual Studio Code

Visual Studio Code has become my editor of choice, even for some C# projects. We will install the client onto our local machine so we can edit the code there. For help installing Visual Studio Code, go here: https://code.visualstudio.com.

Once you have Visual Studio Code installed, open it up and click the extensions icon on the left (looks like a square). Then search for and install the C# extension. In this screenshot, it is the only one in the list for me that does not have an install option, and has a gear next to it (since I already have it installed). Once you install it, it will give you the option to reload Visual Studio Code.

C# ext.png

Creating the .NET Core Project

Now we will be creating the .NET Core project. The machine we will be doing this on is our local machine, not the Pi.

Hello World

Let’s start by creating a dotnet console application.

1. Fire up your favorite shell, and run: dotnet new console -n csharpi.

This will create a new directory named csharpi, and create the project there.

dotnetnewc.png

2. Now open up the csharpi folder with Visual Studio Code. You will see a prompt in the lower right-hand corner asking if you want to add required assets, click “Yes”.

assets.png

If all went well, you will now see a .vscode folder in the list.

vscodefolder.png

3. Now to test things out. Click Program.cs to see the example code the default project provides that will write out “Hello World” to the console. Go to the Debug menu, and click “Start Debugging” (or simply hit F5).

debut menu.png
helloworked.png

If you see “Hello World”, our project has been created, and we have the ability to edit it in Visual Studio Code.

Git Repository Initialization

Now we will initialize the local instance of the Git repository, and push up our code to Github. If you don’t have an account on Github, you can create one, here: https://github.com/join?source=header-home. Once you create it, log in and hang on because we will be using it shortly.

Local Initialization

1. Open up your favorite terminal / console and navigate to the csharpi folder we created. Once there, run: git init. Keep the window open.

gitinit.png

2. Open up the csharpi folder in Visual Studio Code. You’ll notice the Git icon (branching line) has an indicator that there are 22 files to work with. Let’s fix that by creating a file named .gitignore, and tell Git what files we don’t want to commit.

3. Click the File icon in VS Code to see the files in the project, and then click the create new file icon. Name the file .gitignore

newfile.png
filename.png

4. Open the .gitignore file by clicking it, and add the following contents:

        /bin
        /obj
        *.json

This will tell Git to ignore the /bin and /obj folders, as well as any file ending in .json. Since we will be creating a json configuration file with a token we do not want to share in it later, this is a good thing.

contents.png

5. Switch back to the terminal / console window you have open and run:

        git add .        
        git commit -m 'first for csharpi'
addstage.png

6. Now go to Github and login.

7. Once logged in, click the [+] icon in the upper right, and select “New repository”.

newrepo.png

8. On the next page, give it a name and description. Keep it public to follow along with this tutorial. Cloning it will be much easier on the Pi that way.

create.png

9. Click “Create repository, and check out the instructions on the next screen. You’ll see that we completed most of it already.

10. Run the two commands for existing repository.

For me it is (your commands will look similar, with the url after origin reflecting your username and repo name):

            git remote add origin https://github.com/gngrninja/csharpi.git                     
            git push -u origin master

11. You should now see the following on the command line, and on Github once you refresh the page:

pushit.png
reposuccess.png

Cloning the Repository to the Pi

Now let’s test that .NET Core is working correctly on the Pi.

1. Go to your Github repository for this project (for me it is https://github.com/gngrninja/csharpi), and click “Clone or download”, and then copy the URL.

copyurl.png

2. SSH into your Raspberry Pi and run git clone, and then paste in the URL we copied from the above step.:

        git clone https://github.com/gngrninja/csharpi.git

3. We can now change directory to csharpi, check out the contents, and try running it (essentially in debug mode):

        cd csharpi
        ls
        dotnet run
cdrun.png

4. Debugging on the Pi with .NET Core is not fun. It takes forever, and is on their list to fix (as much as they can, considering it is not the fastest processor). Eventually, however, you should see the following:

helloworldpi.png

5. Fret not, running the compiled/published application is much faster. To see this happen let’s create a folder under our home folder that will contain the published version of our code.

        mkdir /home/pi/bot

6. The /home/pi/csharpi folder will contain the code we clone from the repo, that we work on from our main machine via VS Code. The /home/pi/bot folder will contain the published version of that code, that executes much faster. Let’s ensure we are in the source code folder, and run the command to publish the app:

        cd /home/pi/csharpi
        dotnet publish -o /home/pi/bot
        
publish.png

7. Now that it is published, we can run the application. It will execute much faster as the published version.

        cd /home/pi/bot
        dotnet csharpi.dll

(I know right, running a dll on *nix?!)

runpub.png

Success! We now have the groundwork laid for creating the Discord bot.

Now let’s get a Discord bot going:

Raspberry Pi Headless Setup With WiFi and SSH Enabled

Raspberry Pi Headless Setup With WiFi and SSH Enabled

Headless Raspberry Pi Setup With Raspbian

RPi-Logo-Reg-SCREEN.png

The Raspberry Pi is an awesome little computer that can do just about anything. It’s around $35 USD for the 3B+ model, which is the one I prefer to use most of the time. This post will go over how to install Raspbian, configure WiFi, and enable SSH (all without using a monitor, mouse, or keyboard attached to the Pi).

Use the options below to navigate around.

IMPORTANT -> The Raspberry Pi 4 does not support USB booting at this time, you will have to use an SD Card for your boot media. More details below.

Install Raspbian

1. Head on over to https://www.raspberrypi.org/downloads/raspbian/, and download the latest image. For this example I will be downloading the ZIP compressed image for Raspbian Buster Lite.

buster.png
Screen Shot 2019-06-25 at 3.35.48 PM.png

2. Next, we will need an app that allows us to write the image to an SD card or SSD (whatever we will be booting from on the Raspberry Pi). I like balenaEtcher. You can download etcher from here: https://www.balena.io/etcher/.

NOTE: If you have a shiny new Raspberry Pi 4, like I do now, you cannot boot from USB at this time, so you’ll have to use an SD Card. More information, here: https://www.raspberrypi.org/documentation/hardware/raspberrypi/booteeprom.md

dl etcher.png

3. Open Etcher, or whichever app you picked to write the image to the card/drive.

4. Connect the drive/SD card to your computer. I will be using an SSD with a USB converter.

hdd or sd.JPG

(note) Sometimes the voltage on the Pi’s USB ports can drop and affect the SSD attached. I have not had any problems using an SSD with the above linked convereter, but if you encounter issues you may want to look into a powered USB hub. The Pi 3B+ will automagically boot via USB, but if you have the Raspberry Pi v3, check out this link: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md

5. In Etcher, select “Select image“.

select image.png

6. Now choose “Select drive”, and pick the drive / SD card you recently attached, and click “Continue”.

select drive.png
open drive.png

7. Finally, click “Flash!

flash.png
wait.png

Setup WiFi Network Join on Boot

Now we will want to have the Pi join your WiFi network when it boots up. To do this we will create a file that will tell it what network to join, and its password.

1. If you don’t see the boot volume on your machine, safely disconnect and reconnect the SSD or SD card to your computer.

2. You should now see the boot volume attached to your machine.

boot.jpg

3. Using your favorite text editor, create a file named wpa_supplicant.conf.

Here is the content you’ll want for the file (be sure to replace the content in quotes with your SSID and password):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
fast_reauth=1
country=JP

network={
	ssid="Your network's SSID"
	psk="Your network's password/psk"
	id_str="0"
	priority=100
}

4. Save the file/copy it to the boot volume.

copy file.png

Enable SSH on Boot

Next we will be enabling SSH on boot. To do this we simply need to create an empy file named ssh (no extension), and copy it to the boot volume.

1. Create the empty file (you can use Notepad, or any other text editor to do it) named ssh (no extension). For this example, I will use the Terminal app on OSX to create and copy the file.

cd ~/Desktop/
touch ssh
cp ssh /Volumes/boot
empty file create copy.png

2. Now you’ll want to safely eject the boot volume, and attach it to your Raspberry Pi!

safe.png

First Boot and Setup

Now we will want to plug the the storage device into the Pi (SD card or SSD -> USB converter), and power it on. Once it boots up we will find the IP address it was assigned from the router, and ssh in to change the default ssh password.

1. Once the storage device is attached to your Raspberry Pi, plug it in to power it on!

2. Log into your router, and look for something called attached devices/devices or DHCP client list. Keep refreshing the list, and eventually you should see a device with the hostname raspberrypi. For more help locating a headless Pi, check out this link: https://www.raspberrypi.org/documentation/remote-access/ip-address.md

found it.png

3. Now that you have the IP address, ssh in! If you’re on Windows, and need some help, check out this article: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/ssh-under-windows. For this example I will be using the Terminal app on OSX.
The username is pi, and the default password is raspberry.

ssh pi@192.168.1.29

If you are prompted to trust the host, type / select “yes”.
When prompted for the password, enter raspberry.

ssh in.png

4. The very next thing you’ll want to do is change the default password. To do this, use the command (ensuring you are indeed sshed into the Pi): passwd.

(note) You will first be prompted for the current password, so enter raspberry for that. Then select your new password, and confirm it. You will not be able to see anything as you type them out.

change pw.png

[back to top]

Congratulations!

(well, hopefully)

If all went well, you should now have a Raspberry Pi that boots up and joins your WiFi network. You can SSH in to configure things, and do whatever you need to with the device. The files we created earlier (wpa_supplicant.conf and ssh) are removed from the boot volume after the first boot, so you have nothing to worry about there.

Let me know if you have any questions or feedback in the comments, below!

If you’re looking for something to do with your Pi, why not create a Discord bot and run it off it?