How to Upload Code to Raspberry Pi How to Upload Code to a Microcontroller
Executing Python
Then you've decided to take the plunge and learn more nearly computers and programming with a Raspberry Pi, you lot're prepare to kickoff writing your own amazing projects or trying other peoples code simply so y'all realize that you're not too sure how to actually run a program. Well, like most things to do with the Raspberry Pi it is quite piece of cake to do and in this tutorial, I will show you how to do it in a couple of ways. Firstly using the Pi desktop connected to a keyboard and monitor and then using programs like Putty and Filezilla to run the code remotely.
The Raspberry Pi can execute code written in a wide range of languages, given that the "Pi" in Raspberry Pi is a nod to the Python linguistic communication itself and that it is relatively easy to learn for the beginner, it'southward a cracking place to start. In the descriptions below I am bold that you have set-up your Pi and that SSH is enabled, also that you are using the latest version of Raspbian.
Running Lawmaking with Thonny IDE
In order to execute a program the best place to start is with a slice of software called an IDE (Integrated Development Environment), IDE's allow yous to create, edit and execute your code, fortunately the Raspbian operating organization on the Pi it comes with one called Thonny. IDE's are likewise helpful to programmers as they can assist you in debugging and finding errors in your code.
The Python language has inverse over the years and equally such the commands and syntax have been updated and developed, the examples used in this tutorial volition be for Python 3.
To open up Thonny you lot demand to go to the main Raspberry Menu, select Programming then Thonny Python IDE every bit shown beneath.
With Thonny now open up we can begin to create some code, we'll outset with the sometime classic "Hello World", enter the following code into the "Shell" editor and printing enter, as this IDE is designed for Python y'all will detect that the color of what you are writing changes as it recognizes the syntax.
Congratulations you have now run your first Python program. As this is a Python IDE yous can also create programs with loop statements and Thonny will automatically provide the correct indentation to run the code. Every bit an example type in the following lawmaking and printing enter twice.
.
Notice that the syntax has been color-coded, the print statement has been indented and the "for" loop has been executed the required number of times.
Ok, so this is all fine as an example of how Thonny works but in the real world running programs this way isn't very helpful, what you want is a way to write longer programs and be able to run them over and over again. To do this all we need is a simple text editor. Thonny has a built in text editor and it can be seen just in a higher place the Shell tab (currently <untitlied>). In the text tab enter the following lawmaking.
Now with our code written we demand to save it, select "Save Equally…" from the file bill of fare and give you lawmaking a name, notation that all python scripts need to end with the suffix .py.Yous may too discover the first line in the code above, this is known as a "shebang" and serves to let the system know what Python interpreter to use when it runs the program. To execute the programme only select the "Run" option from the carte du jour and then "Run Current Script" or press the green "Play" push. The results of your plan will and then be shown in the Shell tab.
Finally, next to the light-green Play button in Thonny is the Debug button, pressing this button puts the IDE into debug mode, this tin can be very helpful when trying to identify errors in your programs. In Debug mode you are able to footstep through the program ane line at a time and step into and out of whatsoever loops, you can also track any variables to make sure that all parts of your programme are operating as expected.
Execute Lawmaking from the Control Line
Another style to execute your lawmaking once it has been saved is straight from the Command Line Interface (CLI) or last screen. To do this you starting time need to open the terminal on your desktop, select the Applications carte (Raspberry icon), so the Accessories menu and finally select Final
This will present you with a screen that contains the command prompt, to run our saved code you beginning demand to navigate the file system to the binder that you saved your script in, this can be washed using code similar to this just evidently using your own folder names.
Once you lot have navigated to the correct folder you can enter one of the post-obit commands depending on whether yous desire to run the code using Python 2 or 3 as shown in the image below.
As I said earlier the code here is written for Python 3 and as such yous can see in that location are some slight differences in the way theprint command is interpreted using the different versions.
Running the Code Remotely
If yous plan on using your Pi in a Headless mode (ie. no monitor or keyboard attached) then y'all volition need to have some boosted software on your estimator to enable yous to transfer and execute your code. You also demand to have enabled SSH on your Pi so that a final emulator and FTP client can talk to it, this is best done by having a monitor and keyboard fastened initially and running "Raspberry Pi Configuration" from the applications menu.
The iii pieces of software that you lot will need are
- A text editor (Notepad++ or Visual Studio Code)
- Terminal Emulator (PuTTY)
- FTP Client (Filezilla)
All of these programs are free to download.
We'll begin by recreating and saving the code from to a higher place on our local computer in a text editor similar notepad, don't forget to save your file with the suffix .py
Adjacent, nosotros need to transfer this file to the Pi, to do this we'll employ the FTP client Filezilla. If you lot know the IP address of your Pi then you enter this equally the Host, if you lot don't know your IP accost then this can be found past logging into your router (type 192.168.1.1 or 192.168.0.one into a browser bar) and selecting the DHCP entries where you should come across the hostname "raspberrypi" and the associated IP accost.
Alternatively if yous know the hostname of your Pi (default is "raspberrypi") then yous can simply type "raspberrypi.local" equally the Host and it will detect the IP address for you, next enter the username (default = "pi"), password (default = "raspberry"), set the port to 22 (sftp) and click "Quickconnect".
Filezilla will at present brandish your local files on the left-hand side and the files of your Pi on the right, navigate your local file organisation on the left to find the file that we just saved, navigate the files of your Pi to where yous want to relieve your code too then simply elevate and drop the file across. You tin cheque whether the transfer was completed by checking the " Successful transfers" tab at the bottom of the screen.
Now that you lot take successfully got your code onto the Pi we need to access the control line of the Pi, to practise this you need to run the Putty terminal emulator, again enter your IP address or hostname into the Hostname box, select port 22 and SSH from the radio buttons and click open.
Y'all at present have access to the terminal on your Pi and you can execute the code in the same way that you did when nosotros were doing it from the desktop as shown beneath.
And so there you have it, using these techniques y'all should have no problem executing Python code on your Raspberry Pi.
If you lot are new to the Pi and want to find out more almost what you tin can do with it then why non check out my review of Adventures in Raspberry Pi past Carrie Anne Philbin of the Raspberry Pi foundation.
If learning Python and applying it to practical situations is more your thing then why not check out Automate the Boring Stuff by Al Sweigart to go the nuts in a way that may exist useful in the real globe.
If you have any thought'southward about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.
crockerbeader1974.blogspot.com
Source: https://myhydropi.com/how-to-run-python-on-a-raspberry-pi
0 Response to "How to Upload Code to Raspberry Pi How to Upload Code to a Microcontroller"
ارسال یک نظر