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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone welcome back to the channel and today's quick tutorial what I'll be showing you is I'll be showing you how to send data from your local computer to your Raspberry Pi Pico in real time this is an extension of a previous video I did a couple months ago where I showed how to do it vice versa that is from your pico to your local computer in real time programmatically in micro Python and python so if you haven't watched that video I suggest you do because there's many Concepts in that video that we went over that will be continued in this video so there may be some confusion if you haven't watched that video so I'll just link it right here for you guys and it could also be pretty interesting because if you watch that video and you watch this one you can essentially create a program where you're sending data back and forth from your local computer to your Pico and you can do some Advanced things if you watch both those videos and you combine the concepts from both of these videos so enough being said just a little background what's going on here in this setup is we have code on our Raspberry Pi Pico which we're going to go over here in a second on the main.pod file but we also have is we have code on our local computer so I just saved a file called local.pi so I'm on my terminal and this is just an editor for my for my code so my python code on my local computer so we're running this python code on my local computer in conjunction with running this code on the Raspberry Pi Pico so we can send data from our local computer via the serial port to the pickup and so in this micro python code we'll just go over this first what we have here is we have a series of imports and we're just doing this while loop here where we're using this you select import to select standard in empty list empty list 0. so this is essentially the the most confusing part of this code and it's it's not that confusing once once we go over what this function essentially does so how we're sending data from our local computer to our Raspberry Pi Pico is we're just doing it from the standard in that would that is we're just going to send it through the serial port and print to the standard end of that serial port and our Raspberry Pi Pico will process the data as it's coming in from our local computer so in this function we have four parameters so we have this first one here as you can see I have some brief explanation which we'll go over so this is just a list of the file objects to monitor for input in this case we're just monitoring the standard in so you can actually monitor other things in more advanced use cases however a standard in is enough for this use case other than that we have two empty lists here so for these inputs this is the list of file objects to monitor for output so we're not monitoring anything for output here and we're not monitoring anything for exceptional conditions and finally this zero here this is just the timeout duration in milliseconds so this is just how long we're waiting for something to populate in the standard in so in this case it's set to zero that means it's non-blocking so if we do not find anything in the standard in we're just going to continue so if there's none it will immediately return and we'll continue this while loop set so there'll be no slowing down of our code as we are running but you can actually play with this value here and make sure if you increase it I believe this is a millisecond so if you do one you can wait for one milliseconds to wait for something in the standard in okay so that's all we're doing here we're not waiting for anything it's non-blocking so we're just going to process the data as it comes along and we have this buffer here so what's happening is the buffer is initially set to zero and we're just going to read the select result so whatever we're getting from the standard in and we're just going to read character by character here so standardin.read1 and what we're doing here is we're going to process each character by character in this case because we're sending a stream of of numbers and so what we're saying is if the character is not equal to comma this is the the new line character we're going to add a character to this buffer otherwise we're going to save the so if we do get a comma that means we have essentially a new data point so that's sort of our signal from our local computer that we just received a new data point so what we're going to do in that case is we're just going to save that buffer to the the CSV file here so we're just calling this save to CSV and this is opening a CSV file name I just named the CSV file data.csv but really you can name it whatever you want and we have this a so a means we're just appending to the file so if you have an existing file it's just going to append more data points to that but you can change this value here based on your needs you can truncate the whole file and start over and we're just going to write the data point to the file and write a new line and then we're just going to reset the buffer as 0 and then finally we're just going to once again read from the the standard in with non-blocking and we're going to continue processing um forever as long as this thing is plugged into the computer so that's really all we're doing here on the Raspberry Pi Pico side so we're getting data we're processing it one standard input at a time and if it's equal to a comma we know we received a new data point and so we're just going to essentially truncate the buffer save it to the the CSV and start over until we get more standard in Forever Until We plug this thing out of the computer now one thing you'll know have to know here is this is on the main.pi file that is really important because we can't run this code in thoni so I can't just run this code and run the python code at the same time because they're trying to access the same serial port and that simply will not work so the reason it's on the main dot piles is because this is the file that gets executed when your device is plugged into your computer regardless of the serial Port so if I go ahead and plug out this Raspberry Pi Pico and plug it back in this main.pi file will start executing immediately so just one thing to note there so that's what that's the behavior we want because we want this to run in conjunction with this script otherwise if we try to run it here in conjunction with the script we will have issues accessing the same serial Port so that'll all make much more sense in a second let's just jump to the code on our local computer here I hope you guys like this green and black here for my for the terminal some people don't like it but I prefer it just because it makes things stand out so we have a very simple script on our terminal so this is just a python script and this is just a python script in my desktop so let me just actually exit this first and show you so for those of you guys who haven't used terminal before on your Mac it's just a way to access files on your computer and do things with files or other processes with essentially a Unix shell so I have this local.pi file that's located on my MacBook here and so I'm just going to VI into it and VI is just a text editor but really you can edit this file wherever you want if you if you're using pycharm on your local computer or whatever python editor you have you can simply do that so I'm just going to VI into a python file so I would imagine you already have python on your local computer and so I just bi into that so this file already exists and you could just copy the code I have here on your computer the only thing you'll have to change is the serial port so keep that in mind I just got the serial Port from Thane on the bottom right there I'm on a Windows it's a little different serial Port formats and I believe on Linux as well so just keep that in mind the serial Port will be maybe a com Port I believe so just keep that in mind and so in this python script essentially what we're doing is we're just connecting to the serial Port of our Raspberry Pi Pico and we're just doing something simple here in terms of our data today so we're just saying for I in range one to A Thousand and One we're just going to print I so I'm going to print it on the screen and what I'm going to do that's just for debugging purposes it has no no ties to sending data but where we send data is the serial connection.writes so we're just going to write to that serial port and that's just going to be a string of this number plus comma and the reason I added the comma there is just because it's a signal to the Raspberry Pi Pico that this is a a new data point so we want to segment the the rows of the numbers that we're getting from this data stream so I just decided that I'm going to use a comma for that as a signal to my Raspberry Pi Pico that this is a new row of data and we're just going to encode it so we have to send it in bytes so we're just using Dot and code and finally we have this time.sleep point zero one and the reason this is important here is because if we don't have this time that sleep what I realized is that the Raspberry Pi Pico has issues processing all of the data so if we just flood the serial port with a bunch of data as fast as we can we may have issues in essentially missing some data in a buffer and so a good way I found to do this is to add a small sleep value while you're sending data over the serial port and I found that .01 works in my case you may want to play with this value or find other ways to smoothly send data or process all the data in the buffer in real time but I think this is a good way and it's a very small amount of time to wait for processing maybe in your case you want to think of some other ways to deal with that buffer and making sure you process all the buffer but just now I added this to make sure we process all the data we're sending and finally I have this time.sleep before we close the serial connection because another thing I realized is that if we close the serial connection right away is that the tail end of the data will get truncated and we won't process all the data so keep that in mind as well so you you may want to add a little sleep at the end before you close that serial connection so we're doing on the python side once again at a high level is we're just printing to or we're just writing these data points to that serial ports and the Pico is processing all the data and it's going to send it to a CSV file so in order to get this running if you watch the first video what we want to do is simply first of all save this python script and we can see we're connected and thinly to the Raspberry Pi Pico but what as we mentioned is we can't run this anthonia at the same time we do on our local computer so I want to do is I want to plug this in outs of our also let me delete this data.csv file because I don't want it to append to that let's just delete that and so I just want to plug my picot in and out so I don't have a serial connection in thoni so I can access the zero port in the python script so I'm going to plug it out as you can see connection loss and I'm just going to plug it back in so now we'd have to do anything in thoni so the main.pod file as I plug this Pico back in by the way I'm not on video today so you can't see me plug it back in but the picot is plugged back in what's going to happen is this main.pi file is executing right now continuously so it's waiting for some data on the standard in and there's nothing happening so it's not writing anything so we're gonna have to do now is we're going to have to jump back to our python file here and we're just going to run this python local.pi so let me just go ahead and run it here so just give it a few moments there there's a small sleep point zero one seconds to send all that data over and if everything happened properly we should see a data.csv file of a thousand data points uh one set a thousand and we'll be able to save see that after the script is done running so just give that a moment here sometimes it does take some time to process there um all the data points so just a few more seconds here we're almost at the end and of course you could do much more complicated scenarios in this case of data transport now just doing a timed out sleep for 10 seconds to make sure all the buffer is processed at the tail end of the data that's being sent over so now that we have that what we can go do now is we can actually go back to our Thani IDE connect to the device and we will see a data.csv file here we actually can't open CSV files in thony I don't know why so I'm just going to move it to my local computer okay so I just moved it to my desktop and let's just open this and I'm just going to open it in the default app on a Mac so we can see here that we have all these data points let's hopefully we process the Thousand properly so if we scroll all the way down we can see that we have one two thousand which is pretty awesome so I hope you guys enjoyed the video once again I will link all the sources for the code here and my GitHub page so I'll link those down in the description below and once again this has amazing use cases for data processing on your local computer on your pico so I hope I gave you the tools and everything makes sense for you to start sending data back and forth on your devices so hopefully you guys can do some more advanced things after watching this in part one if you enjoyed this video I hope you guys subscribe to the channel because there's much more of that to come in future videos we will be doing some Advanced robotics projects and other contents such as full stack content if you're interested in that in Python content as well and other microcontroller content so there's much to be seen on this channel so if you're not a subscriber Please Subscribe I promise you there is a lot of value that you can gain from subscribing to this Channel and that being said guys just let me know if you have any questions in the comment section down below I know we went over a lot and it can be a little confusing in terms of running the code on your local computer and on your pico so please do not be shy and let me know if you have any questions I will do my best to answer you stay tuned guys thanks for watching and take it easy [Music] thank you
Info
Channel: Shilleh
Views: 2,525
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: mi9UT7fTDZw
Channel Id: undefined
Length: 13min 29sec (809 seconds)
Published: Tue Sep 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.