How to Transfer Data/Files from Raspberry Pi Pico to Local Computer (Programmatically) - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up YouTube in today's video I'll be showing you how to write data straight from a Raspberry Pi Pico or Pico W to your local computer and this has really powerful use cases for when you want to write large swaths of data to a device because that is we are really Limited in the amount of storage we have on our Raspberry Pi Pico or Pico W and we can only write so much data sensor data that is for applications and eventually we will run out of storage to save that important CSV information or any information you have on the device so by the end of this video you will be able to transfer data from your Raspberry Pi Pico or pow to your local computer in real time as you could see with the simple example I have here this is data I wrote from my Raspberry Pi Pico W straight to my computer without even having to save onto my Raspberry Pi Pico W which is pretty cool so by the end of it you will have a foundation to do this and have an example to get started self so enough being said let's jump into it to what we need for the setup and I will show you how to do this okay so the first step today is I'm using a Mac and I'm in the terminal app and at a high level what we're trying to do to transfer files between the Raspberry Pi Pico Pico W in my case to the local computer my Mac is we want to establish a Serial communication between the two to start doing this to be able to write information from the Pico W to my computer so in order to do that we're actually going to run a program on our Mac that allows us to do that in Python and the first thing you want to do is simply pip install a package called Pi cereal so you just do pip install minus pip3 and we could just do PI serial that's the first step you want to do so you do have to have python on your local computer to do this that is a prerequisite but I would imagine if you are have been programming this is already on your computer but installing python is beyond the scope of this video just know once you have python you have to pip install the pi serial package so I'm just going to click enter there and I already installed it and that's for the first step okay so the next thing you want to do after pip installing the pi serial package is you simply want to create a python script somewhere on your computer I just created it on my desktop here and I called it connect to pico.pi this is the python script we'll be running on our local end to be waiting for the serial data from our Raspberry Pi Pico or Pico W so I have that script and I just have it open in vs code you can use any editor you want really and it's actually a really simple script so we just import the serial package which we just pip installed we configure the serial connection with just three lines here we set the port so this port is important because it is the the the path essentially to your Raspberry Pi Pico and how I got that Port is I went to my thany IDE and you can actually get it really easily in thony so if I just go to Thane I could show you if I only knows exactly where the device is and on the bottom right there is just so shows Dev slash CU dot USB modem1101 so yours is probably different than that so just go check that and then just copy that in there for the port and then once you have the port going back to the code about right this is the bad right we want the default bound rate for the Raspberry Pi Pico and we create the serial connection there and we have this open a file on your computer to write the receive data so this is just the file path on our computer the full path where we want to write the data from our Raspberry Pi Pico so we can just really save that wherever it's on my desktop so if we go back here and I minimize this we can actually see that the file is also there on my desktop so you can just call it whatever you want for your purposes and then going back to this code once again what we do is forever in a while loop while true we have this data so data equals serial connection.read so it's waiting to get at least 128 bytes from the Raspberry Pi Pico W and once it has 128 bytes what it's going to do is it's going to check if the data is equal to eof and if it is break we're not really using the condition for the sake of this video but you can add a break condition to eventually break out of the while loop if you like and then we just print the data locally to show that it's working so this is just for our debugging purposes and then we write the data to the file that we set here and then if we broke out of the loop we simply close the destination file and we close the serial connection so that's literally all you have to do for the local end so it's a really simple script I'll link that in the description down below and you can really customize this as you want and get creative but this is a a really basic way to initiate serial communication with the Raspberry Pi Pico W on your computer now that you have this locally we are not going to run it yet I'm going to show you what we're going to do on the Raspberry Pi Pico W end but so let's jump into that and we'll get back to this in a second okay so jumping into the Raspberry Pi side of things I'm using Thani to edit my code really you can use any editor you want vs code pycharm but I think Donnie is the best for this case scenario so what we're going to do is we're just going to hop in thony and we're going to select our device from the bottom right there which I already did and we're just going to go to the main.pi file if you don't have one create a main.pi file and the reason we're doing the main.pi file specifically some people don't know this is because as soon as the Raspberry Pi Pico is plugged into any power source it actually tries to execute the main.pi file and the reason we want to do execution upon plugging in the device is because we can't go back and actually run this code at the same time our local computer is trying to connect to the serial Port because Sonic itself is trying to connect to the same serial Port our local computers trying to connect to and we'll get an error so we want to do and then of this is we want to create something in the main.pi file and we're going to unplug and replug the device once it's ready and that way we'll only have one serial communication at a time between our local computer and the Raspberry Pi Pico W okay so that's what we're doing at a high level it has to be in the main.pi file for this example for this to work so what we have in this main.pi file is we have some simple imports from machine to interact with some of the hardware I squared C pin to do some I squared C communication obviously and time to manage some time we're just doing time.sleep to make the the loop less or more in frequency and then we have import uoos which is for us to allow interaction with the operating system of the Raspberry Pi Pico and then what we're going to do is import bme280 this is just a library I have for my bme280 I'd imagine most of the people watching this video aren't using a BME 280 so if you're not you don't have to worry about this you can use any sensor you want any form of data you want I'm just using the bme280 because it's a popular sensor I like to use and I'll link that down in the description below if you're interested in a sensor like that but it does provide temperature humidity and pressure data so once we have those Imports what we want to do is we want to create a uart object now Ur is something I'm fairly new to and what Ur stands for is actually universal asynchronous receiver transmitter and essentially allows us to transmit and receive data in serial format okay so that's why we're using uarts now we did configure some uart settings here because I had issues sending data to my local computer and reading data from my bme280 at the same time so just know that these three lines of code or these two lines here actually allow me to read data from my bme280 and send data to my local computer at the same time without this I wasn't able to do that I was actually running into issues so if you are using a sensor you probably have to put these two lines of code here to make sure you can read the sensor data and send it to your local computer with one script next what I have here is I'm just creating the BME 280 object with my I squared I squared C object and I'm creating it with the BME Library simple as that once again if you're not using the BME 280 Library you do not have to worry about this and finally what we're doing here is we're just reading the data from the sensor so temperature pressure and humidity and we're printing it so when we actually print in this case we're actually printing it to the the file that we opened on our local computer so it knows based on the serial communication that it's going to print to that file so printing here is not printing on the screen it's actually printing to the file on our local Mac so that's pretty cool so that's all we have to do to write that to our Mac and it's going to separate it by a new line automatically and we're just gonna do that once like I said every second and finally I don't know I don't think I need this code anymore that's from before and because this is a continuous while loop we actually will never reach this line of code so we could just remove that but ideally you would want some exit condition this is just at a high level what you do and that's really all it is for the Raspberry Pi Pico side so right now we're serially connected from Thane to the Raspberry Pi Pico so this won't work right now so we have to do is now that it's saved to our main.pi file I can simply unplug and unplug my Raspberry Pi Pico W so I have it right here so I'm just going to unplug and plug it it's fine and what's going to happen is as soon as I replug it it's going to start reading sensor data and it's going to try to write it to a serial or it's going to try to print it to that serial uh to that port or to that to that channel so it's trying to do that right now but I haven't run the script on my local machine so what's probably happening right now is trying to print somewhere I can't find it and it keeps executing this Loop so it's executing that main.pi file so let's go back to our terminal here and run the file we have on our local computer so if you want to do that you can just go to your terminal and type python the name of the file so I'm just going to type enter there so now give it a few seconds because it's waiting for 128 bytes of data before it does anything so you can see now that every time it retrieves 128 bytes of data from the sensor it's printing on the screen and it's saving to the file so we'll just let it run for a bit and like I said I don't have any specified exit condition it is not the best code but I'm just showing you how to get a skeleton of this for your own purposes and hopefully after watching this video you will have the means to create some nice exit conditions and do some other fancy things with this serial communication so what's happening is right now the Raspberry Pi Pico is getting data from the BME 280 sensor and now that it finds a file to print to it's actually printing to the file we have on our local computer and that's pretty much it so you can print any sensor data you want you can transfer whole files over there's many ways you can go about this so if I go ahead and exit this code right now I'm just going to hit Ctrl C on my computer to exit it to force exit on my local computer we're going to see that all this all these bytes are actually saved in that file I defined on my desktop which is pretty cool so I'll let it write one more there and we'll exit it okay so just Ctrl C and probably a similar command on Windows let's go back and open that store info.txt we could see it wrote a bunch of lines of data so that's exactly what we expected so that pretty much concludes it for this video we wrote two scripts one our local computer and one on our Raspberry Pi picow that allows us to transfer instantly data from our device to our local computer which has many powerful use cases especially as I mentioned at the beginning of the video for sending large amounts of data and people who are using sensors probably are recording data every you know milliseconds and eventually their device will run out of storage so this is a great way to for sensor applications to save data that you want to look at later on and I would imagine it has many other powerful use cases so I know it's a lot of Serial communication can be kind of intimidating at first but I hope you learn how to do this at a high level if you have any questions let me know in the comment section down below I'll be willing to help you with any complications you have regarding this I know we went over a lot in a short amount of time so let me know about that be sure to like comment subscribe because I think this is really useful content in fact I was trying to do this for one of my applications and because it took me some time I thought I would show the internet so I hope it helps someone out there and if it did be sure to subscribe that would mean a lot to supporting a Shilla Tech content and stay tuned guys thanks for watching until next time [Music] thank you
Info
Channel: Shilleh
Views: 5,691
Rating: undefined out of 5
Keywords: serial communication raspberry pi pico, serial communication, uart, file transfer raspberry pi pico, micropython serial communication, thonny, raspberry pi pico transfer files to computer, raspberry pi pico, iot, raspberry pi tutorial, python, how to use serial communication with raspberry pi pico w, pico w, pyserial raspberry pi pico
Id: OfJ5Y1FlW94
Channel Id: undefined
Length: 11min 49sec (709 seconds)
Published: Sat Jun 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.