# Updating a python File on a raspberry pi via notecard

**URL:** <https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199>\
**Category:** Notecard API\
**Created:** [April 7, 2021, 1:39am UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199 "2021-04-07T01:39:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 7, 2021, 1:39am UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/1 "2021-04-07T01:39:23Z")

</div>

I would have to think this is possible.

I am developing a control panel application on a rpi4, touch screen and notecard.

The resulting python files are modular and relatively small. Is there some kind of guidelines for doing some kind of REST GET or some other method of transferring small files through the notecard into a folder on the pi?

I could technically send them 1 line at a time I suppose, breaking on the /n and appending to a file, but that seems messy?

Is there any best practice way of accomplishing this in a secure fashion?

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 7, 2021, 12:57pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/2 "2021-04-07T12:57:45Z")

</div>

Hey @vstadmin great question. You can do this using the Notecard `web.get` api documented [here](https://dev.blues.io/reference/notecard-walkthrough/web-transactions/). The Notecard must be in continuous mode, and you’ll need to break the file into 4-8k chunks (depending on the strength of your connection) and re-assemble on the Pi-side.

The endpoint you retrieve the files from also has to return JSON as the Notecard doesn’t support other content types. My suggestion would be to serve the files in a JSON body with each chunk as a base-64 encoded string, then reassemble on the Pi once you have all the chunks you need.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Carl-Henderson](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/carl-henderson/32/587_2.png) [@Carl-Henderson](https://discuss.blues.com/u/Carl-Henderson)\
**Post date:** [April 7, 2021, 5:10pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/3 "2021-04-07T17:10:54Z")

</div>

The file size limit is larger than 4k with the over-the-air [device firmware update (DFU)](https://dev.blues.io/reference/notecard-walkthrough/host-firmware-update-requests/) mechanism. I’ll see if I can track down a safe limit but I know we have examples greater than 42KB.

---

<div class="post-metadata">

**Author:** ![Carl-Henderson](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/carl-henderson/32/587_2.png) [@Carl-Henderson](https://discuss.blues.com/u/Carl-Henderson)\
**Post date:** [April 7, 2021, 5:13pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/4 "2021-04-07T17:13:29Z")

</div>

The Host DFU size limit is at least 700KB FYI.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 7, 2021, 5:28pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/5 "2021-04-07T17:28:35Z")

</div>

I did see these settings and limitation, but I thought they were specific for DFU transfer via the Blues Cloud of binary files to supported devices.

How would I use base64 to transfer small python text files from a web server to the Raspberry PI connected to the Blues Modem?

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 7, 2021, 5:34pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/6 "2021-04-07T17:34:24Z")

</div>

Hi @vstadmin that’s correct, those limitations are specific to host DFU, which uses the Notecard to store a binary before delivering it to the host. And while it is possible in theory to use the DFU mechanism to obtain Python files, as [discussed in this thread](https://discuss.blues.com/t/esp32-dfu-micropython/128) it’s not something that we have tested, so I cannot guarantee it will work.

If you’re looking to pull down `.py` files from a remote source to get into the filesystem of your device, the `web.get` approach is your best bet.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 7, 2021, 6:45pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/7 "2021-04-07T18:45:39Z")

</div>

I just want be able to use the Blues modem and do remote updates in the field. I’m just puzzling through the options.

It would be the same with using any micropython on circuit python device with a Blues modem, I would think? They all have to be updated from time to time in the field.

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 7, 2021, 6:54pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/8 "2021-04-07T18:54:59Z")

</div>

Yes it would, although I think you have a bit more flexibility on the Pi because you have direct filesystem access and can easily reassemble files from a remote location and update the appropriate file location directly. In Micro/CircuitPython, this process would require creating a custom bootloader in a host-native language to handle updating the Python files.

Hope that makes sense. If it would be helpful, I am happy to create a detailed breakdown of how I think this could work and/or a sample app to illustrate it.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 7, 2021, 7:27pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/9 "2021-04-07T19:27:14Z")

</div>

That would be immensely helpful. I think it would probably benefit a lot of people as well as we attempt to buy other hardware and get hit with 58 week lead times and have to use Raspberry Pi’s.

I really do appreciate it!

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 8, 2021, 12:36pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/10 "2021-04-08T12:36:32Z")

</div>

Happy to help @vstadmin. I will start putting together something in the next few days and hopefully can have something to share next week.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 13, 2021, 8:13pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/11 "2021-04-13T20:13:46Z")

</div>

Any chance that you have come up with a process for uploading python code through the blues modem?

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 13, 2021, 8:24pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/12 "2021-04-13T20:24:33Z")

</div>

Hey @vstadmin I am working on it, but don’t have anything to share yet. Will try to have something ready by the end of this week. Fingers crossed.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 16, 2021, 1:49pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/13 "2021-04-16T13:49:04Z")

</div>

I’m going into a project meeting in about 15 minutes. Any ideas yet?

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 16, 2021, 1:56pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/14 "2021-04-16T13:56:48Z")

</div>

Ideas and progress! I’ll follow-up with details and a sample later today.

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 16, 2021, 2:09pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/15 "2021-04-16T14:09:39Z")

</div>

In case you are curious, here is the process the sample I am building will follow:

1. A `.py` file is uploaded to an S3 bucket.
2. The bucket triggers a Lambda function which uses the Notehub API to send an inbound Note to the Notecard.
3. The Notecard detects the inbound Note and fires the `ATTN` pin.
4. The Host (Raspberry Pi) detects the ATTN pin is HIGH and performs a `web.get` request against an endpoint that is running another Lambda function.
5. The function retrieves the file from S3 and performs a base64 encoding on the contents. That Base64 string is returned as a payload to the Notecard. The function breaks files larger than 4k into chunks and can be pinged multiple times to retrieve the entire file if over 4k.
6. If multiple calls are needed to retrieve the entire file, `web.get` is called until the file has been downloaded.
7. Take the base64 string for all chunks, decode and save to disk.
8. Restart the main python program file to reload the changed source file.

I’ll be providing examples for both updating a utility file, and the main program file.

S3 and Lambda are just example in this case and can be swapped in for whichever provider you prefer.

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 16, 2021, 2:21pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/16 "2021-04-16T14:21:52Z")

</div>

Thanks for the reply. I’m in the meeting now. I can share that we have several “ideas”

---

<div class="post-metadata">

**Author:** ![bsatrom](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/bsatrom/32/3_2.png) [@bsatrom](https://discuss.blues.com/u/bsatrom)\
**Post date:** [April 16, 2021, 10:23pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/17 "2021-04-16T22:23:12Z")

</div>

Hey @vstadmin here’s a working sample for you to reference. It follows the process above and the full details are here: [note-samples/python-file-downloader at main · blues/note-samples · GitHub](https://github.com/blues/note-samples/tree/main/python-file-downloader).

Hope it’s useful!

 ![downloader-example](https://us1.discourse-cdn.com/flex020/uploads/blues/original/1X/f74086b7cc4bca5f065a391dd5fbcca8bb5fe362.png)

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 17, 2021, 10:52am UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/18 "2021-04-17T10:52:15Z")

</div>

Thank you! I will enjoy digging through this!

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [April 19, 2021, 2:08pm UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/19 "2021-04-19T14:08:34Z")

</div>

I really appreciate this. I will be looking at it over the next couple of days

---

<div class="post-metadata">

**Author:** ![vstadmin](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vstadmin](https://discuss.blues.com/u/vstadmin)\
**Post date:** [June 3, 2021, 10:41am UTC](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199/20 "2021-06-03T10:41:25Z")

</div>

@bsatrom - I have been looking at this part of our project (finally) and have had another person looking at it for about a week. I’m an intermediate level programmer who has made considerable progress in the last month on our main monitoring program to monitor and report pressure from a vapor recovery system for the gasoline distribution industry.

That being said, we do not have a lot of experience with AWS or their version of “lambda”. I understood “lambda” to be an anonymous function that I have only touched a couple of times in programming tutorials. Your python guide for AWS seems to be extremely comprehensive and would be well suited for our needs, but we just cannot figure it out.

There seems to be something missing in the glue that connects all the functions together between the web hooks and the functions and we cannot really determine which function goes where and how they all inter-relate.

Is there a simplier way perhaps? one were we just drop a file in any accessible location and the send a note to card to begin the download process? We have our own linux server currently exposed on port 80 and responding to web hooks from particle and is being used to receive and collect the data from units already. I could easily add a python script there to received a command from a blues webhook.

Any ideas?

looking at the notifier code, you have:

```auto
def lambda_handler(event, context):
    project = os.environ['PRODUCT_UID']
    device = os.environ['DEVICE_UID']

```

I do not understand how a specific notecard actually gets notified that there is a new file waiting.

How does the notifier get the project and device within the scope of the AWS lambda function?

[Next page](https://discuss.blues.com/t/updating-a-python-file-on-a-raspberry-pi-via-notecard/199.md?page=2)
